0

I have a web application which has alot of widgests/page in it. Certain widgets I want to block when opened in Safari and display some sort of message if anyone tries to open the webapp from Safari browser. Below is the code I have written.

  public class BlockFromSafari : System.Web.Mvc.ActionFilterAttribute
    {
        // runs just before the action is executed
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var browserType = filterContext.HttpContext.Request.Browser.Browser;
            base.OnActionExecuting(filterContext);
            if (browserType.Contains("Safari"))
            {
                // Redirect the user accordingly
                filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary { { "controller", "Security" }, { "action", "Safari" } });
            }
        }
    }

Kindly note that I have applied this filter on the all the Action Method which I want

  public ActionResult Safari()
  {
            return Content("This is SAFARI!!");
  }

Current Behaviour:

  1. If I open the website and visit the corresponding URL, in Chrome in WindowsPC page opens fine, where as if I open in Safari Browser in Windows PC I get redirected to "This is SAFARI content page".

  2. If I open the website and visit the corresponding URL, in Chrome in Mac PC page opens fine, where as if I open in Safari Browser in Mac PC I get redirected to "This is SAFARI content page"

So far so good. Now here is the problem

Issue: If I open the website and visit the corresponding URL, in Chrome in Mobile device or Ipad page still displays "This is SAFARI" ideally I expected the same behaviour as in PC. Please guide me.

Expected Behaviour:

Mobile Device-Chrome - Should open fine

Mobeil Devive-Safari - "This is SAFARI" should be displayed

Interesting Read Chrome Treated as Safari

Unbreakable
  • 7,776
  • 24
  • 90
  • 171

1 Answers1

0

So wear, this article probably will help you https://51degrees.com/blog/mobile-and-tablet-device-detection-with-mvc5

  1. The main idea is to get Browser information from headers, something like:

    requestContext.HttpContext.Request.Headers.Profiles.SelectMany(p => p.Values).ToArray();

So you could add another conditional if it's mobile.

  1. Another aproach is to use JavaScript and redirect if its mobile, like:

    var userAgent = window.navigator.userAgent; if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i)) { // iPad or iPhone } else { // Anything else } here is the full answer(Determine if user navigated from mobile Safari)

german1311
  • 95
  • 5