Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I detect user operating system

I have the following code to obtain user details:

HttpBrowserCapabilities bc = HttpContext.Current.Request.Browser; string UserAgent = HttpContext.Current.Request.UserAgent;  ENT_TrackingData ret = new ENT_TrackingData() {     IPAddress = HttpContext.Current.Request.UserHostAddress,     Browser = bc.Browser + " " + bc.Version,                     DateStamp = DateTime.Now,     PageViewed = HttpContext.Current.Request.Url.AbsolutePath,     NodeId = UmbracoHelper.GetCurrentNodeID(),     IsMobileDevice = IsMobileDevice(UserAgent),     Platform = bc.Platform }; 

This works great but I noticed that the Platform always says windows NT for my machine not Windows 7. Is there any way to detect this type of information in ASP.Net?

like image 622
Funky Avatar asked Mar 16 '12 09:03

Funky


People also ask

Can JavaScript detect OS?

Detect Operating System With the userAgent Property in JavaScript. If we want to track a little detail about the user's used browser and platform, we can go for the userAgent property with the navigator object. This special feature returns a string containing the browser's name, version, and platform name.

How does PHP detect operating system?

php_uname() returns a description of the operating system PHP is running on. This is the same string you see at the very top of the phpinfo() output. For the name of just the operating system, consider using the PHP_OS constant, but keep in mind this constant will contain the operating system PHP was built on.


1 Answers

Here's what I came up with and it seems to work fairly well:

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; } 
like image 149
pistol-pete Avatar answered Sep 28 '22 03:09

pistol-pete