Getting Client Specific Information using C#

1. Get Browser Details: There is a simple namespace available in C# which helps the user to get the browser details of the individual using the application – HttpBrowserCapabilities.

We can use the “Request” keyword to get the browser information for the current request and get it as an object of the type HttpBrowserCapabilities.

 HttpBrowserCapabilities

This provides us a lot many information regarding the browser. The few of them are as below:

  1. Browser Name
  2. Cookies Value – True or False, specifying whether the browser can have cookies.
  3. Input type – “Keyboard” or something else.
  4. isMobileDevice – To check whether the device is a mobile device or not.

I would like to point out one more thing regarding how to know if the current request device is a Mobile or a desktop machine. Please find the code for the same as below:

bool bl = context.Request.Browser.IsMobileDevice;

This will return “true “if the browser is a mobile device else return “false” for a desktop device.

2. Get User IP Address: Many times we have a need to get the IP Address of the user that is visiting the application. We can use it by a small piece of code as mentioned in the screenshot below:

code

When users are behind any proxies or routers the REMOTE_ADDR returns the IP Address of the router and not the client user’s machine. Hence first we need to check HTTP_X_FORWARDED_FOR, since when client user is behind a proxy server his machine’s IP Address the Proxy Server’s IP Address is appended to the client machine’s IP Address. If there are multiple proxy servers the IP Addresses of all of them are appended to the client machine IP Address.

Hence we need to first check HTTP_X_FORWARDED_FOR and then REMOTE_ADDR.