Skip to content
  • English Deutsch

benjamin-abt.com

Performance is a feature | Cloud - AI - Azure - .NET - Sustainability

  • Home
  • Blog
  • Portfolio
    • Overview
    • Workshops & Training
    • Publications
  • Contact
  • Home
  • Blog

Parse HTTP User Agents with ASP.NET Core

Parse HTTP User Agents with ASP.NET Core
June 18, 2021 2 min read BEN ABT
.NET ASP.NET Core

This article was written in 2021. Versions, tools and recommendations may have changed since then. Newer posts are collected under .NET.

In my last blog post I shared why we wrote our own user agent parser (mycsharp/HttpUserAgentParser ) in .NET (reason: performance).

This is now about the correct usage of such a parser.

Static Parsing

The library has a static interface for parsing user agent strings.

C#
1string userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36";
2HttpUserAgentInformation info = HttpUserAgentParser.Parse(userAgent);

The info object created from this now contains all relevant information of the agent:

JavaScript
1UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36"
2Type = HttpUserAgentType.Browser
3Platform = {
4    Name = "Windows 10",
5    PlatformType = HttpUserAgentPlatformType.Windows
6}
7Name = "Chrome"
8Version = "90.0.4430.212"
9MobileDeviceType = null

Dependency Injection

For integration via Dependency Injection in ASP.NET Core, package MyCSharp.HttpUserAgentParser.AspNetCore must also be installed. Afterwards, the User Agent Parser can be registered as a service.

C#
1public void ConfigureServices(IServiceCollection services)
2{
3    services.AddHttpUserAgentParser(); // uses HttpUserAgentParserDefaultProvider and does not cache
4}

The registration contains the IHttpUserAgentParserProvider interface, which can now be used for parsing.

C#
 1public class MyClass
 2{
 3    private IHttpUserAgentParserProvider _parserProvider;
 4    public MyClass(IHttpUserAgentParserProvider parserProvider)
 5    {
 6        _parserProvider = parserProvider;
 7    }
 8}
 9
10    public void MyMethod(string userAgent)
11    {
12        HttpUserAgentInformation info = _parserProvider.Parse(userAgent);
13    }

The default implementation of the registry only registers the parser (HttpUserAgentParserDefaultProvider), without any caching integration or other capabilities.

Caching

Caching is enabled by implementing your own implementation of IHttpUserAgentParserProvider or using the built-in caching functionality.

On the one hand we offer an implementation via a ConcurrentDictionary:

C#
1public void ConfigureServices(IServiceCollection services)
2{
3    services.AddHttpUserAgentCachedParser(); // uses `HttpUserAgentParserCachedProvider`
4    // or
5    // services.AddHttpUserAgentParser<YourHttpUserAgentParserCachedProvider>();
6}

On the other hand, we also offer a caching implementation based on MemoryCache, for which there is an extra NuGet package: MyCSharp.HttpUserAgentParser.MemoryCache

C#
 1public void ConfigureServices(IServiceCollection services)
 2{
 3    services.AddHttpUserAgentMemoryCachedParser();
 4
 5    // or use options
 6
 7    services.AddHttpUserAgentMemoryCachedParser(options =>
 8    {
 9        options.CacheEntryOptions.SlidingExpiration = TimeSpan.FromMinutes(60); // default is 1 day
10
11        // limit the total entries in the MemoryCache
12        //   each unique user agent string counts as one entry
13        options.CacheOptions.SizeLimit = 1024; // default is null (= no limit)
14    });
15}

After that, it is also easy to use the IHttpUserAgentParserProvider interface to use the parser.

All caching mechanisms store the full user agent string as a key and use a one-time parsing result as the result, which is then always returned. This guarantees that each user agent string only has to go through our parsing mechanism once.

Related articles

Production Rate Limiting in ASP.NET Core

Nov 3, 2026 - 16 min read

Production Rate Limiting in ASP.NET Core

Most ASP.NET Core rate-limiting examples stop after registering a fixed window. That is enough to demonstrate the middleware, but it is not …

Problem Details in ASP.NET Core and .NET 11

Oct 5, 2026 - 12 min read

Problem Details in ASP.NET Core and .NET 11

builder.Services.AddProblemDetails() registers infrastructure. It does not create an error contract. An ASP.NET Core request can fail during …

Resilience in .NET with Microsoft.Extensions.Resilience

Sep 21, 2026 - 11 min read

Resilience in .NET with Microsoft.Extensions.Resilience

Retries are the most visible resilience strategy and the easiest one to misuse. Repeating a failing call can recover from a short network …


BEN ABT

BEN ABT

Ben is Chief Engineering Officer and Principal Software Engineer at Medialesson.de, with a strong focus on .NET and Microsoft Azure. In his professional role, he designs and builds highly scalable platforms for hybrid and cloud-based scenarios. He also advises companies and C-levels on migrating to the sovereign cloud.

He runs myCSharp.de, the largest and most active German-speaking C# community forum, actively contributes to both his own and external open-source projects and regularly shares his knowledge through his blog, at community events, conferences and as a writer for technical magazines.

Let's Work Together

Looking for an experienced Platform Architect or Engineer for your next project? Whether it's cloud migration, platform modernization or building new solutions from scratch - I'm here to help you succeed.

New Platforms

Modernization

Training & Consulting

Contact Me
X Facebook LinkedIn WhatsApp
Back Next Blog Post

Open for work!

Contact Me
BEN ABT BEN ABT Software Engineer · Microsoft MVP Passionate about .NET, Azure, performance optimization and sustainable software engineering.

Medialesson GmbH Logo

Microsoft MVP Logo

Recent posts

View all
Production Rate Limiting in ASP.NET Core

Production Rate Limiting in ASP.NET Core

Nov 3, 2026
The Big Decimal Bugfix with .NET 11

The Big Decimal Bugfix with .NET 11

Nov 2, 2026
Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Oct 27, 2026
Stop Using nginx as Your Default Static File Server in Docker

Stop Using nginx as Your Default Static File Server in …

Oct 24, 2026
Containerizing .NET in 2026 Without a Dockerfile

Containerizing .NET in 2026 Without a Dockerfile

Oct 20, 2026

Tags

.NET (195) Csharp (99) ASP.NET Core (49) Azure (41) DevOps (17) Docker (15) Microsoft (14) EF Core (11) Microsoft MVP (11) Security (11) AI (10) GitHub (10) GitHub Copilot (10) ASP.NET (9) Synology (9) .NET 11 (8) Architecture (8) Raspberry (8) Azure Functions (7) Cloudflare (7)
benjamin-abt.com

About BEN

Benjamin Abt is a Software Engineer, Platform Architect and Microsoft MVP specializing in .NET, Azure, cloud architecture, performance optimization and sustainable software development.

Connect

  • Follow me on GitHub
  • Follow me on LinkedIn
  • Follow me on Mastodon
  • Microsoft MVP profile
  • Follow SchwabenCode on GitHub
  • Follow me on X

Quick Links

  • Portfolio
  • Blog
  • Workshops & Training
  • Publications
  • Get in Touch
  • Sitemap
Open for Work

© Copyright Benjamin Abt & SchwabenCode | 2011 - 2026

Content licensed under CC BY 4.0 unless otherwise noted.

  • Impressum
  • Privacy
  • Contact