Reputation: 4042
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?
Upvotes: 1
Views: 4830
Reputation: 31198
The browserCaps element is deprecated in ASP.NET 2.0 and higher. Unless you're using .NET 1 or 1.1, you should use a browser definition file instead.
Add the *App_Browsers* folder to your site, if it doesn't already exist, and create a new file called "Platforms.browser". (The name doesn't matter; only the extension.)
Open the new .browser file and paste in the following:
<browsers>
<gateway id="PlatformWinVista" parentID="PlatformWinnt">
<identification>
<userAgent match="Windows NT 6\.0" />
</identification>
<capabilities>
<capability name="platform" value="Windows Vista" />
</capabilities>
</gateway>
<gateway id="PlatformWin7" parentID="PlatformWinnt">
<identification>
<userAgent match="Windows NT 6\.1" />
</identification>
<capabilities>
<capability name="platform" value="Windows 7" />
</capabilities>
</gateway>
</browsers>
You might need to trigger a recompilation of the site for the new file to take effect.
NB: These nodes have to be gateway nodes rather than browser nodes. If you try to create them as browser nodes, you'll get a parser error when your site recompiles.
Upvotes: 7
Reputation: 1518
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.
Upvotes: 4