3

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.

Code that I used:

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

I tried also this code, but, it gives me error:

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

I searched in many links, but, I didn't find what I need, and, this is some of the link that I searched in:

  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://learn.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

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?

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
ali
  • 175
  • 1
  • 4
  • 21
  • Out of curiosity, why do you need this? A client can easily spoof or remove the `User-Agent` value so it's not very reliant. Also, what is the value you get from the header? – fredrik Oct 16 '19 at 08:25
  • Not reliably no. The "User-Agent" header (and every other request header) can easily be spoofed, some browsers do this by default out of security/ tracking concerns. And like frederik I'll ask: Why do you need this? – MindSwipe Oct 16 '19 at 08:30
  • That's not a "full list of user's browsers"; it's the UA sent by the browser actually making the request. The other "browsers" listed are for compatibility, pretty much because of people doing exactly what you are here: trying to sniff the browser and provide different experiences or deny access. All the browsers started adding tokens from the other major browsers so there's no easy way to just cut out all Chrome users, for example. Still, each UA string is unique to a particular browser and version. – Chris Pratt Oct 16 '19 at 13:43
  • Have a look at [this link](https://stackoverflow.com/questions/28664770/how-to-get-user-browser-name-user-agent-in-asp-net-core/52609204#52609204) . you will get almost full browser info. – Aneeq Azam Khan Nov 11 '20 at 10:41

2 Answers2

4

You can install Wangkanai.Detection package. The full documentation could be found here: https://github.com/wangkanai/Detection

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

PM> install-package Wangkanai.Detection -pre

While it is still possible to install the individual package if you just need that specific resolver.

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  

Installation of Responsive library will bring in all dependency packages (This will include Wangkanai.Detection.Device).

PM> install-package Wangkanai.Responsive -pre

I think the following should be enough for you:

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

Then you need to configure the Startup.cs by adding the detection service in the ConfigureServices method:

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

And finally in your Controller, do something like this:

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;
        //...
    }
} 
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • What about 'detection'? It's not defined,which I think would be a key element of this solution. Without a definition of 'detection' I'm not getting how this works. – jdosser Apr 20 '20 at 17:26
  • @jdosser detection is passed to the controller by the startup. that's why its added to configure services. its just like when you use ILogger or IConfiguration. Its just how MVC works in .net core 3 – SGP Jul 18 '20 at 23:30
  • IDetection is missing a using, which is it? – user33276346 Feb 03 '21 at 15:29
  • Did anyone figure out what the missing '@using' is? – Cef May 05 '22 at 19:19
  • if anybody need the using, is `Wangkanai.Detection.Services.IDetectionService` – L. Ronquillo May 09 '23 at 00:36
0

You can use this nuget package that aims to provide a reliable semantic parsing.

To make a simple example I will use a singleton instead of dependency injection:

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

Then in your controller:

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

For a working sample, you can find a complete asp.net core project here

Stefano Balzarotti
  • 1,760
  • 1
  • 18
  • 35