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

C#asp.net Coreasp.net Core-Mvc

C# Problem Overview


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

C# Solutions


Solution 1 - C#

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

Solution 2 - C#

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

Installed ua-parse.

In controller using UAParser;

var userAgent = HttpContext.Request.Headers["User-Agent"];
var uaParser = Parser.GetDefault();
ClientInfo c = uaParser.Parse(userAgent);

after using above code was able to get browser details from userAgent by using c.UA.Family + " " + c.UA.Major +"." + c.UA.Minor You can also get OS details like c.OS.Family;

Where c.UA.Major is a browser major version and c.UA.Minor is a browser minor version.

Solution 3 - C#

Solution 4 - C#

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());
        }
    }
}

Solution 5 - C#

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();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioneadamView Question on Stackoverflow
Solution 1 - C#eadamView Answer on Stackoverflow
Solution 2 - C#Aneeq Azam KhanView Answer on Stackoverflow
Solution 3 - C#UzayView Answer on Stackoverflow
Solution 4 - C#Sarin Na WangkanaiView Answer on Stackoverflow
Solution 5 - C#Stefano BalzarottiView Answer on Stackoverflow