Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpContext.Request.Browser.Browser detects Edge as Chrome

This is really odd, I'm using the HttpContext.Request.Browser.Browser property to check from which browser the request came for.

When using chrome, the value is Chrome

When using firefox, the value is Firefox

When using Edge, the value is Chrome

Is it a known bug in HttpContext?

What is the most accurate way to detect IE\Edge users? I've seen many JS codes which checks the user_agent value, but it keeps changing with every IE version so it is really hard to know which code is updated, and which one isn't.

Maybe there's some good JS library for that purpose that someone can please recommend?

like image 326
LiranBo Avatar asked Nov 01 '25 23:11

LiranBo


2 Answers

This Method worked fine for me

if (Regex.IsMatch(HttpContext.Request.UserAgent, @"Edge\/\d+"))
 {
    wrkBrowser = "Edge"
  }

If you're checking for multiple browsers be careful of the order you check as many browsers like to mention other browsers in their UserAgent string. At that time Check the Below Condition also

 Request.Browser.Browser == "Chrome" 
like image 172
Ahamed Ahlam Avatar answered Nov 03 '25 13:11

Ahamed Ahlam


I was facing the same issue and I figured out the root cause of it and also able to resolve it.

Solution is to increase the userAgentCacheKeyLength in Web.config, as follows:

<system.web>
    <browserCaps userAgentCacheKeyLength="256" />
</system.web>

By default the value for userAgentCacheKeyLength is 64.

What happens is that IIS cached the user agent value up to the length defined in userAgentCacheKeyLength (which is 64 by default). So if you hit the first request with Chrome IIS stores first 64 characters of user agent value of Chrome in cache. Now the next time if you hit the request from Edge, IIS matches the first 64 characters and it's similar to Chrome's so IIS returns the browser value as Chrome.

If you increase the value to 256, IIS will be able to store the full value of user agent hence it will distinguish Chrome and Edge browsers accurately always.

like image 39
rizzz86 Avatar answered Nov 03 '25 13:11

rizzz86



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!