16

My current website runs only in Chrome browser, to do this I have checked in following way

if (Request.Browser.Browser == "Chrome")
{
   // Allow
}

But for Edge as well it is returning as "Chrome" only.

How can I allow access from only Chrome browser?

3
  • 1
    How about using feature detection instead of user agent sniffing? BTW, "This website only works in X browser" never goes over well with users. Aug 7, 2015 at 6:14
  • 1
    @AlexanderO'Mara Its in beta version as of now and we are working on it to make to available for all browsers. But for time being we want to restrict it from accessing to other browsers
    – Shaggy
    Aug 7, 2015 at 6:16
  • 1
    The better question is “what are you using that doesn’t work in Microsoft Edge?” We should solve that problem, not sniff and break the site for users. Aug 20, 2015 at 16:25

4 Answers 4

20

You can check user-agent and see whether it is Microsoft Edge or not because Microsoft Edge contains Edge/version in it's user-agent string.

//get user agent somehow here based on what you are working on
userAgent = Request.UserAgent;

if (userAgent.IndexOf("Edge") > -1)
{
   // maybe client's browser is Microsoft Edge
}

sample of Edge user-agent strings

Mozilla/5.0 (X11; CrOS x86_64 6783.1.0) AppleWebKit/537.36 (KHTML, like Gecko) Edge/12.0

See more here and here

At the end I suggest to use feature detection on browser instead of acting based on user-agent.

3
  • 2
    Careful, black-listing/white-listing user agents is how this whole user agent thing got started. Aug 7, 2015 at 6:16
  • 2
    yes. It's not a good idea at all. Feature detection is the best known approach here IMHO. @AlexanderO'Mara Aug 7, 2015 at 6:19
  • @AlexanderO'Mara even feature detection isn't always 100%. Why is Microsoft Edge appearing as Chrome when reading Request.Browser.Browser?... Why does Mozilla and AppleWebKit appear in it's user-agent string? Apr 24, 2019 at 15:31
2

I'm curious -- what is the use case? Regardless, here you go:

Microsoft Edge UA string:

Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.10136

I detail why in this blog post.

Neowin recently reported that Microsoft’s new browser for Windows 10, Spartan, uses the Chrome UA string, “Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36 Edge/12.0″. That is done on purpose.

You’ll also notice that the entire string ends with “Edge/12.0″, which Chrome does not.

I should point out, that this isn’t a redical departure from what Microsoft did with IE 11, which on Windows 8 reads: Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko, as explained in this post.

What is User Agent sniffing?

Often, web developers will UA sniffing for browser detection. Mozilla explains it well on their blog:

Serving different Web pages or services to different browsers is usually a bad idea. The Web is meant to be accessible to everyone, regardless of which browser or device they’re using. There are ways to develop your web site to progressively enhance itself based on the availability of features rather than by targeting specific browsers.

Here’s a great article explaining the history of the User Agent.

Often, lazy developers will just sniff for the UA string and disable content on their website based on which browser they believe the viewer is using. Internet Explorer 8 is a common point of frustration for developers, so they will frequently check if a user is using ANY version of IE, and disable features.

The Edge team details this even deeper on their blog.

All user agents strings contain more information about other browsers than the actual browser you are using – not just tokens, but also ‘meaningful’ version numbers.

Internet Explorer 11’s UA string:

Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko

Microsoft Edge UA string:

Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.10136

The userAgent property has been aptly described as “an ever-growing pack of lies” by Patrick H. Lauke in W3C discussions. (“or rather, a balancing act of adding enough legacy keywords that won’t immediately have old UA-sniffing code falling over, while still trying to convey a little bit of actually useful and accurate information.”)

We recommend that web developers avoid UA sniffing as much as possible; modern web platform features are nearly all detectable in easy ways. Over the past year, we’ve seen some UA-sniffing sites that have been updated to detect Microsoft Edge… only to provide it with a legacy IE11 code path. This is not the best approach, as Microsoft Edge matches ‘WebKit’ behaviors, not IE11 behaviors (any Edge-WebKit differences are bugs that we’re interested in fixing).

In our experience Microsoft Edge runs best on the ‘WebKit’ code paths in these sites. Also, with the internet becoming available on a wider variety of devices, please assume unknown browsers are good – please don’t limit your site to working only on a small set of current known browsers. If you do this, your site will almost certainly break in the future.

Conclusion

By presenting the Chrome UA string, we can work around the hacks these developers are using, to present the best experience to users.

1
  • 1
    One use case... report to the user whether they were using Chrome or Edge when they logged in last week. (Nothing to do with feature detection... just telling a human what they need to know.) Aug 18, 2020 at 21:15
1
if(HttpContext.Current.Request.UserAgent.Contains("Edge"))
{
   // Allow
}

Worked for me.

0

I use browser detection when logging JavaScript errors as it is useful to know in which browser the error occurred.

Originally from this question I used an entry in the App_Browsers folder:

<browser id="Edge" parentID="Chrome">
  <identification>
    <userAgent match="Edge/(?'version'(?'major'\d+)(?'minor'\.\d+))" />
  </identification>
  <capabilities>
    <capability name="browser" value="Edge" />
    <capability name="version" value="${version}" />
    <capability name="majorversion" value="${major}" />
    <capability name="minorversion" value="${minor}" />
  </capabilities>
</browser>

But this was inconsistent and sometimes still reported Chrome so now I additionally use this wrapper class:

using System.Web;

public class BrowserInfo
{
    public BrowserInfo(HttpRequestBase request)
    {
        if (request.Browser != null)
        {
            if (request.UserAgent.Contains("Edge")
                && request.Browser.Browser != "Edge")
            {
                Name = "Edge";
            }
            else
            {
                Name = request.Browser.Browser;
                Version = request.Browser.MajorVersion.ToString();
            }
            Browser = request.Browser;
            Platform = request.Browser.Platform;
            IsMobileDevice = request.Browser.IsMobileDevice;
            if (IsMobileDevice)
            {
                Name = request.Browser.Browser;
                Name = request.Browser.Browser;
            }
        }
    }

    public HttpBrowserCapabilitiesBase Browser { get; }
    public string Name { get; }
    public string Version { get; }
    public string Platform { get; }
    public bool IsMobileDevice { get; }
    public string MobileBrand { get; }
    public string MobileModel { get; }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.