51Degrees Device Detection .NET  4.3

Device detection services for 51Degrees Pipeline


This example is available in full on GitHub.

This example requires a local data file. Free data files can be acquired by pulling the submodules under this repository or from the device-detection-data GitHub repository.

Required NuGet Dependencies:

Expected output:

This example demonstrates detection using user-agent client hints. The sec-ch-ua value can be used to determine the browser of the connecting device, but not other components such as the hardware. We show this by first performing detection with sec-ch-ua only. We then repeat with the user-agent header only.

Finally, we use both sec-ch-ua and user-agent.Note that sec-ch-ua takes priority over the user-agent for detection of the browser.

Sec-CH-UA = '"Google Chrome";v="89", "Chromium";v="89", ";Not A Brand";v="99"' User-Agent = 'NOT_SET' Browser = Chrome 89 IsMobile = No matching profiles could be found for the supplied evidence.A 'best guess' can be returned by configuring more lenient matching rules.See https://51degrees.com/documentation/_device_detection__features__false_positive_control.html

Sec-CH-UA = 'NOT_SET' User-Agent = 'Mozilla/5.0 (Linux; Android 9; SAMSUNG SM-G960U) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/10.1 Chrome/71.0.3578.99 Mobile Safari/537.36' Browser = Samsung Browser 10.1 IsMobile = True

Sec-CH-UA = '"Google Chrome";v="89", "Chromium";v="89", ";Not A Brand";v="99"' User-Agent = 'Mozilla/5.0 (Linux; Android 9; SAMSUNG SM-G960U) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/10.1 Chrome/71.0.3578.99 Mobile Safari/537.36' Browser = Chrome 89 IsMobile = True Complete.Press key to exit.

/* *********************************************************************
* This Original Work is copyright of 51 Degrees Mobile Experts Limited.
* Copyright 2019 51 Degrees Mobile Experts Limited, 5 Charlotte Close,
* Caversham, Reading, Berkshire, United Kingdom RG4 7BY.
* This Original Work is licensed under the European Union Public Licence (EUPL)
* v.1.2 and is subject to its terms as set out below.
* If a copy of the EUPL was not distributed with this file, You can obtain
* one at https://opensource.org/licenses/EUPL-1.2.
* The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
* amended by the European Commission) shall be deemed incompatible for
* the purposes of the Work and the provisions of the compatibility
* clause in Article 5 of the EUPL shall not apply.
* If using the Work as, or as part of, a network application, by
* including the attribution notice(s) required under Article 5 of the EUPL
* in the end user terms of the application under an appropriate heading,
* such notice(s) shall fulfill the requirements of that article.
* ********************************************************************* */
using FiftyOne.Pipeline.Core.FlowElements;
using FiftyOne.Pipeline.Engines;
using System;
using System.IO;
public class Program
public class Example : ExampleBase
private static string mobileUserAgent =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) " +
"AppleWebKit/537.36 (KHTML, like Gecko) " +
"Chrome/95.0.4638.69 Safari/537.36 Edg/95.0.1020.44";
private static string secchuaValue =
"\"Google Chrome\";v=\"89\", \"Chromium\";v=\"89\", \";Not A Brand\";v=\"99\"";
private static string secchPlatformValue = "Windows";
// A platform version field >= 13 indicates Windows 11.
// https://docs.microsoft.com/en-us/microsoft-edge/web-platform/how-to-detect-win11
private static string secchPlatformVersionValue = "14.0.0";
public void Run(string dataFile)
FileInfo f = new FileInfo(dataFile);
Console.WriteLine($"Using data file at '{f.FullName}'");
Console.WriteLine($"This example demonstrates detection " +
$"using user-agent client hints.");
Console.WriteLine($"The sec-ch-ua value can be used to " +
$"determine the browser of the connecting device, " +
$"but not other components such as the hardware.");
Console.WriteLine($"Similarly, sec-ch-ua-platform and " +
$"platform-version value can be used to " +
$"determine the platform/operating system.");
Console.WriteLine($"We show this by first performing " +
$"detection with the sec-ch-ua fields only.");
Console.WriteLine($"We then repeat with the user-agent " +
$"header only.");
Console.WriteLine($"Finally, we use both sec-ch-ua fields " +
"and user-agent. Note that sec-ch-ua takes priority " +
"over the user-agent for detection of the browser and" +
"sec-ch-ua-platform and platform-version act similarly " +
"for the platform.");
// Use the DeviceDetectionPipelineBuilder to build a new Pipeline
// to use an on-premise Hash engine with the low memory
// performance profile.
using (var pipeline = new DeviceDetectionPipelineBuilder()
.UseOnPremise(dataFile, null, false)
// Prefer low memory profile where all data streamed
// from disk on-demand. Experiment with other profiles.
// first try with just sec-ch-ua.
AnalyseClientHints(pipeline, false, true);
// Now with just user-agent.
AnalyseClientHints(pipeline, true, false);
// Finally, perform detection with both.
AnalyseClientHints(pipeline, true, true);
private void AnalyseClientHints(IPipeline pipeline, bool setUserAgent, bool setSecChUa)
// Create the FlowData instance.
using (var data = pipeline.CreateFlowData())
// Add a value for the user-agent client hints header
// sec-ch-ua as evidence
if (setSecChUa)
data.AddEvidence(Pipeline.Core.Constants.EVIDENCE_QUERY_PREFIX +
Pipeline.Core.Constants.EVIDENCE_SEPERATOR + "sec-ch-ua", secchuaValue);
data.AddEvidence(Pipeline.Core.Constants.EVIDENCE_QUERY_PREFIX +
Pipeline.Core.Constants.EVIDENCE_SEPERATOR + "sec-ch-ua-platform", secchPlatformValue);
data.AddEvidence(Pipeline.Core.Constants.EVIDENCE_QUERY_PREFIX +
Pipeline.Core.Constants.EVIDENCE_SEPERATOR + "sec-ch-ua-platform-version", secchPlatformVersionValue);
// Also add a standard user-agent if requested
if (setUserAgent)
// Process the supplied evidence.
// Get device data from the flow data.
var device = data.Get<IDeviceData>();
var browserName = device.BrowserName;
var browserVersion = device.BrowserVersion;
var platformName = device.PlatformName;
var platformVersion = device.PlatformVersion;
var hardwareVendor = device.HardwareVendor;
var hardwareModel = device.HardwareModel;
var chua = setSecChUa ? secchuaValue : "NOT_SET";
var plat = setSecChUa ? secchPlatformValue : "NOT_SET";
var platV = setSecChUa ? secchPlatformVersionValue : "NOT_SET";
Console.WriteLine($"Sec-CH-UA = '{chua}'");
Console.WriteLine($"Sec-CH-UA-Platform = '{plat}'");
Console.WriteLine($"Sec-CH-UA-Platform-Version = '{platV}'");
var ua = setUserAgent ? mobileUserAgent : "NOT_SET";
Console.WriteLine($"User-Agent = '{ua}'");
// Output the Browser.
if (browserName.HasValue && browserVersion.HasValue)
Console.WriteLine($"\tBrowser = {browserName.Value} {browserVersion.Value}");
else if (browserName.HasValue)
Console.WriteLine($"\tBrowser = {browserName.Value} (version unknown)");
Console.WriteLine($"\tBrowser = {browserName.NoValueMessage}");
// Output the Platform
if (platformName.HasValue && platformVersion.HasValue)
Console.WriteLine($"\tPlatform = {platformName.Value} {platformVersion.Value}");
else if (platformName.HasValue)
Console.WriteLine($"\tPlatform = {platformName.Value} (version unknown)");
Console.WriteLine($"\tPlatform = {platformName.NoValueMessage}");
// Output the Hardware
if (hardwareVendor.HasValue && hardwareModel.HasValue)
Console.WriteLine($"\tDevice = {hardwareVendor.Value} {hardwareModel.Value}");
else if (hardwareVendor.HasValue)
Console.WriteLine($"\tDevice = {hardwareVendor.Value} (model unknown)");
Console.WriteLine($"\tDevice = {hardwareVendor.NoValueMessage}");
static void Main(string[] args)
var defaultDataFile = "..\\..\\..\\..\\..\\..\\..\\..\\device-detection-cxx\\device-detection-data\\51Degrees-LiteV4.1.hash";
var defaultDataFile = "..\\..\\..\\..\\..\\..\\..\\device-detection-cxx\\device-detection-data\\51Degrees-LiteV4.1.hash";
var dataFile = args.Length > 0 ? args[0] : defaultDataFile;
new Example().Run(dataFile);
#if (DEBUG)
Console.WriteLine("Complete. Press key to exit.");