Authentication AntiforgeryToken验证失败

Authentication AntiforgeryToken验证失败,authentication,antiforgerytoken,asp.net-core-2.1,Authentication,Antiforgerytoken,Asp.net Core 2.1,我有一个ASP CORE 2.1应用程序,它与angular SPA客户端一起工作。 我按照规定保护了我的申请 但问题是,在我登录系统后,我经常收到400个错误请求。根据我的观点,这就是系统中发生的事情: 对应用程序的第一个请求->返回AntiForgery CookieToken和 RequestToken(重要注意,用户尚未通过身份验证) 用户登录系统->反伪造验证通过, 已将身份验证cookie发送到客户端 用户请求任何 其他终结点,但由于为发布了AntiforgeryTokenSet 未

我有一个ASP CORE 2.1应用程序,它与angular SPA客户端一起工作。 我按照规定保护了我的申请

但问题是,在我登录系统后,我经常收到400个错误请求。根据我的观点,这就是系统中发生的事情:

  • 对应用程序的第一个请求->返回AntiForgery CookieToken和 RequestToken(重要注意,用户尚未通过身份验证
  • 用户登录系统->反伪造验证通过, 已将身份验证cookie发送到客户端
  • 用户请求任何 其他终结点,但由于为发布了AntiforgeryTokenSet 未经身份验证的用户,收到400个错误请求
  • 很明显,登录后我们需要重新发布AntiforgeryTokenSet,但我不知道在哪里以及如何重新发布。我试图在结果过滤器中发布令牌,但没有成功

    public class SPAAntiforgeryCookieResultFilter : ResultFilterAttribute
        {
            private readonly IAntiforgery _antiforgery;
    
            public SPAAntiforgeryCookieResultFilter(IAntiforgery antiforgery)
            {
                _antiforgery = antiforgery;
            }
    
            public override void OnResultExecuting(ResultExecutingContext context)
            {
                Action assignAntiForgery = () =>
                {
                    var tokens = _antiforgery.GetAndStoreTokens(context.HttpContext);
                    context.HttpContext.Response.Cookies.Append("XSRF-TOKEN", tokens.RequestToken, new CookieOptions() { HttpOnly = false });
                };
    
                if (context.Result is ViewResult)
                {
                    assignAntiForgery();
                }
                else if (string.Equals(context.ActionDescriptor.ActionName, nameof(AccountController.Login), StringComparison.OrdinalIgnoreCase))
                {
                    assignAntiForgery();
                }
            }
        }
    
    似乎ResultExecutingContext不知道经过身份验证的用户,仍然为匿名用户颁发令牌。
    那么,如何在登录身份验证用户后立即刷新antiforgery RequestToken令牌呢?

    现在,我找到了解决此问题的方法

    以前,我在成功登录后返回了
    Ok()
    ,但现在我返回了
    RedirectToAction()
    ,并且我的
    OnResultExecuting
    过滤器略有更改,以便在发生
    RedirectToAction
    后刷新令牌

        public class SPAAntiforgeryCookieResultFilter : ResultFilterAttribute
            {
                private readonly IAntiforgery _antiforgery;
    
                public SPAAntiforgeryCookieResultFilter(IAntiforgery antiforgery)
                {
                    _antiforgery = antiforgery;
                }
    
                public override void OnResultExecuting(ResultExecutingContext context)
                {
                    Action assignAntiForgery = () =>
                    {
                        var tokens = _antiforgery.GetAndStoreTokens(context.HttpContext);
                        context.HttpContext.Response.Cookies.Append("XSRF-TOKEN", tokens.RequestToken, new CookieOptions() { HttpOnly = false });
                    };
    
                    if (context.Result is ViewResult)
                    {
                        assignAntiForgery();
                    }
                    // Here we check whether our redirect action is executed. Update AFT if it is
                    else if (string.Equals(context.ActionDescriptor.RouteValues["action"], nameof(AccountController.RefreshAntiForgeryAfterLogin), StringComparison.OrdinalIgnoreCase))
                    {
                        assignAntiForgery();
                    }
                }
            }
    
    这种肮脏的黑客行为的工作方式如下:

  • 对应用程序的第一个请求->返回AntiForgery CookieToken和RequestToken(重要注意,用户尚未通过身份验证)
  • 用户按登录到系统->反伪造验证通过,身份验证cookie设置->用户获得带有身份验证cookie和状态代码302的响应(重定向)->用户通过具有身份验证信息的上下文接触我们的
    重定向用户操作
    方法(
    User.IsAuthenticated==true