【问题标题】:How to get browser information of client?如何获取客户端的浏览器信息?
【发布时间】:2020-02-12 23:17:16
【问题描述】:

如何在 asp.net core 3.0.1 中获取客户端的浏览器信息,我 尝试使用此代码,但是,它返回给我用户的完整列表 浏览器,但是,我需要用户使用它的浏览器。

我使用的代码:

var userAgent = Request.Headers["User-Agent"].ToString();

我也试过这段代码,但是,它给了我错误:

UserAgent.UserAgent ua = new UserAgent.UserAgent(userAgent);

我搜索了很多链接,但是,我没有找到我需要的,这是我搜索的一些链接:

  1. https://code.msdn.microsoft.com/How-to-get-OS-and-browser-c007dbf7
  2. How to get user Browser name ( user-agent ) in Asp.net Core?
  3. https://docs.microsoft.com/en-us/dotnet/api/system.web.httprequest.useragent?view=netframework-4.8
  4. https://www.c-sharpcorner.com/forums/how-to-get-current-browser-details-in-asp-net-core

有什么方法可以获取客户端运行应用程序的浏览器名称和版本 使用 Asp.Net Core 3.0.1?

【问题讨论】:

  • 出于好奇,你为什么需要这个?客户端可以轻松欺骗或删除User-Agent 值,因此它不是很依赖。另外,您从标头中获得的值是多少?
  • 不可靠没有。 “User-Agent”标头(以及所有其他请求标头)很容易被欺骗,一些浏览器默认这样做是出于安全/跟踪问题。就像弗雷德里克一样,我会问:你为什么需要这个?
  • 这不是“用户浏览器的完整列表”;实际发出请求的是浏览器发送的 UA。列出的其他“浏览器”是为了兼容性,很大程度上是因为人们在做你在这里所做的事情:试图嗅探浏览器并提供不同的体验或拒绝访问。所有浏览器都开始添加来自其他主要浏览器的令牌,因此没有简单的方法可以删除所有 Chrome 用户,例如。尽管如此,每个 UA 字符串对于特定的浏览器和版本都是唯一的。
  • 看看this link。您将获得几乎完整的浏览器信息。

标签: c# browser .net-core asp.net-core-3.0


【解决方案1】:

您可以安装Wangkanai.Detection 包。完整的文档可以在这里找到:https://github.com/wangkanai/Detection

检测库的安装现在使用单个软件包完成 参考点。

PM> install-package Wangkanai.Detection -pre

如果您只需要特定的解析器,仍然可以安装单独的包。

PM> install-package Wangkanai.Detection.Device -pre  
PM> install-package Wangkanai.Detection.Browser -pre  
PM> install-package Wangkanai.Detection.Engine -pre   //concept
PM> install-package Wangkanai.Detection.Platform -pre //concept
PM> install-package Wangkanai.Detection.Crawler -pre  

安装Responsive 库将引入所有依赖包(这将包括Wangkanai.Detection.Device)。

PM> install-package Wangkanai.Responsive -pre

我认为以下内容对你来说应该足够了:

install-package Wangkanai.Detection -pre 
install-package Wangkanai.Detection.Browser -pre

然后需要在ConfigureServices方法中添加检测服务来配置Startup.cs

public void ConfigureServices(IServiceCollection services)
{
   // Add detection services container and device resolver service.
    services.AddDetection();
    services.AddDetectionCore().AddBrowser();
    // Add framework services.
    services.AddMvc();
}

最后在您的Controller 中,执行以下操作:

public class HomeController : Controller
{
    private readonly IDetection _detection;

    public HomeController(IDetection detection)
    {
        _detection = detection;
    }

    public IActionResult Index()
    {
        string browser_information = _detection.Browser.Type.ToString() +
                                     _detection.Browser.Version;
        //...
    }
} 

【讨论】:

  • “检测”呢?它没有定义,我认为这将是此解决方案的关键要素。如果没有“检测”的定义,我不知道它是如何工作的。
  • @jdosser 检测由启动传递给控制器​​。这就是为什么它被添加到配置服务。就像您使用 ILogger 或 IConfiguration 时一样。这正是 MVC 在 .net core 3 中的工作方式
  • IDetection 缺少一个 using,是什么?
【解决方案2】:

您可以使用旨在提供可靠语义解析的this nuget package

为了做一个简单的例子,我将使用单例而不是依赖注入:

public static class YauaaSingleton
{
    private static UserAgentAnalyzer.UserAgentAnalyzerBuilder Builder { get; }

    private static readonly Lazy<UserAgentAnalyzer> analyzer = new Lazy<UserAgentAnalyzer> (() => Builder.Build());

    public static UserAgentAnalyzer Analyzer
    {
        get
        {
            return analyzer.Value;
        }
    }

    static YauaaSingleton()
    {
        Builder = UserAgentAnalyzer.NewBuilder();
        Builder.DropTests();
        Builder.DelayInitialization();
        Builder.WithCache(100);
        Builder.HideMatcherLoadStats();
        Builder.WithAllFields();
    }
}

然后在你的控制器中:

var userAgentString  = this.HttpContext?.Request?.Headers?.FirstOrDefault(s => s.Key.ToLower() == "user-agent").Value;
var ua = YauaaSingleton.Analyzer.Parse(userAgentString);
var browserNameField = ua.Get(DefaultUserAgentFields.AGENT_NAME);
var browserVersionField = ua.Get(DefaultUserAgentFields.AGENT_VERSION);
Debug.WriteLine(browserNameField.GetValue());
Debug.WriteLine(browserVersionField.GetValue());

对于工作示例,您可以找到完整的 asp.net 核心项目here

【讨论】:

    猜你喜欢
    • 2020-01-18
    • 2010-11-22
    • 2016-09-14
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    • 2011-11-19
    • 1970-01-01
    • 2013-02-14
    相关资源
    最近更新 更多