. NET obtains the operating system version, browser version, and IP address of the client. net browser version

Source: Internet
Author: User

. 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.