UserAgents.Net 1.0.0.215

dotnet add package UserAgents.Net --version 1.0.0.215
                    
NuGet\Install-Package UserAgents.Net -Version 1.0.0.215
                    
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="UserAgents.Net" Version="1.0.0.215" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="UserAgents.Net" Version="1.0.0.215" />
                    
Directory.Packages.props
<PackageReference Include="UserAgents.Net" />
                    
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 UserAgents.Net --version 1.0.0.215
                    
#r "nuget: UserAgents.Net, 1.0.0.215"
                    
#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.
#:package UserAgents.Net@1.0.0.215
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=UserAgents.Net&version=1.0.0.215
                    
Install as a Cake Addin
#tool nuget:?package=UserAgents.Net&version=1.0.0.215
                    
Install as a Cake Tool

UserAgents

A .NET port of intoli/user-agents, a JavaScript library for generating random, realistic user agents.

Update Data, Build, Test, and Publish

NuGet Version

Overview

This library provides functionality to generate random, realistic user agents for web scraping and testing purposes. It's a direct port of the JavaScript library to .NET, maintaining the same functionality and data sources.

Installation

dotnet add package UserAgents.Net

Usage

Basic Usage

using UserAgents;

// Create a new instance of UserAgentSelector
var selector = new UserAgentSelector();

// Get a random user agent
var userAgent = selector.GetRandom();
if (userAgent != null)
{
    Console.WriteLine(userAgent.UserAgentString);
}

Using with HttpClient

using UserAgents;

var selector = new UserAgentSelector();
var userAgent = selector.GetRandom();

if (userAgent != null)
{
    using var httpClient = new HttpClient();
    httpClient.DefaultRequestHeaders.UserAgent.Clear();
    httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(userAgent.UserAgentString);
}

Filtering User Agents

using UserAgents;

var selector = new UserAgentSelector();

// Get an iPhone user agent
var iphoneFilter = new UserAgentFilter { Platform = "iPhone" };
var iphoneUserAgent = selector.GetRandom(iphoneFilter);
if (iphoneUserAgent != null)
{
    Console.WriteLine(iphoneUserAgent.UserAgentString);
}

// Get a desktop user agent with high resolution screen
var desktopFilter = new UserAgentFilter 
{ 
    MinScreenWidth = 1920,
    MinScreenHeight = 1080
};
var desktopUserAgent = selector.GetRandom(desktopFilter);
if (desktopUserAgent != null)
{
    Console.WriteLine(desktopUserAgent.UserAgentString);
}

// Get a mobile user agent with WiFi connection
var mobileWifiFilter = new UserAgentFilter 
{ 
    MaxScreenWidth = 768,
    ConnectionType = "wifi"
};
var mobileWifiUserAgent = selector.GetRandom(mobileWifiFilter);
if (mobileWifiUserAgent != null)
{
    Console.WriteLine(mobileWifiUserAgent.UserAgentString);
}

// Get a user agent matching a specific browser version using regex
var chromeFilter = new UserAgentFilter
{
    UserAgentPattern = @"Chrome/120\.0"
};
var chromeUserAgent = selector.GetRandom(chromeFilter);
if (chromeUserAgent != null)
{
    Console.WriteLine(chromeUserAgent.UserAgentString);
}

Getting Multiple Random User Agents

using UserAgents;

var selector = new UserAgentSelector();

// Get 5 random user agents
var randomUserAgents = selector.GetManyRandom(5).ToList();
foreach (var ua in randomUserAgents.Where(u => u != null))
{
    Console.WriteLine(ua.UserAgentString);
}

// Get 3 random Android user agents
var androidFilter = new UserAgentFilter { Platform = "Android" };
var androidUserAgents = selector.GetManyRandom(3, androidFilter).ToList();
foreach (var ua in androidUserAgents.Where(u => u != null))
{
    Console.WriteLine(ua.UserAgentString);
}

