UaDetector 3.1.1
Prefix Reserveddotnet add package UaDetector --version 3.1.1
NuGet\Install-Package UaDetector -Version 3.1.1
<PackageReference Include="UaDetector" Version="3.1.1" />
<PackageVersion Include="UaDetector" Version="3.1.1" />
<PackageReference Include="UaDetector" />
paket add UaDetector --version 3.1.1
#r "nuget: UaDetector, 3.1.1"
#:package UaDetector@3.1.1
#addin nuget:?package=UaDetector&version=3.1.1
#tool nuget:?package=UaDetector&version=3.1.1
UaDetector
A powerful user agent parser inspired by device-detector
UaDetector is a user agent parser that identifies devices (desktops, tablets, mobiles, TVs, cars, consoles),
clients (feed readers, media players, mobile apps), browsers, operating systems, brands, and bots.
It consists of several independent sub-parsers (OsParser, BrowserParser, ClientParser, and BotParser)
that can be used separately when only specific information is needed.
Packages
| Package | Description |
|---|---|
| UaDetector | User agent parser optimized for speed |
| UaDetector.Lite | Memory-optimized variant with slower parsing speed |
| UaDetector.Abstractions | Shared models, enums, and constants |
| UaDetector.MemoryCache | Memory cache built on Microsoft.Extensions.Caching.Memory |
Features
- Thread-safe: Parsers are stateless, making them safe for dependency injection and multithreaded scenarios
- Fast: Uses compiled regular expressions and frozen dictionaries for faster pattern matching and lookup operations
- Rich metadata: Static classes provide access to common values:
OsNames,OsFamilies,CpuArchitectures,BrowserNames,BrowserFamilies,BrowserEngines,BrandNames - Enum support: Values such as
OsCode,BrowserCode,BrandCode,ClientType,DeviceType, andBotCategoryare enums, making them suitable for database storage - Try-Parse Pattern: Parsers implement the Try-Parse Pattern, returning a bool to indicate success and assigning the result to an out parameter
Requirements
- .NET 9 SDK or newer for compilation. Projects can target earlier .NET versions.
- Visual Studio 2022 (version 17.12 or later)
- JetBrains Rider (version 2024.3 or later)
⚙️ 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();
Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
VersionTruncation |
enum |
Minor |
Controls how version numbers are shortened (e.g., None, Major, Minor, Patch, Build). |
DisableBotDetection |
bool |
false |
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.
Avoid directly instantiating parsers. The first call to TryParse causes a noticeable delay due to the creation of regular expression objects. To prevent this one-time cost during runtime, register the service with dependency injection, as shown earlier.
[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");
}
🗂️ Registry Access
Static registry classes offer bidirectional lookups for converting between enum codes and their corresponding string names.
The BrowserRegistry, OsRegistry, and BrandRegistry classes provide type-safe access to predefined values.
// Get browser name from enum code
string browserName = BrowserRegistry.GetBrowserName(BrowserCode.Safari);
// Returns: "Safari"
// Try to get browser code from name (case-insensitive)
if (BrowserRegistry.TryGetBrowserCode("Safari", out var browserCode))
{
Console.WriteLine($"Browser Code: {browserCode}"); // Output: Browser Code: Safari
}
else
{
Console.WriteLine("Browser not found");
}
💾 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();
});
Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
MaxKeyLength |
int |
256 |
Maximum length allowed for a cache key. Entries with longer keys will not be cached. |
Expiration |
TimeSpan? |
null |
Entries will expire after this duration, regardless of how frequently they are accessed. |
SlidingExpiration |
TimeSpan? |
null |
Entries will expire if they haven't been accessed within this time period. The expiration timer resets each time the entry is accessed. |
ExpirationScanFrequency |
TimeSpan |
1 minute |
Interval between automatic scans that evict expired cache entries. |
MaxEntries |
long? |
null |
Maximum number of entries allowed in the cache. When the limit is reached, least recently used entries will be evicted. |
EvictionPercentage |
double |
0.05 |
Percentage of cache entries to evict when MaxEntries limit is reached. Eviction runs asynchronously. When the cache is full, new entries will not be cached until eviction completes. |
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 is compatible. 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.6)
- UaDetector.Abstractions (>= 3.1.1)
-
.NETStandard 2.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- System.Collections.Immutable (>= 8.0.0)
- System.Text.Json (>= 8.0.6)
- UaDetector.Abstractions (>= 3.1.1)
-
net10.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.0)
- UaDetector.Abstractions (>= 3.1.1)
-
net8.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- UaDetector.Abstractions (>= 3.1.1)
-
net9.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.10)
- UaDetector.Abstractions (>= 3.1.1)
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.
| Version | Downloads | Last Updated |
|---|---|---|
| 3.1.1 | 161 | 11/27/2025 |
| 3.1.0 | 362 | 11/16/2025 |
| 3.0.1 | 249 | 11/14/2025 |
| 3.0.0 | 270 | 11/14/2025 |
| 2.0.1 | 707 | 8/21/2025 |
| 2.0.0 | 247 | 7/3/2025 |
| 1.2.2 | 1,185 | 6/14/2025 |
| 1.2.1 | 421 | 5/23/2025 |
| 1.2.0 | 185 | 5/9/2025 |
| 1.1.0 | 239 | 5/7/2025 |
| 1.0.2 | 254 | 5/5/2025 |
| 1.0.1 | 117 | 5/3/2025 |
| 1.0.0 | 180 | 5/1/2025 |
| 0.1.4 | 220 | 4/29/2025 |
| 0.1.3 | 197 | 4/29/2025 |
| 0.1.2 | 205 | 4/28/2025 |
| 0.1.1 | 218 | 4/28/2025 |
| 0.1.0 | 187 | 4/28/2025 |