Code Monkey home page Code Monkey logo

useragentservice's Introduction

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.

useragentservice's People

Contributors

dannyboyng avatar nathan-alden-sr avatar randyburden avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.