check whether browser is chrome or edge

Shaggy picture Shaggy · Aug 7, 2015 · Viewed 24.3k times · Source

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?

Answer

Hamid Pourjam picture Hamid Pourjam · Aug 7, 2015

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.