This is the ASP.NET Core implementation of ua-parser.
The implementation uses the shared regex patterns and overrides from regexes.yaml (found in uap-core). The assembly embeds the latest regex patterns (enabled through a node module) which are loaded into the default parser. You can create a parser with more updated regex patterns by using the static methods on Parser to pass in specific patterns in yaml format.
You can then build and run the tests by
> dotnet restore UAParser.Core.sln> dotnet test UAParser.Core.slnTo pull the latest regexes into the project:
> npm install
> npm run updateStep 1: Install the UAParser.Core nuget package
> Install-Package UAParser.CoreStep 2: Enable the browser detection service inside the ConfigureServices method of Startup.cs.
public void ConfigureServices(IServiceCollection services)
{
// Add user agent service
services.AddUserAgentParser();
services.AddMvc();
}Step 3: Inject IUserAgentParser to your controller class or view file or middleware and access the ClientInfo property.
Example usage in controller code
public class HomeController : Controller
{
private readonly IUserAgentParser userAgentParser;
public HomeController(IUserAgentParser parser)
{
this.userAgentParser = parser;
}
public IActionResult Index()
{
var clientInfo = this.userAgentParser.ClientInfo;
// Use ClientInfo object as needed.
return View();
}
}Example usage in view code
@inject UAParser.Interfaces.IUserAgentParser parser
<h2> @parser.ClientInfo.Browser.Family </h2>
<h3> @parser.ClientInfo.Browser.Version </h3>
<h3> @parser.ClientInfo.OS.ToString() </h3>
<h3> @parser.ClientInfo.Device.ToString() </h3>Optional: detect Windows 11. Windows 11 browsers still send Windows NT 10.0 in the User-Agent string, so the
string alone reports Windows 10. Chromium based browsers (Edge, Chrome, Opera) can report the real version through the
Sec-CH-UA-Platform-Version client hint,
but only when the server asks for it. Add the middleware to request it:
app.UseUserAgentClientHints();This sends Accept-CH and Critical-CH response headers (pass critical: false to skip Critical-CH, then the hint is
only available from the second request on). When the hint reports Windows 11, ClientInfo.OS is Windows 11
(Major = "11"). Client hints are only sent over HTTPS; Firefox and Safari don't send them, so they keep reporting
Windows 10. If you cache responses that depend on the OS, add Sec-CH-UA-Platform-Version to the Vary header.
Example usage in custom middlware
You can inject the IUserAgentParser to the InvokeAsync method.
public class MyCustomMiddleware
{
private RequestDelegate next;
public MyCustomMiddleware(RequestDelegate next)
{
this.next = next;
}
public async Task InvokeAsync(HttpContext httpContext, IUserAgentParser parser)
{
var clientInfo = parser.ClientInfo;
if (clientInfo.Browser.Family == "Edge")
{
await httpContext.Response.WriteAsync("Have you tried the new chromuim based edge ?");
}
else
{
await this.next.Invoke(httpContext);
}
}
}- Søren Enemærke @sorenenemaerke / github
- Atif Aziz @raboof / github