admin管理员组

文章数量:1317898

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 remend?

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 remend?

Share Improve this question asked Nov 12, 2015 at 11:11 LiranBoLiranBo 2,1462 gold badges27 silver badges39 bronze badges 5
  • Look for the Edge keyword in the user agent it exists only in Edge browsers and not in old Trident browsers – Sagi Commented Nov 12, 2015 at 11:17
  • 5 It looks like Edge returns the following User-Agent Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.10136 which is recognized as Chrome - so, yes, it is a bug. You can simply check if user agent string contains Edge. However, why do you need this? – Yeldar Kurmangaliyev Commented Nov 12, 2015 at 11:18
  • @Yeldar Kurmangaliyev, I need to block IE\Edge users in my site. I've used the HttpContext so far, I know that it's not the best Idea to check it on the server side, but I thought it might be more accurate, which now I see is not correct. – LiranBo Commented Nov 12, 2015 at 11:22
  • @LiranBo Woah, this website seems to be a browser racist :) If you don't mind, can you describe the original reason of such decision? Maybe, we can find another solution. – Yeldar Kurmangaliyev Commented Nov 12, 2015 at 11:25
  • @Yeldar Kurmangaliyev :), it is they are evil:) , nha I'm using drag & drop on my site which is not supported well in IE\Edge. My site is built to conduct tests over Mturk, so I'm not really worried about losing a few users – LiranBo Commented Nov 12, 2015 at 11:29
Add a ment  | 

3 Answers 3

Reset to default 3

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" 

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.

If you don't mind using JS you can do something like this:

function get_browser() {
            var ua = navigator.userAgent, tem, M = ua.match(/(opera|chrome|safari|firefox|msie|trident|edge(?=\/))\/?\s*(\d+)/i) || [];
            if (/trident/i.test(M[1])) {
                tem = /\brv[ :]+(\d+)/g.exec(ua) || [];
                return { name: 'IE', version: (tem[1] || '') };
            }
            if (M[1] === 'Chrome') {
                tem = ua.match(/\bOPR\/(\d+)/);
                if (tem != null) { return { name: 'Opera', version: tem[1] }; }
                tem = ua.match(/\bEdge\/(\d+)/);
                if (tem != null) { return { name: 'Edge', version: tem[1] }; }
            }
            M = M[2] ? [M[1], M[2]] : [navigator.appName, navigator.appVersion, '-?'];
            if ((tem = ua.match(/version\/(\d+)/i)) != null) { M.splice(1, 1, tem[1]); }
            return {
                name: M[0],
                version: M[1]
            };
        }

and then you can simply call it and send the result to your back-end. It worked like a charm for me.

本文标签: javascriptHttpContextRequestBrowserBrowser detects Edge as ChromeStack Overflow