UaDetector 1.2.0

Prefix Reserved
dotnet add package UaDetector --version 1.2.0
                    
NuGet\Install-Package UaDetector -Version 1.2.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="1.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="UaDetector" Version="1.2.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 1.2.0
                    
#r "nuget: UaDetector, 1.2.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.
#addin nuget:?package=UaDetector&version=1.2.0
                    
Install UaDetector as a Cake Addin
#tool nuget:?package=UaDetector&version=1.2.0
                    
Install UaDetector 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.

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 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 53 5/9/2025
1.1.0 110 5/7/2025
1.0.2 129 5/5/2025
1.0.1 56 5/3/2025
1.0.0 132 5/1/2025
0.1.4 133 4/29/2025
0.1.3 142 4/29/2025
0.1.2 137 4/28/2025
0.1.1 142 4/28/2025
0.1.0 134 4/28/2025