asp.net-mvc - Mvc3 Antiforgery token 多标签

标签 asp.net-mvc asp.net-mvc-3 antiforgerytoken

我们在登录页面上的防伪 token 存在特定问题。如果用户仅使用一个事件窗口登录,则一切正常,但是,如果用户在两个不同的窗口中打开登录页面并从窗口A登录(不会登录,则没有问题),然后从该窗口中的窗口B返回登录。用户将收到“未提供必需的防伪 token 或该 token 无效”的信息。

有没有其他办法可以从 View / Controller 操作中删除防伪 token ?我们希望拥有 token 来提高安全性!

这与这个问题非常相似,但是有人问了mvc2
MVC ValidateAntiForgeryToken multi-tabs problem

最佳答案

MVC3或MVC4中的此行为是按设计设计的,但是如上所述,它对用户非常不友好,但是在生产中,必须妥善解决此问题,并且应用程序需要处理这种奇怪的情况。解决此问题的方法是创建一个应用于登录帖子的过滤器,该筛选器将验证用户是否已登录并将用户带到正确的页面,否则他们将保留在登录页面上。

以下是过滤器属性的代码

/// <summary>
/// Handle Antiforgery token exception and redirect to customer area if the user is Authenticated
/// </summary>
public class RedirectOnError : HandleErrorAttribute
{
    /// <summary>
    /// 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
    /// </summary>
    /// <param name="filterContext">Current Exception Context of the request</param>
    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);
        }
    }
}

这是用法的例子
        [HttpPost]
        **[RedirectOnError]**
        [ValidateAntiForgeryToken]
        public ActionResult LogOn(LogOnViewModel model, UserSessionState session, string returnUrl)
        {
        .....
        }

关于asp.net-mvc - Mvc3 Antiforgery token 多标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9881157/

相关文章:

c# - 将区域/ Controller 限制为经过身份验证的用户

angularjs - ASP.NET Core API - 登录和防伪 token

asp.net-mvc - ASP.NET MVC - ValidateAntiForgeryToken 过期

asp.net-mvc - __RequestVerificationToken 并不总是被创建

c# - "Logging out"用户关闭浏览器事件

asp.net-mvc - 如何在ASP.NET MVC6 beta5项目中添加 Controller ?

c# - 将 System.ComponentModel.DisplayName 与动态值一起使用?

asp.net-mvc-3 - 如何在asp.net mvc模型中访问 session

c# - 命令、命令处理程序和命令调用程序

.net - 将 ASP.net MVC 网站部署到 Azure 网站的子文件夹?