Search code examples
c#.netasp.netuser-agenthttpbrowsercapabilities

HttpBrowserCapabilities missing some information?


I'd like to use the Request.Browser property (HttpBrowserCapabilities class) to determine some properties of the client's system.

However I seem to run into some limitations of this class. I can't find some properties that should be relatively easy to parse from the UserAgent string, like the OS version (Platform will only return WinNT for most Windows versions, but not Vista, XP, etc.) or whether it's x64 or not (only Win16 and Win32 properties).

I would have expected to see these properties in the HttpBrowserCapabilities class, because most other user agent information is there. Am I missing something? Can I find this information somewhere else? Or should I just parse it from the UserAgent string myself?


Solution

  • You can extend the HttpBrowserCapabilities by adding/extending the browserCaps configuration section in your machine.config/web.config file. For example, to detect the OS version more accurately, add something like this to your config file:

    <system.web>
        <browserCaps>
          <use var="HTTP_USER_AGENT" />
          <filter>
            <case match="Windows NT 6.1">
              platform=Windows7
            </case>
          </filter>
        </browserCaps>
      </system.web>
    

    If you access the web site with Mozilla running on windows 7 (UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1") you'll see that Request.Browser.Platform will display "Windows7". See here for more information: http://msdn.microsoft.com/en-us/library/sk9az15a%28v=vs.71%29.aspx

    Add a regex in the browserCaps section to match the WOW64 string in order to detect whether the client platform is 64 bit (I'm not sure what the WOW64-equivalent is for non-Windows platforms running on 64 bit).

    ...Of course, by using a regex in the element you're doing nothing else than actually parsing the UserAgent string yourself. However you can easily find predefined browseCaps on the web (e.g. http://owenbrady.net/browsercaps/CodeProject.xml).

    Keep in mind that even though this capability is quite powerful, it still is not 100% accurate. For example, both Windows 7 and Windows Server 2008 R2 will return Windows NT 6.1 as the platform.