当我使用用户的凭据登录时,第一次登录应用程序时没有错误,
但是当我注销并再次使用其他用户的凭据登录时出现错误
The provided anti-forgery token was meant for user "UserName", but the current user is "".
我正在使用 asp.net mvc 5
实现 AntiForgoryToken
和 IPrincipal
我试过了:
AntiForgeryConfig.SuppressIdentityHeuristicChecks = true;
在 Application_Start() 中
如何修复此错误?
提前致谢。
最佳答案
我发现我的 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/