2012-03-16 82 views
15

我有下面的代码,以获得用户的详细信息:我怎样检测用户操作系统

HttpBrowserCapabilities bc = HttpContext.Current.Request.Browser; 
string UserAgent = HttpContext.Current.Request.UserAgent; 

ENT_TrackingData ret = new ENT_TrackingData() 
{ 
    IPAddress = HttpContext.Current.Request.UserHostAddress, 
    Browser = bc.Browser + " " + bc.Version,     
    DateStamp = DateTime.Now, 
    PageViewed = HttpContext.Current.Request.Url.AbsolutePath, 
    NodeId = UmbracoHelper.GetCurrentNodeID(), 
    IsMobileDevice = IsMobileDevice(UserAgent), 
    Platform = bc.Platform 
}; 

这个伟大的工程,但我注意到,该平台一直称Windows NT我的机器不能在Windows 7有什么在ASP.Net中检测这种类型的信息的方法?

+1

如果您使用的是Windows 7,你的平台_is_ “Windows NT”。也许你需要寻找能够给你平台版本号的东西? – 2012-03-16 09:27:20

+0

请阅读下面的答案以获取最新的解决方案。 – 2016-08-09 08:16:23

回答

2

没有准确这样做的方式,因为您从用户请求的标题获得的所有信息可以很容易地由用户更改,并且可以包含任何内容。

如果你有阅读可能是不准确的信息,OK,那么你可能要检查this SO answer to similar question

+0

在我看来,大多数用户甚至不知道请求标题是什么,更不用说要主动将它们改为* mask/hide *它们所连接的平台。您的陈述应为:“如果您阅读*潜在*不准确的信息...” – 2017-09-19 15:57:10

18

使用Request.UserAgent

if (Request.UserAgent.IndexOf("Windows NT 5.1") > 0) 
{ 
//xp 
} 
else if (Request.UserAgent.IndexOf("Windows NT 6.0") > 0) 
{ 
//VISTA 
} 
else if (Request.UserAgent.IndexOf("Windows NT 6.1") > 0) 
{ 
//7 
} 
else if (Request.UserAgent.IndexOf("Windows NT 6.2") > 0) 
{ 
//8 
} 
else if (Request.UserAgent.IndexOf("Windows NT 6.3") > 0) 
{ 
//8.1 
} 
else if (Request.UserAgent.IndexOf("Windows NT 10.0") > 0) 
{ 
//10 
} 
+0

