How to get user Browser name ( user-agent ) in Asp.net Core?

后端 未结 5 1933
攒了一身酷
攒了一身酷 2020-12-02 11:49

Can you please let me know how to get the browser\'s name that the client is using in MVC 6, ASP.NET 5?

相关标签:
5条回答
  • 2020-12-02 12:08

    I have developed a library to extend ASP.NET Core to support web client browser information detection at Wangkanai.Detection This should let you identity the browser name.

    namespace Wangkanai.Detection
    {
        /// <summary>
        /// Provides the APIs for query client access device.
        /// </summary>
        public class DetectionService : IDetectionService
        {
            public HttpContext Context { get; }
            public IUserAgent UserAgent { get; }
    
            public DetectionService(IServiceProvider services)
            {
                if (services == null) throw new ArgumentNullException(nameof(services));
    
                this.Context = services.GetRequiredService<IHttpContextAccessor>().HttpContext;
                this.UserAgent = CreateUserAgent(this.Context);
            }
    
            private IUserAgent CreateUserAgent(HttpContext context)
            {
                if (context == null) throw new ArgumentNullException(nameof(Context)); 
    
                return new UserAgent(Context.Request.Headers["User-Agent"].FirstOrDefault());
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-02 12:24

    For me Request.Headers["User-Agent"].ToString() did't help cuase returning all browsers names so found following solution.

    Installed ua-parse. In controller using UAParser;

    var userAgent = httpContext.Request.Headers["User-Agent"];
    string uaString = Convert.ToString(userAgent[0]);
    var uaParser = Parser.GetDefault();
    ClientInfo c = uaParser.Parse(uaString);
    

    after using above code was able to get browser details from userAgent by using c.UserAgent.Family You can alse get OS details like c.OS.Family;

    0 讨论(0)
  • 2020-12-02 12:27

    I think this was an easy one. Got the answer in Request.Headers["User-Agent"].ToString()

    Thanks

    0 讨论(0)
  • 2020-12-02 12:29

    Install this .nuget package

    create a class like this:

    public static class YauaaSingleton
        {
            private static UserAgentAnalyzer.UserAgentAnalyzerBuilder Builder { get; }
    
            private static UserAgentAnalyzer analyzer = null;
    
            public static UserAgentAnalyzer Analyzer
            {
                get
                {
                    if (analyzer == null)
                    {
                        analyzer = Builder.Build();
                    }
                    return analyzer;
                }
            }
    
            static YauaaSingleton()
            {
                Builder = UserAgentAnalyzer.NewBuilder();
                Builder.DropTests();
                Builder.DelayInitialization();
                Builder.WithCache(100);
                Builder.HideMatcherLoadStats();
                Builder.WithAllFields();
            }
    
    
        }
    

    in your controller you can read the user agent from http headers:

    string userAgent = Request.Headers?.FirstOrDefault(s => s.Key.ToLower() == "user-agent").Value;
    

    Then you can parse the user agent:

     var ua = YauaaSingleton.Analyzer.Parse(userAgent );
    
     var browserName = ua.Get(UserAgent.AGENT_NAME).GetValue();
    

    you can also get the confidence level (higher is better):

     var confidence = ua.Get(UserAgent.AGENT_NAME).GetConfidence();
    
    0 讨论(0)
  • 2020-12-02 12:31
    userAgent = Request.Headers["User-Agent"]; 
    

    https://code.msdn.microsoft.com/How-to-get-OS-and-browser-c007dbf7 (link not live) go for 4.8

    https://docs.microsoft.com/en-us/dotnet/api/system.web.httprequest.useragent?view=netframework-4.8

    0 讨论(0)
提交回复
热议问题