0

how can check if user platform is windows or web or android or IOS in asp.net mvc?

I think we can use something like

Request.Browser.Platform

but don't know how can check user platform specially when platform is windows (I mean desctop application not web or mobile)

thanks

I tried this code

      var Agent = Request.UserAgent;


        if (Agent.ToLower().Contains("windows"))
        {
            return "WINDOWS";
        }
        else if (Agent != null && (Agent.ToLower().Contains("android")))
        {
            return "ANDROID";
        }
        else if (Agent != null && (Agent.ToLower().Contains("ios")))
        {
            return "IOS";
        }
        else
        {
            return "WEB";
        }

but when I test it using locallhost it returns windows not web what's wront here?

Liam
  • 22,818
  • 25
  • 93
  • 157
  • 2
    You're probably not going to be able to determine anything outside of the information the browser they are using can give you - probably via the UserAgent signature. See this answer to see if it's any help: https://stackoverflow.com/questions/9734668/how-do-i-detect-user-operating-system – scgough Oct 03 '18 at 10:16
  • Using C# you can't, C# runs on the server. The HTTP request doesn't contain this information. [Javascript can determine some information which you could then send to the server](https://stackoverflow.com/questions/9514179/how-to-find-the-operating-system-version-using-javascript) – Liam Oct 03 '18 at 10:45

1 Answers1

0
public String GetUserEnvironment(HttpRequest request)
{
    var browser = request.Browser;
    var platform = GetUserPlatform(request);
    return string.Format("{0} {1} / {2}", browser.Browser, browser.Version, platform);
}

public String GetUserPlatform(HttpRequest request)
{
    var ua = request.UserAgent;

    if (ua.Contains("Android"))
        return string.Format("Android {0}", GetMobileVersion(ua, "Android"));

    if (ua.Contains("iPad"))
        return string.Format("iPad OS {0}", GetMobileVersion(ua, "OS"));

    if (ua.Contains("iPhone"))
        return string.Format("iPhone OS {0}", GetMobileVersion(ua, "OS"));

    if (ua.Contains("Linux") && ua.Contains("KFAPWI"))
        return "Kindle Fire";

    if (ua.Contains("RIM Tablet") || (ua.Contains("BB") && ua.Contains("Mobile")))
        return "Black Berry";

    if (ua.Contains("Windows Phone"))
        return string.Format("Windows Phone {0}", GetMobileVersion(ua, "Windows Phone"));

    if (ua.Contains("Mac OS"))
        return "Mac OS";

    if (ua.Contains("Windows NT 5.1") || ua.Contains("Windows NT 5.2"))
        return "Windows XP";

    if (ua.Contains("Windows NT 6.0"))
        return "Windows Vista";

    if (ua.Contains("Windows NT 6.1"))
        return "Windows 7";

    if (ua.Contains("Windows NT 6.2"))
        return "Windows 8";

    if (ua.Contains("Windows NT 6.3"))
        return "Windows 8.1";

    if (ua.Contains("Windows NT 10"))
        return "Windows 10";

    //fallback to basic platform:
    return request.Browser.Platform + (ua.Contains("Mobile") ? " Mobile " : "");
}

public String GetMobileVersion(string userAgent, string device)
{
    var temp = userAgent.Substring(userAgent.IndexOf(device) + device.Length).TrimStart();
    var version = string.Empty;

    foreach (var character in temp)
    {
        var validCharacter = false;
        int test = 0;

        if (Int32.TryParse(character.ToString(), out test))
        {
            version += character;
            validCharacter = true;
        }

        if (character == '.' || character == '_')
        {
            version += '.';
            validCharacter = true;
        }

        if (validCharacter == false)
            break;
    }

    return version;
}

Saw it here- https://stackoverflow.com/a/32124271/4836581

Zvi Redler
  • 1,490
  • 1
  • 12
  • 21