🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

Ng.UserAgentService

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

Ng.UserAgentService

A service to parse user-agent strings in C#.

3.0.0
Source
NuGet
Version published
Maintainers
1
Created
Source

UserAgentService

NuGet Version NuGet Download Count

A service to parse user-agent strings in C#. UserAgentService is extremely fast because it uses in-memory caching. UserAgentService only looks at the first 512 characters of the useragent string, it ignores the rest of the string. Most user-agent strings are within this limit but this limitation is introduced to protect itself from malicious, extremely long, hand crafted, user-agent strings.

Dependancies

Microsoft.Extensions.Caching.Memory
Microsoft.Extensions.Options

Installing

Install from Nuget

Install-Package Ng.UserAgentService

Usage

Console application

using Ng.Services;
...
//You do not have to specify any settings if you want to use the defaults
var settings = new UserAgentSettings
{
    CacheSizeLimit = 20000, //Default: 10000
    CacheSlidingExpiration = TimeSpan.FromDays(1), //Default: TimeSpan.FromDays(3)
    UaStringSizeLimit = 256, //Default: 512
};
var userAgentService = new UserAgentService(settings);

var ua = userAgentService.Parse("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0");

var isBrowser = ua.IsBrowser; // true
var isRobot = ua.IsRobot; // false
var isMobile = ua.IsMobile; // false

var platform = ua.Platform; // Windows 7
var bowser = ua.Browser; // Firefox
var version = ua.BrowserVersion; // 47.0
var mobile = ua.Mobile; // empty string
var robot = ua.Robot; // empty string

ASP.NET Core

Register service with dependency injection in Startup.cs

using Ng.Services;
...
public void ConfigureServices(IServiceCollection services)
{
    services.AddUserAgentService(); //Is equivalent to services.AddSingleton<IUserAgentService, UserAgentService>();

    or

    services.AddUserAgentService(options => {
        options.CacheSizeLimit = 20000; //Default: 10000
        options.CacheSlidingExpiration = TimeSpan.FromDays(1); //Default: TimeSpan.FromDays(3)
        options.UaStringSizeLimit = 256; //Default: 512
    });

    or
    
    services.AddSingleton<IUserAgentService, UserAgentService>(); //Is equivalent to services.AddUserAgentService();
}

Inject IUserAgentService into a Controller or wherever you like

using Ng.Services;
...
public class MyController
{
    public MyController(IUserAgentService userAgentService) // <-- Inject IUserAgentService here
    {
        string userAgentString = Request.Headers["User-Agent"].ToString();
        UserAgent ua = userAgentService.Parse(userAgentString);
    }
}

License

This project is licensed under the MIT License.

Contributions

Contributions are welcome.

Keywords

useragentparser

FAQs

Package last updated on 27 Mar 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts