Home > Back-end >  C# ASPNET - How to detect browser type
C# ASPNET - How to detect browser type

Time:10-04

I'm inputting the following code for detect browser type (IE, FF, Chrome, Edge) used from users, using C# ASPNET.

The detected browser type working but I have verified that when the code is getting from the Microsoft Edge browser the output is

Browser Capabilities
Type = Chrome94
Name = Chrome
Version = 94.0
Major Version = 94
Minor Version = 0
Platform = WinNT
Is Beta = False
Is Crawler = False
Is AOL = False
Is Win16 = False
Is Win32 = True
Supports Frames = True
Supports Tables = True
Supports Cookies = True
Supports VBScript = False
Supports JavaScript = 3.0
Supports Java Applets = True
Supports ActiveX Controls = False
Supports JavaScript Version = 1.7

As if the browser used were Google Chrome.

What could be the problem?

My code below

System.Web.HttpBrowserCapabilities browser = Request.Browser;
string s = "Browser Capabilities\n"
      "Type = "                      browser.Type   "\n"
      "Name = "                      browser.Browser   "\n"
      "Version = "                   browser.Version   "\n"
      "Major Version = "             browser.MajorVersion   "\n"
      "Minor Version = "             browser.MinorVersion   "\n"
      "Platform = "                  browser.Platform   "\n"
      "Is Beta = "                   browser.Beta   "\n"
      "Is Crawler = "                browser.Crawler   "\n"
      "Is AOL = "                    browser.AOL   "\n"
      "Is Win16 = "                  browser.Win16   "\n"
      "Is Win32 = "                  browser.Win32   "\n"
      "Supports Frames = "           browser.Frames   "\n"
      "Supports Tables = "           browser.Tables   "\n"
      "Supports Cookies = "          browser.Cookies   "\n"
      "Supports VBScript = "         browser.VBScript   "\n"
      "Supports JavaScript = "       
        browser.EcmaScriptVersion.ToString()   "\n"
      "Supports Java Applets = "     browser.JavaApplets   "\n"
      "Supports ActiveX Controls = "   browser.ActiveXControls 
            "\n"
      "Supports JavaScript Version = "  
        browser["JavaScriptVersion"]   "\n";

CodePudding user response:

This is what has worked for me in the past:

An accurate browser detection is to use the UserAgent property, as in

HttpContext.Current.Request.Request.UserAgent

If your user agent contains the sub-string "Edg" as in like case:

"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML,
like Gecko) Chrome/93.0.4577.82 Safari/537.36 Edg/93.0.961.52"

Then your browser will be Edge.

If your user agent contains "Chrome" as in this case:

"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36
(KHTML,like Gecko) Chrome/93.0.4577.82 Safari/537.36"

Then your browser will be Chrome.

Similarly for Firefox as in this case:

"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0"

A property I have tried in the past is this:

HttpContext.Current.Request.Browser.Browser 

Which seems like the obvious choice, and will give you the correct browser in many cases, but within an Edge browser, it has given me Chrome. So using the UserAgent string is the best way of obtaining the browser for an ASP.NET HTTP request.

  • Related