コード例 #1
0
 protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
 {
     if (filterContext.HttpContext.User.Identity.IsAuthenticated)
     {
         filterContext.Result = filterContext.HttpContext.Request.Url != null
                                                                                 ? new RedirectResult("~/No-Permission?returnUrl=" + filterContext.HttpContext.Request.Url.AbsolutePath)
                                                                                 : new RedirectResult("~/No-Permission");
     }
     else
     {
         if (filterContext.HttpContext.Request.IsAjaxRequest())
         {
             var urlHelper = new UrlHelper(filterContext.RequestContext);
             var returnUrl = filterContext.HttpContext.Request.Url != null
                                                                         ? filterContext.HttpContext.Request.Url.AbsolutePath
                                                                         : string.Empty;
             filterContext.Result = new ServiceStackJsonResult
             {
                 Data = new RedirectError
                 {
                     Message     = "You must be logged in to perform this action.",
                     ErrorCode   = (int)HttpStatusCode.Unauthorized,
                     RedirectUrl = urlHelper.Account().Login(returnUrl)
                 }
             };
         }
         else
         {
             base.HandleUnauthorizedRequest(filterContext);
         }
     }
 }