Server side browser sniffing for IE 11 in .NET

534 views Asked by At

Before the release of IE11, I have been using

context.Request.Browser.Browser == "IE" 

to detect the IE browser on the server end. Since IE11 has user agent changes, now using the same technique doesn't work anymore.

Is regular expression on context.Request.Browser.UserAgent only choice?

1

There are 1 answers

2
Chris Love On

First why do you need to browser sniff in the first place. You really should not do that, instead do feature detection in the client and adjust from there. Here is how to check if the browser 'cuts the mustard'

        <script>

        if (!('querySelector' in document)  //this should work in ie 9+
             || !('localStorage' in window)  //ie 8+
             || !('addEventListener' in window)  //ie 8 + (I think)
            || !('matchMedia' in window)) {//ie 10+

            //do your redirect here
        }

    </script>

With that said here is a method I built to detect if it is obsolete IE or not. Basically it checks if the browser is 'IE' and the major version is 10. If it is not 10 then it is 9,8,7. IE 11 returns 'Internet Explorer' so you know it is good to go.

        public static bool IsOldIE()
    {

        var Browser = HttpContext.Current.Request.Browser;
        var isIE = Browser.Browser == "IE";

        if (!isIE)
        {
            return false;
        }

        return Browser.MajorVersion != 10;

    }