MrMistreater

Mobile browser detection

Apr 13th, 2012
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.18 KB | None | 0 0
  1. using System.Text.RegularExpressions;
  2. using System.Web;
  3. using System.Web.Mvc;
  4. using System.Web.Routing;
  5.  
  6. namespace MobileTest
  7. {
  8.     /*
  9.      * Add the global filer RedirectMobileDevicesToMobileAreaAttribute to the global filter collection in
  10.      * the Application_Start() of Global.asax.cs file
  11.      *
  12.      * GlobalFilters.Filters.Add(new RedirectMobileDevicesToMobileAreaAttribute(), 1);
  13.      *
  14.      * Add a reference to "51Degrees.mobi" library using NuGet
  15.      *
  16.      * See: http://weblogs.asp.net/shijuvarghese/archive/2011/02/25/mobile-enabled-web-apps-with-asp-net-mvc-3-and-jquery-mobile.aspx
  17.      *      http://aspnetmobilesamples.codeplex.com/
  18.      *      
  19.      * How to configure areas in MVC3:
  20.      *      http://2010wave.blogspot.com/2009/12/how-to-create-areas-in-aspnet-mvc.html
  21.      *      http://stackoverflow.com/questions/5243158/how-to-configure-areas-in-asp-net-mvc3
  22.      */
  23.  
  24.     public class RedirectMobileDevicesToMobileAreaAttribute : AuthorizeAttribute
  25.     {
  26.         /// <summary>
  27.         /// When overridden, provides an entry point for custom authorization checks.
  28.         /// </summary>
  29.         /// <param name="httpContext">The HTTP context, which encapsulates all HTTP-specific information about an individual HTTP request.</param>
  30.         /// <returns>
  31.         /// true if the user is authorized; otherwise, false.
  32.         /// </returns>
  33.         /// <exception cref="T:System.ArgumentNullException">The <paramref name="httpContext"/> parameter is null.</exception>
  34.         protected override bool AuthorizeCore(HttpContextBase httpContext)
  35.         {
  36.             // Only redirect on the first request in a session
  37.             if (!httpContext.Session.IsNewSession)
  38.                 return true;
  39.  
  40.             // Don't redirect non-mobile browsers
  41.             if (!httpContext.Request.Browser.IsMobileDevice)
  42.                 return true;
  43.  
  44.             // Don't redirect requests for the Mobile area
  45.             if (Regex.IsMatch(httpContext.Request.Url.PathAndQuery, "/Mobile($|/)"))
  46.                 return true;
  47.  
  48.             return false;
  49.         }
  50.  
  51.         /// <summary>
  52.         /// Gets the redirection route values.
  53.         /// </summary>
  54.         /// <param name="requestContext">The request context.</param>
  55.         /// <returns></returns>
  56.         protected virtual RouteValueDictionary GetRedirectionRouteValues(RequestContext requestContext)
  57.         {
  58.             return new RouteValueDictionary(new { area = "Mobile", controller = "Home", action = "Index" });
  59.         }
  60.  
  61.         /// <summary>
  62.         /// Processes HTTP requests that fail authorization.
  63.         /// </summary>
  64.         /// <param name="filterContext">Encapsulates the information for using <see cref="T:System.Web.Mvc.AuthorizeAttribute"/>. The <paramref name="filterContext"/> object contains the controller, HTTP context, request context, action result, and route data.</param>
  65.         protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
  66.         {
  67.             var redirectionRouteValues = GetRedirectionRouteValues(filterContext.RequestContext);
  68.             filterContext.Result = new RedirectToRouteResult(redirectionRouteValues);
  69.         }
  70.     }
  71. }
Add Comment
Please, Sign In to add comment