UaDetector 3.1.0

Prefix Reserved
dotnet add package UaDetector --version 3.1.0
                    
NuGet\Install-Package UaDetector -Version 3.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="UaDetector" Version="3.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="UaDetector" Version="3.1.0" />
                    
Directory.Packages.props
<PackageReference Include="UaDetector" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add UaDetector --version 3.1.0
                    
#r "nuget: UaDetector, 3.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package UaDetector@3.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=UaDetector&version=3.1.0
                    
Install as a Cake Addin
#tool nuget:?package=UaDetector&version=3.1.0
                    
Install as a Cake Tool

UaDetector

A powerful user-agent parsing library inspired by device-detector

Build License

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.

Packages

Package Description
UaDetector High-performance user agent parser
UaDetector.Lite Memory-efficient variant with slower parsing
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, and BotCategory are 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 Default Description
MaxKeyLength 256 Maximum length allowed for a cache key. Entries with longer keys will not be cached.
Expiration null Entries will expire after this duration, regardless of how frequently they are accessed.
SlidingExpiration 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 1 minute Interval between automatic scans that evict expired cache entries.
MaxEntries null Maximum number of entries allowed in the cache. When the limit is reached, least recently used entries will be evicted.
EvictionPercentage 0.05 Percentage of cache entries to evict when MaxEntries limit is reached. This setting only applies when MaxEntries is configured. Default is 0.05 (5%). Higher values free more space during eviction. Eviction runs asynchronously. When the cache is full, new entries are not added until eviction completes.

Note: For full documentation, visit the GitHub repository.

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.0 302 11/16/2025
3.0.1 245 11/14/2025
3.0.0 267 11/14/2025
2.0.1 671 8/21/2025
2.0.0 246 7/3/2025
1.2.2 1,149 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 116 5/3/2025
1.0.0 179 5/1/2025
0.1.4 220 4/29/2025
0.1.3 195 4/29/2025
0.1.2 205 4/28/2025
0.1.1 216 4/28/2025
0.1.0 187 4/28/2025