. NET obtains the operating system version, browser version, and IP address of the client. net browser version
When using. NET as a website, we often need to know the operating system version and browser version of the client. How can we obtain the operating system and browser version of the client? We can obtain it by analyzing UserAgent.
. NET to obtain the client operating system
See the following code. First, create a method to obtain the operating system by analyzing UserAgent.
/// <Summary> /// obtain the operating system name /// </summary> /// <param name = "userAgent"> </param> // <returns> </returns> public static string GetOSNameByUserAgent (string userAgent) {string osVersion = "unknown"; if (userAgent. contains ("NT 10.0") {osVersion = "Windows 10";} else if (userAgent. contains ("NT 6.3") {osVersion = "Windows 8.1";} else if (userAgent. contains ("NT 6.2") {osVersion = "Windows 8";} else if (userAgent. contains ("NT 6.1") {osVersion = "Windows 7";} else if (userAgent. contains ("NT 6.1") {osVersion = "Windows 7";} else if (userAgent. contains ("NT 6.0") {osVersion = "Windows Vista/Server 2008";} else if (userAgent. contains ("NT 5.2") {if (userAgent. contains ("64") osVersion = "Windows XP"; else osVersion = "Windows Server 2003";} else if (userAgent. contains ("NT 5.1") {osVersion = "Windows XP";} else if (userAgent. contains ("NT 5") {osVersion = "Windows 2000";} else if (userAgent. contains ("NT 4") {osVersion = "Windows NT4";} else if (userAgent. contains ("Me") {osVersion = "Windows Me";} else if (userAgent. contains ("98") {osVersion = "Windows 98";} else if (userAgent. contains ("95") {osVersion = "Windows 95";} else if (userAgent. contains ("Mac") {osVersion = "Mac";} else if (userAgent. contains ("Unix") {osVersion = "UNIX";} else if (userAgent. contains ("Linux") {osVersion = "Linux";} else if (userAgent. contains ("SunOS") {osVersion = "SunOS";} else {osVersion = System. web. httpContext. current. request. browser. platform;} return osVersion ;}
The above method is to parse the operating system string information contained in UserAgent and return the specific operating system and version, where Request. browser. platform is used to obtain the kernel of the operating system. If none of the above matches, the kernel version of the operating system is directly returned. The method above can detect the latest Windows 10, it can also detect mainstream operating systems such as Apple, Linux, and SunOS.
You can write the call method as follows:
Copy codeThe Code is as follows: string systemName = GetOSNameByUserAgent (System. Web. HttpContext. Current. Request. UserAgent );
The user agent that passes the Request can return to the operating system.
. NET to get the browser version of the Client
. NET to get the Browser version is very simple, you only need to pass the inherent property of the Browser request, the following code
Copy codeThe Code is as follows: string browserName = System. Web. HttpContext. Current. Request. Browser. Browser + "+ System. Web. HttpContext. Current. Request. Browser. Version;
. Net to obtain the IP Address:
/// <Summary> /// obtain the IP address of the current client /// </summary> /// <returns> </returns> public static string GetCurrentUserHostAddress () {string userHostAddress = ""; userHostAddress = System. web. httpContext. current. request. serverVariables ["HTTP_X_FORWARDED_FOR"]; if (string. isNullOrEmpty (userHostAddress) {// if no proxy IP address is available, the Connection Client IP address userHostAddress = System is directly obtained. web. httpContext. current. request. serverVariables ["REMOTE_ADDR"];} return userHostAddress ;}
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.