else if(Request.UserAgent.IndexOf(“Windows NT 6.2”)> 0) { // 8 } – mjb 2013-01-01 10:07:11

+0

@mjb,如果你确定,就去找一个头并编辑答案,我没有Win 8来确认 – Habib 2013-01-01 10:10:22

+0

否则if(Request.UserAgent.IndexOf(“Windows NT 6.3”)> 0){// 8.1 } – Dreamwalker 2013-12-17 15:21:38

1

使用的版本号 “的Windows NT” 之后。 Windows 7的版本号为6.1。

但是不要太在意,用户代理字符串是标准。 例如查看this list,您会看到有人在Windows 9.0上使用Internet Explorer!

+1

那些可能是微软工程师测试新的操作系统;) – 2012-03-16 09:35:41

+1

我想要它,我即将安装我的新Visual Studio 14.0! :P – 2012-03-16 09:37:28

+0

闪存转发,我有2015年,woot。 – 2015-10-09 14:30:43

3
private string getOS() 
{ 
    string os = null; 
    if (Request.UserAgent.IndexOf("Windows NT 5.1") > 0) 
    { 
     os ="Windows XP"; 
     return os; 
    } 
    else if (Request.UserAgent.IndexOf("Windows NT 6.0") > 0) 
    { 
     os= "Windows Vista"; 
     return os; 
    } 
    else if (Request.UserAgent.IndexOf("Windows NT 6.1") > 0) 
    { 
     os = "Windows 7"; 
     return os; 
    } 
    else if (Request.UserAgent.IndexOf("Intel Mac OS X") > 0) 
    { 
     //os = "Mac OS or older version of Windows"; 
     os = "Intel Mac OS X"; 
     return os; 
    } 
    else 
    { 
     os = "You are using older version of Windows or Mac OS"; 
     return os; 
    } 

} 
9

根据This Official Microsoft Document我们可以用它来检测Windows操作系统:

String userAgent = Request.UserAgent; 

if (userAgent.IndexOf("Windows NT 6.3") > 0) 
{ 
    //Windows 8.1 
} 
else if (userAgent.IndexOf("Windows NT 6.2") > 0) 
{ 
    //Windows 8 
} 
else if (userAgent.IndexOf("Windows NT 6.1") > 0) 
{ 
    //Windows 7 
} 
else if (userAgent.IndexOf("Windows NT 6.0") > 0) 
{ 
    //Windows Vista 
} 
else if (userAgent.IndexOf("Windows NT 5.2") > 0) 
{ 
    //Windows Server 2003; Windows XP x64 Edition 
} 
else if (userAgent.IndexOf("Windows NT 5.1") > 0) 
{ 
    //Windows XP 
} 
else if (userAgent.IndexOf("Windows NT 5.01") > 0) 
{ 
    //Windows 2000, Service Pack 1 (SP1) 
} 
else if (userAgent.IndexOf("Windows NT 5.0") > 0) 
{ 
    //Windows 2000 
} 
else if (userAgent.IndexOf("Windows NT 4.0") > 0) 
{ 
    //Microsoft Windows NT 4.0 
} 
else if (userAgent.IndexOf("Win 9x 4.90") > 0) 
{ 
    //Windows Millennium Edition (Windows Me) 
} 
else if (userAgent.IndexOf("Windows 98") > 0) 
{ 
    //Windows 98 
} 
else if (userAgent.IndexOf("Windows 95") > 0) 
{ 
    //Windows 95 
} 
else if (userAgent.IndexOf("Windows CE") > 0) 
{ 
    //Windows CE 
} 
else 
{ 
    //Others 
} 
16

这就是我想出了,它似乎工作得相当好:

public String GetUserEnvironment(HttpRequest request) 
{ 
    var browser = request.Browser; 
    var platform = GetUserPlatform(request); 
    return string.Format("{0} {1}/{2}", browser.Browser, browser.Version, platform); 
} 

public String GetUserPlatform(HttpRequest request) 
{ 
    var ua = request.UserAgent; 

    if (ua.Contains("Android")) 
     return string.Format("Android {0}", GetMobileVersion(ua, "Android")); 

    if (ua.Contains("iPad")) 
     return string.Format("iPad OS {0}", GetMobileVersion(ua, "OS")); 

    if (ua.Contains("iPhone")) 
     return string.Format("iPhone OS {0}", GetMobileVersion(ua, "OS")); 

    if (ua.Contains("Linux") && ua.Contains("KFAPWI")) 
     return "Kindle Fire"; 

    if (ua.Contains("RIM Tablet") || (ua.Contains("BB") && ua.Contains("Mobile"))) 
     return "Black Berry"; 

    if (ua.Contains("Windows Phone")) 
     return string.Format("Windows Phone {0}", GetMobileVersion(ua, "Windows Phone")); 

    if (ua.Contains("Mac OS")) 
     return "Mac OS"; 

    if (ua.Contains("Windows NT 5.1") || ua.Contains("Windows NT 5.2")) 
     return "Windows XP"; 

    if (ua.Contains("Windows NT 6.0")) 
     return "Windows Vista"; 

    if (ua.Contains("Windows NT 6.1")) 
     return "Windows 7"; 

    if (ua.Contains("Windows NT 6.2")) 
     return "Windows 8"; 

    if (ua.Contains("Windows NT 6.3")) 
     return "Windows 8.1"; 

    if (ua.Contains("Windows NT 10")) 
     return "Windows 10"; 

    //fallback to basic platform: 
    return request.Browser.Platform + (ua.Contains("Mobile") ? " Mobile " : ""); 
} 

public String GetMobileVersion(string userAgent, string device) 
{ 
    var temp = userAgent.Substring(userAgent.IndexOf(device) + device.Length).TrimStart(); 
    var version = string.Empty; 

    foreach (var character in temp) 
    { 
     var validCharacter = false; 
     int test = 0; 

     if (Int32.TryParse(character.ToString(), out test)) 
     { 
      version += character; 
      validCharacter = true; 
     } 

     if (character == '.' || character == '_') 
     { 
      version += '.'; 
      validCharacter = true; 
     } 

     if (validCharacter == false) 
      break; 
    } 

    return version; 
} 
+0

工作得很好,谢谢 – Manish 2016-06-01 23:27:51

3

有一个酷工具命名为:https://github.com/ua-parser/uap-csharp
,解析用户代理到操作系统,浏览器,浏览器版本等...
Link to Nuget

这是如何使用它:

public static string GetUserOS(string userAgent) 
{ 
    // get a parser with the embedded regex patterns 
    var uaParser = Parser.GetDefault(); 
    ClientInfo c = uaParser.Parse(userAgent); 
    return c.OS.Family; 
} 
+0

这是你的工具吗? – SolidSnake4444 2016-08-17 21:55:21

+0

@ SolidSnake4444我希望,但不......我很高兴它帮助你。 – 2016-08-18 07:09:34

1

的VB.NET回答:我有它只是因为它可能会更容易阅读和理解。

Public Function GetOS() As String 
    Dim MyAgent As String = Current.Request.UserAgent 
    If MyAgent.IndexOf("Windows NT 10.0") >= 0 Then 
     Return "Windows 10" 
    ElseIf MyAgent.IndexOf("Windows NT 6.3") >= 0 Then 
     Return "Windows 8.1" 
    ElseIf MyAgent.IndexOf("Windows NT 6.2") >= 0 Then 
     Return "Windows 8" 
    ElseIf MyAgent.IndexOf("Windows NT 6.1") >= 0 Then 
     Return "Windows 7" 
    ElseIf MyAgent.IndexOf("Windows NT 6.0") >= 0 Then 
     Return "Windows Vista" 
    ElseIf MyAgent.IndexOf("Windows NT 5.2") >= 0 Then 
     Return "Windows Server 2003" 
    ElseIf MyAgent.IndexOf("Windows NT 5.1") >= 0 Then 
     Return "Windows XP" 
    ElseIf MyAgent.IndexOf("Windows NT 5.01") >= 0 Then 
     Return "Windows 2000 (SP1)" 
    ElseIf MyAgent.IndexOf("Windows NT 5.0") >= 0 Then 
     Return "Windows 2000" 
    ElseIf MyAgent.IndexOf("Windows NT 4.5") >= 0 Then 
     Return "Windows NT 4.5" 
    ElseIf MyAgent.IndexOf("Windows NT 4.0") >= 0 Then 
     Return "Windows NT 4.0" 
    ElseIf MyAgent.IndexOf("Win 9x 4.90") >= 0 Then 
     Return "Windows ME" 
    ElseIf MyAgent.IndexOf("Windows 98") >= 0 Then 
     Return "Windows 98" 
    ElseIf MyAgent.IndexOf("Windows 95") >= 0 Then 
     Return "Windows 95" 
    ElseIf MyAgent.IndexOf("Windows CE") >= 0 Then 
     Return "Windows CE" 
    ElseIf (MyAgent.Contains("iPad")) Then 
     Return String.Format("iPad OS {0}", GetMobileVersion(MyAgent, "OS")) 
    ElseIf (MyAgent.Contains("iPhone")) Then 
     Return String.Format("iPhone OS {0}", GetMobileVersion(MyAgent, "OS")) 
    ElseIf (MyAgent.Contains("Linux") AndAlso MyAgent.Contains("KFAPWI")) Then 
     Return "Kindle Fire" 
    ElseIf (MyAgent.Contains("RIM Tablet") OrElse (MyAgent.Contains("BB") AndAlso MyAgent.Contains("Mobile"))) Then 
     Return "Black Berry" 
    ElseIf (MyAgent.Contains("Windows Phone")) Then 
     Return String.Format("Windows Phone {0}", GetMobileVersion(MyAgent, "Windows Phone")) 
    ElseIf (MyAgent.Contains("Mac OS")) Then 
     Return "Mac OS" 
    ElseIf MyAgent.IndexOf("ANDROID") >= 0 Then 
     Return String.Format("Android {0}", GetMobileVersion(MyAgent, "ANDROID")) 
    Else 
     Return "OS is unknown." 
    End If 
End Function 

Private Function GetMobileVersion(userAgent As String, device As String) As String 
    Dim ReturnValue As String = String.Empty 
    Dim RawVersion As String = userAgent.Substring(userAgent.IndexOf(device) + device.Length).TrimStart() 
    For Each character As Char In RawVersion 
     If IsNumeric(character) Then 
      ReturnValue &= character 
     ElseIf (character = "." OrElse character = "_") Then 
      ReturnValue &= "." 
     Else 
      Exit For 
     End If 
    Next 
    Return ReturnValue 
End Function 
+0

弗兰克是对的。我将所有的WINDOWS改为Windows。 – 2017-02-08 23:43:26

0

约翰的VB功能不错,但Windows 10的行不起作用,因为你有大写的“WINDOWS”。

它应该是别人一样,即

If MyAgent.IndexOf("Windows NT 10.0") >= 0 Then 
     Return "Windows 10" 
0

所有在一个类

public class OS 
     { 
      public string os_name { get; set; } 
      public string os_version { get; set; } 

      public OS() 
      { 
       var ua = HttpContext.Current.Request.UserAgent; 
       if (ua.Contains("Android")) 
       { 
        this.os_name = "Android"; 
        SetVersion(ua, "Android"); 
        return; 
       } 

       if (ua.Contains("iPhone")) 
       { 
        this.os_name = "iPhone"; 
        SetVersion(ua, "OS"); 
        return; 
       } 

       if (ua.Contains("iPad")) 
       { 
        this.os_name = "iPad"; 
        SetVersion(ua, "OS"); 
        return; 
       } 

       if (ua.Contains("Mac OS")) 
       { 
        this.os_name = "Mac OS"; 
        return; 
       } 

       if (ua.Contains("Windows NT 10")) 
       { 
        this.os_name = "Windows"; 
        this.os_version = "10"; 
        return; 
       } 

       if (ua.Contains("Windows NT 6.3")) 
       { 
        this.os_name = "Windows"; 
        this.os_version = "8.1"; 
        return; 
       } 

       if (ua.Contains("Windows NT 6.2")) 
       { 
        this.os_name = "Windows"; 
        this.os_version = "8"; 
        return; 
       } 


       if (ua.Contains("Windows NT 6.1")) 
       { 
        this.os_name = "Windows"; 
        this.os_version = "7"; 
        return; 
       } 

       if (ua.Contains("Windows NT 6.0")) 
       { 
        this.os_name = "Windows"; 
        this.os_version = "Vista"; 
        return; 
       } 

       if (ua.Contains("Windows NT 5.1") || ua.Contains("Windows NT 5.2")) 

       { 
        this.os_name = "Windows"; 
        this.os_version = "XP"; 
        return; 
       } 

       if (ua.Contains("Windows NT 5")) 
       { 
        this.os_name = "Windows"; 
        this.os_version = "2000"; 
        return; 
       } 

       if (ua.Contains("Windows NT 4")) 
       { 
        this.os_name = "Windows"; 
        this.os_version = "NT4"; 
        return; 
       } 

       if (ua.Contains("Win 9x 4.90")) 
       { 
        this.os_name = "Windows"; 
        this.os_version = "Me"; 
        return; 
       } 

       if (ua.Contains("Windows 98")) 
       { 
        this.os_name = "Windows"; 
        this.os_version = "98"; 
        return; 
       } 

       if (ua.Contains("Windows 95")) 
       { 
        this.os_name = "Windows"; 
        this.os_version = "95"; 
        return; 
       } 


       if (ua.Contains("Windows Phone")) 
       { 
        this.os_name = "Windows Phone"; 
        SetVersion(ua, "Windows Phone"); 
        return; 
       } 

       if (ua.Contains("Linux") && ua.Contains("KFAPWI")) 
       { 
        this.os_name = "Kindle Fire"; 
        return; 
       } 

       if (ua.Contains("RIM Tablet") || (ua.Contains("BB") && ua.Contains("Mobile"))) 
       { 
        this.os_name = "Black Berry"; 
        return; 
       } 

       //fallback to basic platform: 
       this.os_name = request.Browser.Platform + (ua.Contains("Mobile") ? " Mobile " : ""); 
      } 

      private void SetVersion(string userAgent, string device) 
      { 
       var temp = userAgent.Substring(userAgent.IndexOf(device) + device.Length).TrimStart(); 
       var version = string.Empty; 

       foreach (var character in temp) 
       { 
        var validCharacter = false; 
        int test = 0; 

        if (Int32.TryParse(character.ToString(), out test)) 
        { 
         version += character; 
         validCharacter = true; 
        } 

        if (character == '.' || character == '_') 
        { 
         version += '.'; 
         validCharacter = true; 
        } 

        if (validCharacter == false) 
         break; 
       } 
       this.os_version = version; 
      } 
     } 

使用

var os = new OS(); 
os.os_name; // here is os name 
os.os_version; // here is os vers 
相关问题