ASP.NET: Access Client Info Server-side
I was playing around with HTTP request headers and for some reason started thinking about how limited information they provide about the requesting browser.
All they really provide is the user agent string which provide very little information about the visitors platform, and that’s about it. If they also provided screen resolution and available browser window real estate, it would start being useful for other scenarios than we are used to.
This means that it should be possible to retrieve the information in a way that is totally transparent to the visitor. In other words, the website appear to act 100% as usual. If you don’t believe me, read on and I’ll tell you how it’s done.
How it works
To do the trick we need an HttpModule and HttpHandler. The module’s job is to expose the client-side variables to the ASP.NET code while the HttpHandler fills them with data from the browser.
Before you think it sounds too complex, let me assure you that it is very simple. Not only is it simple, it is also plug ‘n play so you can easily implement it into any existing web application – no code required. I’ve tested it in IE 7, Firefox 2 and Opera 9 all on Windows and it works perfectly. This might be the coolest thing I’ve ever developed.
Download the ClientStats.cs below and put it in the App_Code folder of your website. Then add the following lines to the web.config’s <system.web> section:
<add type="ClientStats" name="ClientStats"/>
<add verb="*" path="*clientstats.aspx" type="ClientHandler" />
That is all there is needed to gain access to the properties of the ClientStats module.
Mads Kristensen currently works as a Senior Developer at Traceworks located
in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in
2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and
web services in his daily work as well. A true .NET developer with great passion for the simple solution.