// Get 4 random user agents with complex criteria
var complexFilter = new UserAgentFilter 
{ 
    Platform = "Win32",
    EffectiveConnectionType = "4g",
    MinScreenWidth = 1920,
    MinScreenHeight = 1080
};
var complexUserAgents = selector.GetManyRandom(4, complexFilter).ToList();
foreach (var userAgent in complexUserAgents.Where(u => u != null))
{
    Console.WriteLine(userAgent.UserAgentString);
}

Getting All Matching User Agents

using UserAgents;

var selector = new UserAgentSelector();

// Get all user agents with 4K resolution
var highResFilter = new UserAgentFilter 
{ 
    MinScreenWidth = 3840,
    MinScreenHeight = 2160
};
var highResUserAgents = selector.GetAllMatching(highResFilter).ToList();
foreach (var ua in highResUserAgents)
{
    Console.WriteLine($"{ua.UserAgentString} ({ua.ScreenWidth}x{ua.ScreenHeight})");
}

// Get all mobile user agents with 5G connection
var mobile5GFilter = new UserAgentFilter 
{ 
    MaxScreenWidth = 768,
    EffectiveConnectionType = "5g"
};
var mobile5GUserAgents = selector.GetAllMatching(mobile5GFilter).ToList();
foreach (var ua in mobile5GUserAgents)
{
    Console.WriteLine($"{ua.UserAgentString} ({ua.Connection.EffectiveType})");
}

Ignoring Weights

By default, user agents are selected based on their weight distribution. To ignore weights and select completely randomly:

var selector = new UserAgentSelector();

// Get a completely random user agent, ignoring weights
var randomUserAgent = selector.GetRandom(ignoreWeights: true);
if (randomUserAgent != null)
{
    Console.WriteLine(randomUserAgent.UserAgentString);
}

// Get a random iPhone user agent, ignoring weights
var iphoneFilter = new UserAgentFilter { Platform = "iPhone" };
var randomIphoneUserAgent = selector.GetRandom(iphoneFilter, ignoreWeights: true);
if (randomIphoneUserAgent != null)
{
    Console.WriteLine(randomIphoneUserAgent.UserAgentString);
}

// Get multiple random user agents, ignoring weights
var randomUserAgents = selector.GetManyRandom(5, ignoreWeights: true).ToList();
foreach (var ua in randomUserAgents.Where(u => u != null))
{
    Console.WriteLine(ua.UserAgentString);
}

// Get multiple filtered user agents, ignoring weights
var androidUserAgents = selector.GetManyRandom(3, androidFilter, ignoreWeights: true).ToList();
foreach (var ua in androidUserAgents.Where(u => u != null))
{
    Console.WriteLine(ua.UserAgentString);
}

Project Structure

  • UserAgents/ - The main library project containing the core functionality
  • UserAgents.Console/ - Console project to demonstrate library's functionality
  • UserAgents.Tests/ - xUnit test project for ensuring the library's functionality

Requirements

  • .NET 9.0 or later (broader support will come later)
  • Visual Studio 2022 (recommended) or any other .NET IDE

Building the Project

dotnet build

Running Tests

dotnet test

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

Product Compatible and additional computed target framework versions.
.NET 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net9.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0.215 154 4/27/2026
1.0.0.204 109 4/11/2026
1.0.0.201 108 4/5/2026
1.0.0.198 103 3/31/2026
1.0.0.195 101 3/25/2026
1.0.0.193 97 3/21/2026
1.0.0.192 105 3/19/2026
1.0.0.191 98 3/18/2026
1.0.0.189 102 3/15/2026
1.0.0.167 243 2/7/2026
1.0.0.145 228 12/27/2025
1.0.0.99 191 10/3/2025
1.0.0.93 233 9/21/2025
1.0.0.92 291 9/19/2025
1.0.0.82 182 8/31/2025
1.0.0.79 255 8/26/2025
1.0.0.78 291 8/25/2025
1.0.0.77 114 8/23/2025
1.0.0.76 177 8/21/2025
1.0.0.75 178 8/19/2025
Loading failed