asp.net-mvc-5 - 提供的防伪 token 是针对用户 "UserName"的,但当前用户是 ""

标签 asp.net-mvc-5 antiforgerytoken iprincipal

当我使用用户的凭据登录时,第一次登录应用程序时没有错误,

但是当我注销并再次使用其他用户的凭据登录时出现错误

The provided anti-forgery token was meant for user "UserName", but the current user is "".

我正在使用 asp.net mvc 5 实现 AntiForgoryTokenIPrincipal

我试过了:

  1. AntiForgeryConfig.SuppressIdentityHeuristicChecks = true; 在 Application_Start() 中

  2. 我已经引用了这个链接 When attempt logoff, The provided anti-forgery token was meant for user "XXXX", but the current user is ""

如何修复此错误?

提前致谢。

最佳答案

我发现我的 IIS 实例同时启用了匿名和 Windows 身份验证,这导致我的应用程序出现此异常

Authentication Settings both enabled

这个设置似乎是这个异常的原因。似乎 ValidateAntiForgeryToken 操作过滤器正在使用在 Anon 身份验证中填充了空字符串的当前用户,然后尝试将其与使用 Win 身份验证填充的 token 进行匹配。

通过关闭 Windows 或匿名身份验证(对于我的应用程序,我关闭了 Anon 身份验证), token 现在匹配。

Authentication Settings one enabled

可以在 IIS 管理器或 web.config 文件中像这样更改此设置

<system.webServer>
    <security>
      <authentication>
        <anonymousAuthentication enabled="false" />
        <windowsAuthentication enabled="true" />
      </authentication>
    </security>
</system.webServer>

关于asp.net-mvc-5 - 提供的防伪 token 是针对用户 "UserName"的,但当前用户是 "",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34266523/

相关文章:

javascript - 查询字符串参数在发布后被删除并返回到 asp.net mvc 中的同一页面

asp.net-mvc - 列出用户时,带有角色的 Windows 身份验证不起作用,角色具有更高的优先级

c# - 无法加载文件或程序集或其依赖项。该系统找不到指定的文件

c# - ObjectDisposedException 即使我正在使用 .Include()、.ToList() 和其他一切

asp.net-mvc - 如何在不同域的 MVC 前端和 Web Api 之间实现防伪 token ?

asp.net-mvc - 您可以将 RolePrincipal 包装在自定义 IPrincipal 对象中吗?

asp.net-mvc - ASP MVC防伪 token 问题

asp.net-core - 如何在 ASP.NET Core 3.1 中实现自定义 ValidateAntiforgeryTokenAuthorizationFilter

openid - 将用户 ID 存储在主体或身份中? ASP.Net/OpenID

asp.net - User.Identity.Name 和 User.Identity.IsAuthenticated 是什么集合?