Mvc3 Antiforgery token multi tabs

前端 未结 2 1308
感情败类
感情败类 2020-12-29 14:11

we have a specific issue with the anti forgery token on the login page. If the user logs in with only one active window everything works great however if the user opens the

2条回答
  •  旧巷少年郎
    2020-12-29 14:31

    This behaviour in MVC3 or MVC4 is as designed however it is very user-unfriendly as explained above, however in production this issue needs to be solved gracefully and application needs to handle this odd situation. The solution for this problem is to create a filter that is applied to the login post that will verify if the user is logged in and take them to the correct page otherwise they will remain on the login page.

    Below is the code for the filter attribute

    /// 
    /// Handle Antiforgery token exception and redirect to customer area if the user is Authenticated
    /// 
    public class RedirectOnError : HandleErrorAttribute
    {
        /// 
        /// Override the on exception method and check if the user is authenticated and redirect the user 
        /// to the customer service index otherwise continue with the base implamentation
        /// 
        /// Current Exception Context of the request
        public override void OnException(ExceptionContext filterContext)
        {
            if (filterContext.Exception is HttpAntiForgeryException && filterContext.HttpContext.User.Identity.IsAuthenticated)
            {
                // Set response code back to normal
                filterContext.HttpContext.Response.StatusCode = 200;
    
                // Handle the exception
                filterContext.ExceptionHandled = true;
    
                UrlHelper urlH = new UrlHelper(filterContext.HttpContext.Request.RequestContext);
    
                // Create a new request context
                RequestContext rc = new RequestContext(filterContext.HttpContext, filterContext.RouteData);
    
                // Create a new return url
                string url = RouteTable.Routes.GetVirtualPath(rc, new RouteValueDictionary(new { Controller = "CustomerArea", action = "Index" })).VirtualPath;
    
                // Check if there is a request url
                if (filterContext.HttpContext.Request.Params["ReturnUrl"] != null && urlH.IsLocalUrl(filterContext.HttpContext.Request.Params["ReturnUrl"]))
                {
                    url = filterContext.HttpContext.Request.Params["ReturnUrl"];
                }
    
                // Redirect the user back to the customer service index page
                filterContext.HttpContext.Response.Redirect(url, true);
            }
            else
            {
                // Continue to the base
                base.OnException(filterContext);
            }
        }
    }
    

    This is the example of usage

            [HttpPost]
            **[RedirectOnError]**
            [ValidateAntiForgeryToken]
            public ActionResult LogOn(LogOnViewModel model, UserSessionState session, string returnUrl)
            {
            .....
            }
    

提交回复
热议问题