UaDetector 0.1.4

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package UaDetector --version 0.1.4
                    
NuGet\Install-Package UaDetector -Version 0.1.4
                    
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="0.1.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="UaDetector" Version="0.1.4" />
                    
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 0.1.4
                    
#r "nuget: UaDetector, 0.1.4"
                    
#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.
#addin nuget:?package=UaDetector&version=0.1.4
                    
Install UaDetector as a Cake Addin
#tool nuget:?package=UaDetector&version=0.1.4
                    
Install UaDetector as a Cake Tool

UaDetector

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

Build License

UaDetector is a user-agent parsing library 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, OsPlatformTypes, 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 make use of the Try-Parse Pattern, returning a bool status and setting the out parameter to null on failure.

⚙️ 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 avoid 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("No matching user-agent information was found");
    }
}

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");
}

⚡ Benchmarks

The following benchmark compares the performance of other .NET user-agent parsing libraries.

Method Mean Error StdDev Ratio Allocated Alloc Ratio
UaDetector 1.844 ms 0.0229 ms 0.0388 ms 1.00 3.54 KB 1.00
DeviceDetector 6.084 ms 0.0346 ms 0.0307 ms 3.30 4534.51 KB 1,279.86
UaParser 6.783 ms 0.0844 ms 0.0789 ms 3.68 10794.88 KB 3,046.84

The following benchmark measures the performance of different parsers within the library.

Method Mean Error StdDev Allocated
UaDetector_TryParse 1,725.3 us 30.59 us 30.05 us 3627 B
BrowserParser_TryParse 1,266.7 us 24.91 us 66.06 us 1320 B
ClientParser_TryParse 170.2 us 3.35 us 3.59 us 1024 B
BotParser_TryParse 342.1 us 4.30 us 4.02 us 353 B
BotParser_IsBot 333.8 us 3.63 us 3.40 us -

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 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. 
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
1.2.0 93 5/9/2025
1.1.0 140 5/7/2025
1.0.2 150 5/5/2025
1.0.1 59 5/3/2025
1.0.0 134 5/1/2025
0.1.4 135 4/29/2025
0.1.3 144 4/29/2025
0.1.2 139 4/28/2025
0.1.1 144 4/28/2025
0.1.0 137 4/28/2025