. NET get the client's operating system version, browser version and IP address-practical tips

Source: Internet
Author: User
Tags get ip httpcontext servervariables

We are using. NET to do the site, in many cases need to know the client's operating system version and browser version, how to obtain the client's operating system and browser version? We can get it by analyzing useragent.

. NET gets the operating system of the client

Looking at the code below, we first create a way to get the operating system by analyzing useragent

<summary>///Get the name of the operating system///</summary>///<param name= "useragent" ></param>///<returns&

 gt;</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 (")") 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) {osversion = "Windows 98";
 else if (useragent.contains) {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 return the specific operating system and version by parsing the operating system string information contained in the useragent. Where Request.Browser.Platform is the kernel of the operating system, if none of the above matches directly return to the operating system kernel version, the above method can detect the latest Windows 10, and can detect Apple, Linux, SunOS and other mainstream operating systems.

Call method We can write like the following code:

Copy Code code as follows:
String systemname = Getosnamebyuseragent (System.Web.HttpContext.Current.Request.UserAgent);

The useragent can be returned to the operating system by passing in the request.

. NET gets the browser version of the client

. NET get Browser version is very simple, only need to pass the browser intrinsic properties of the request, such as the following code

Copy Code code as follows:
String browsername = System.Web.HttpContext.Current.Request.Browser.Browser + "" + System.Web.HttpContext.Current.Request.Browser.Version;

. NET Get IP Address:

<summary>
///Get current client IP address
///</summary>
///<returns></returns>
public static string getcurrentuserhostaddress ()
{
 string userhostaddress = "";
 userhostaddress = system.web.httpcontext.current.request.servervariables["Http_x_forwarded_for"];
 if (string. IsNullOrEmpty (userhostaddress))
 {
 ///No proxy IP, direct access to client IP 
 userhostaddress = system.web.httpcontext.current.request.servervariables["REMOTE_ADDR"];
 }
 return userhostaddress;
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.