如何获取客户端的浏览器信息?

How to get browser information of client?

How can I get browser information for client in asp.net core 3.0.1, I tried to use this code, but, it return me the full list of user's browsers, but, I need the browser that user using it.

我使用的代码:

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

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

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

我搜索了很多 link,但是,我没有找到我需要的东西,而且,这是我搜索的一些 link:

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

Is there any way to get the browser name and version that client run the application from it using Asp.Net Core 3.0.1?

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

Installation of detection library is now done with a single package reference point.

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;
        //...
    }
} 

您可以使用旨在提供可靠语义解析的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