UaDetector 1.2.0
Prefix Reserveddotnet add package UaDetector --version 1.2.0
NuGet\Install-Package UaDetector -Version 1.2.0
<PackageReference Include="UaDetector" Version="1.2.0" />
<PackageVersion Include="UaDetector" Version="1.2.0" />
<PackageReference Include="UaDetector" />
paket add UaDetector --version 1.2.0
#r "nuget: UaDetector, 1.2.0"
#addin nuget:?package=UaDetector&version=1.2.0
#tool nuget:?package=UaDetector&version=1.2.0
UaDetector
A powerful user-agent parsing library inspired by device-detector.
UaDetector is a user-agent parser that identifies the browser, operating system, device, client, and even detects bots.
It is composed of several sub-parsers: OsParser
, BrowserParser
, ClientParser
, and BotParser
.
Each can be used independently if only certain information is needed from the user-agent string.
Key Features
- Thread Safety: The parsers are stateless by design, making them fully thread-safe and dependency-injection friendly.
- Optimized for Performance: Uses compiled regular expressions and frozen dictionaries for faster pattern matching and lookup operations.
- Predefined Values: Static classes provide access to browser, operating system, and other related metadata.
These include:
OsNames
,OsFamilies
,CpuArchitectures
,BrowserNames
,BrowserFamilies
,BrowserEngines
,BrandNames
. - Type-Safe Values: Certain values are represented by enums, making them suitable for database storage.
These include:
OsCode
,BrowserCode
,BrandCode
,ClientType
,DeviceType
,BotCategory
. - Try-Parse Pattern: Parsers implement the Try-Parse Pattern, returning a bool to indicate success and assigning the result to an out parameter.
⚙️ Configuration
To use UaDetector, register it in Program.cs with the AddUaDetector
method.
To use a sub-parser, register it using its dedicated method: AddOsParser
, AddBrowserParser
, AddClientParser
, or AddBotParser
.
All sub-parsers, except AddBotParser
, can be configured via UaDetectorOptions using the Options pattern as shown below.
using UaDetector;
builder.Services.AddUaDetector(options =>
{
// Custom configuration options
// e.g., options.VersionTruncation = VersionTruncation.Major;
});
Option | Type | Description |
---|---|---|
VersionTruncation |
enum |
Controls how version numbers are shortened (e.g., Major , Minor , None ) |
DisableBotDetection |
bool |
Disables bot detection entirely, skipping bot-related checks and parsing |
🚀 Quick Start
Each parser provides two TryParse
methods: one that accepts only the user-agent string and another
that accepts both the user-agent string and a collection of HTTP headers.
For more accurate detection, it is recommended to provide the HTTP headers.
Tip: Avoid directly instantiating parsers. The first initialization of UaDetector (or its sub-parsers) takes a few seconds (around 1-3s). To prevent this one-time cost during runtime, register the service with dependency injection, as shown earlier. This way, the instantiation will happen at application startup.
[ApiController]
public class UaDetectorController : ControllerBase
{
private readonly IUaDetector _uaDetector;
public UaDetectorController(IUaDetector uaDetector)
{
_uaDetector = uaDetector;
}
[HttpGet]
[Route("ua-detector")]
public IActionResult GetUserAgentInfo()
{
var userAgent = HttpContext.Request.Headers.UserAgent.ToString();
var headers = Request.Headers.ToDictionary(
h => h.Key,
h => h.Value.ToArray().FirstOrDefault()
);
if (_uaDetector.TryParse(userAgent, headers, out var result))
{
return Ok(result);
}
return BadRequest("Unrecognized user-agent");
}
}
The BotParser
class provides an additional IsBot
method to determine whether a user-agent string represents a bot.
using UaDetector.Parsers;
var botParser = new BotParser();
const string userAgent = "Mozilla/5.0 (compatible; Discordbot/2.0; +https://discordapp.com)";
if (botParser.IsBot(userAgent))
{
Console.WriteLine("Bot detected");
}
else
{
Console.WriteLine("No bot detected");
}
💾 Caching
To enable caching, install the UaDetector.MemoryCache package and configure it using the UseMemoryCache
extension method.
using UaDetector;
using UaDetector.MemoryCache;
builder.Services.AddUaDetector(options =>
{
options.UseMemoryCache();
});
Note: For full documentation, visit the GitHub repository.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 is compatible. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.6.2
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- System.Collections.Immutable (>= 8.0.0)
- System.Text.Json (>= 8.0.5)
-
.NETStandard 2.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- System.Collections.Immutable (>= 8.0.0)
- System.Text.Json (>= 8.0.5)
-
net8.0
-
net9.0
NuGet packages (1)
Showing the top 1 NuGet packages that depend on UaDetector:
Package | Downloads |
---|---|
UaDetector.MemoryCache
UaDetector thread-safe, in-memory cache support built on top of Microsoft.Extensions.Caching.Memory |
GitHub repositories
This package is not used by any popular GitHub repositories.