Can you please let me know how to get the browser's name that the client is using in MVC 6, ASP.NET 5?
-
[Browser detection using the user agent](https://developer.mozilla.org/en-US/docs/Web/HTTP/Browser_detection_using_the_user_agent) – Hans Kesting Mar 13 '23 at 11:12
8 Answers
I think this was an easy one. Got the answer in Request.Headers["User-Agent"].ToString()

- 10,970
- 6
- 59
- 64

- 23,151
- 18
- 48
- 71
-
15
-
5
-
12Beware this will result if a KeyNotFoundException if the request has no User-Agent! Be sure to use .ContainsKey first to check! – user169771 Nov 10 '16 at 20:29
-
12Scratch that. it just returns "" instead for some reason. Hooray for consistency... – user169771 Nov 10 '16 at 21:18
-
75Some may prefer `Request.Headers[HeaderNames.UserAgent]` as avoiding the string literal (may not have worked in Core 1.0, not sure) – Will Dean Jun 06 '18 at 08:35
-
4@WillDean this constant is in the Microsoft.Net.Http.Headers namespace. – Evgeni Nabokov Nov 07 '18 at 20:10
-
2this returned all names of browser but need only client browser details – Nitika Chopra Jun 25 '19 at 10:17
-
1I get `UAgent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/102.0.5005.63 Safari/537.36`, using Chrome. – Kiquenet Jun 01 '22 at 09:57
-
-
-
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.

- 15,928
- 4
- 31
- 48

- 992
- 1
- 10
- 23
-
7That is not a list of all browser names, that is what the browser is setting as the User-Agent. The UAParser knows how to take all those names and determine what the actual browser and OS it. – John C May 01 '19 at 20:22
-
-
1@Kiquenet, I'm using UAParser, information from this library was enough for me, works like a charm :D – Konsultan IT Bandung Jul 21 '22 at 03:43
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://learn.microsoft.com/en-us/dotnet/api/system.web.httprequest.useragent?view=netframework-4.8

- 809
- 9
- 18
-
That link is the best answer. The User-Agent string is a string that you have to decipher and parse to get version information. The classes supplied there do all the hard-work. – JustLooking Apr 10 '19 at 17:06
-
-
I have a web app and about 95% of users send back a user agent. However, the rest do not. These are not bots because the users are making paid bookings. Can anyone explain why user agent would be null? Most users are on mobile devices. Are there some browsers not returning ua? – Norbert Norbertson Mar 28 '22 at 10:39
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());
}
}
}

- 131
- 2
- 6
-
How does this work? I see you have a `DeviceResolver.cs` to find out whether it's a mobile, table or desktop, but I can't see similar code to extract details of the user agent header. – thoean Dec 21 '16 at 09:59
-
I have updated the repo and the readme is getting more mature. https://github.com/wangkanai/Detection – Sarin Na Wangkanai Feb 02 '18 at 09:16
-
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();

- 1,760
- 1
- 18
- 35
-
-
Sorry I didn't worked on that project by a while, I hope to resume it soon. If you found issues report them to github. – Stefano Balzarotti Aug 13 '22 at 10:10
In production application it is important to check first if user agent exists.
public static string GetUserAgent(this HttpContext context)
{
if (context.Request.Headers.TryGetValue(HeaderNames.UserAgent, out var userAgent))
{
return userAgent.ToString();
}
return "Not found";
}

- 1,973
- 3
- 19
- 40
var userAgent = $"{this.Request?.HttpContext?.Request?.Headers["user-agent"]}";

- 2,732
- 2
- 21
- 24
If you are using .net 6 or later it looks like there is a property you can use. So if you are in a controller you can access it like this:
var userAgent = HttpContext.Request.Headers.UserAgent;
Or if you are not in a controller you can inject an implmentation of IHttpContextAccessor and access for example like this
using Microsoft.AspNetCore.Http;
public class MyClass
{
public MyClass(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public string? GetUserAgent()
{
return _httpContextAccessor?.HttpContext?.Request.Headers.UserAgent;
}
}
Note in you will need to register IHttpContextAccessor by adding the following in your program.cs or startup.cs by adding this line of code
services.AddHttpContextAccessor();

- 2,045
- 2
- 16
- 32