吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1626|回复: 2
收起左侧

[其他转载] C#获取用户客户端系统版本设备名称浏览器

[复制链接]
迷恋自留地 发表于 2022-4-22 10:58

C#获取用户客户端系统版本设备名称浏览器

先看效果

使用

Neget引用包UAParser


在这里插入代码片

项目的github :https://github.com/ua-parser/uap-csharp


  using UAParser;

...

  string uaString = "Mozilla/5.0 (iPhone; CPU iPhone OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3";

  // get a parser with the embedded regex patterns

  var uaParser = Parser.GetDefault();

  // get a parser using externally supplied yaml definitions

  // var uaParser = Parser.FromYaml(yamlString);

  ClientInfo c = uaParser.Parse(uaString);

  Console.WriteLine(c.UA.Family); // => "Mobile Safari"

  Console.WriteLine(c.UA.Major);  // => "5"

  Console.WriteLine(c.UA.Minor);  // => "1"

  Console.WriteLine(c.OS.Family);        // => "iOS"

  Console.WriteLine(c.OS.Major);         // => "5"

  Console.WriteLine(c.OS.Minor);         // => "1"

  Console.WriteLine(c.Device.Family);    // => "iPhone"

其他方法

C# 服务端获取客户端 系统/浏览器/IP

参考:https://www.cnblogs.com/Aaxuan/p/10301926.html

```csharp/// <summary>

    /// 获取客户端操作系统版本

    /// </summary>

    /// <returns></returns>

    public static string GetOSName()

    {

        if (HttpContext.Current == null || HttpContext.Current.Request == null || HttpContext.Current.Request.UserAgent == null)

        { return "未知"; }

        var userAgent = HttpContext.Current.Request.UserAgent.ToLower();

        if (userAgent.Contains("android"))

        { return "Android"; }

        else if (userAgent.Contains("mac os x"))

        { return "ios"; }

        else if (userAgent.Contains("windows phone"))

        { return "Windows Phone"; }

        else if (userAgent.Contains("nt 10.0"))

        { return "Windows 10"; }

        else if (userAgent.Contains("NT 6.3"))

        { return "Windows8.1"; }

        else if (userAgent.Contains("NT 6.2"))

        { return "Windows8"; }

        else if (userAgent.Contains("nt 6.1"))

        { return "Windows 7"; }

        else if (userAgent.Contains("nt 6.0"))

        { return "Windows Vista/Server 2008"; }

        else if (userAgent.Contains("nt 5.2"))

        { return "Windows Server 2003"; }

        else if (userAgent.Contains("nt 5.1"))

        { return "Windows XP"; }

        else if (userAgent.Contains("nt 5"))

        { return "Windows 2000"; }

        else if (userAgent.Contains("nt 4"))

        { return "Windows NT4"; }

        else if (userAgent.Contains("me"))

        { return "Windows Me"; }

        else if (userAgent.Contains("98"))

        { return "Windows 98"; }

        else if (userAgent.Contains("95"))

        { return "Windows 95"; }

        else if (userAgent.Contains("mac"))

        { return "Mac"; }

        else if (userAgent.Contains("unix"))

        { return "UNIX"; }

        else if (userAgent.Contains("linux"))

        { return "Linux"; }

        else if (userAgent.Contains("sunos"))

        { return "SunOS"; }

        return "未知";

    }

    /// <summary>

    /// 获取浏览器名称

    /// </summary>

    /// <returns></returns>

    public static string GetBrowser()

    {

        if (HttpContext.Current == null || HttpContext.Current.Request == null || HttpContext.Current.Request.UserAgent == null)

        { return "未知"; }

        var userAgent = HttpContext.Current.Request.UserAgent.ToLower();

        if (userAgent.Contains("opera/ucweb"))

        { return "UC Opera"; }

        else if (userAgent.Contains("openwave/ ucweb"))

        { return "UCOpenwave"; }

        else if (userAgent.Contains("ucweb"))

        { return "UC"; }

        else if (userAgent.Contains("360se"))

        { return "360"; }

        else if (userAgent.Contains("metasr"))

        { return "搜狗"; }

        else if (userAgent.Contains("maxthon"))

        { return "遨游"; }

        else if (userAgent.Contains("the world"))

        { return "世界之窗"; }

        else if (userAgent.Contains("tencenttraveler") || userAgent.Contains("qqbrowser"))

        { return "腾讯"; }

        else if (userAgent.Contains("chrome"))

        { return "Chrome"; }

        else if (userAgent.Contains("safari"))

        { return "safari"; }

        else if (userAgent.Contains("firefox"))

        { return "Firefox"; }

        else if (userAgent.Contains("opera"))

        { return "Opera"; }

        else if (userAgent.Contains("msie"))

        { return "IE"; }

        else

        { return System.Web.HttpContext.Current.Request.Browser.Browser; }

    }

    /// <summary>

    /// 取得客户端IP

    /// </summary>

    /// <returns></returns>

    public static string GetClientIp()

    {

        var result = "0.0.0.0";

        if (HttpContext.Current == null && HttpContext.Current.Request == null)

        { return result; }

        result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

        if (result == null || result == String.Empty)

        {

            result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

        }

        if (result == null || result == String.Empty)

        {

            result = HttpContext.Current.Request.UserHostAddress;

        }

        if (result == null || result == String.Empty)

        {

            result = "0.0.0.0";

        }

        return result;

    }


https://www.cnblogs.com/netcore5/p/16177948.html

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

aizongke 发表于 2022-4-22 11:17
好,收藏
nihao3312 发表于 2022-4-22 13:42
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 07:49

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表