https://github.com/w8tcha/uap-csharp
C# (ASP.NET CORE) implementation of ua-parser
https://github.com/w8tcha/uap-csharp
user-agent user-agent-parser useragent useragentparser
Last synced: 30 days ago
JSON representation
C# (ASP.NET CORE) implementation of ua-parser
- Host: GitHub
- URL: https://github.com/w8tcha/uap-csharp
- Owner: w8tcha
- License: other
- Fork: true (ua-parser/uap-csharp)
- Created: 2023-07-22T12:13:42.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2025-12-26T04:35:23.000Z (about 2 months ago)
- Last Synced: 2025-12-27T15:37:36.034Z (about 2 months ago)
- Topics: user-agent, user-agent-parser, useragent, useragentparser
- Language: C#
- Homepage:
- Size: 4.29 MB
- Stars: 5
- Watchers: 1
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
UserAgent Parser (based on ua_parser C# Library)
======================
This is the ASP.NET Core implementation of [ua-parser](https://github.com/tobie/ua-parser).
[](https://nuget.org/packages/UAParser.Core)
[](https://github.com/w8tcha/uap-csharp/actions/workflows/build.yml)
The implementation uses the shared regex patterns and overrides from regexes.yaml (found in [uap-core](https://github.com/ua-parser/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.
## Build and Run Tests
------
You can then build and run the tests by
```` cmd
> dotnet restore UAParser.Core.sln
````
```` cmd
> dotnet test UAParser.Core.sln
````
Update the embedded regexes
------
To pull the latest regexes into the project:
```` cmd
> npm install && grunt
````
## How to use ?
**Step 1:**
Install the [UAParser.Core nuget package](https://www.nuget.org/packages/UAParser.Core/)
```` cmd
> Install-Package UAParser.Core
````
**Step 2:** Enable the browser detection service inside the `ConfigureServices` method of `Startup.cs`.
```c#
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
```c#
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
```c#
@inject UAParser.Interfaces.IUserAgentParser parser
@parser.ClientInfo.Browser.Family
@parser.ClientInfo.Browser.Version
@parser.ClientInfo.OS.ToString()
@parser.ClientInfo.Device.ToString()
```
Example usage in custom middlware
You can inject the `IUserAgentParser` to the `InvokeAsync` method.
```c#
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);
}
}
}
```
Authors:
-------
* Søren Enemærke [@sorenenemaerke](https://twitter.com/sorenenemaerke) / [github](https://github.com/enemaerke)
* Atif Aziz [@raboof](https://twitter.com/raboof) / [github](https://github.com/atifaziz)