MVC5 A claim of type 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier' or 'http://schemas.microsoft.com/accesscontrolservice/2

前台页面使用@Html.AntiForgeryToken()和Controller中的[ValidateAntiForgeryToken]配合使用可以防止CSRF攻击,详细介绍可查看一下链接:

http://www.cnblogs.com/hyddd/archive/2009/04/09/1432744.html

出现当前错误,是因为在登陆完成为当前用户创建ClaimsIdentity对象时没有指定ClaimTypes,导致验证时不知道该用什么类型进行验证,或者验证类型不正确。

解决办法:

打开Global.asax文件,在 Application_Start()方法中添加

AntiForgeryConfig.UniqueClaimTypeIdentifier =ClaimTypes.Name;

如果不知道到底该配置为什么类型,防止配置错误,可以用如下方式查看类型:

1.先将前台页面的@Html.AntiForgeryToken()删除掉,在Controller中添加如下代码

   string strCurrentType = AntiForgeryConfig.UniqueClaimTypeIdentifier;

2.在上面那句代码上打断点,然后调试项目,就能知道在Application_Start()方法中究竟该配置哪个类型了。

原文地址:https://www.cnblogs.com/pkwblack/p/3952111.html