你的位置:首页 > 软件开发 > ASP.net > Asp.Net MVC anti

Asp.Net MVC anti

发布时间:2016-03-02 18:00:04
当使用ClaimsIdentity的时候,Asp.Net MVC在生成AntiForgeryToken的时候会默认使用User.Identity中两种ClaimsType的值:NameIdentifier (http://schemas. 详细的错误信息是这样的: A clai ...

当使用ClaimsIdentity的时候,Asp.Net MVC在生成AntiForgeryToken的时候会默认使用User.Identity中两种ClaimsType的值:NameIdentifier (http://schemas.Asp.Net MVC anti

详细的错误信息是这样的:

A claim of type 'http://schemas.If the configured claims provider instead uses a different claim type as a unique identifier, it can be configured by setting the static property AntiForgeryConfig.UniqueClaimTypeIdentifier.

从错误提示我们可以看到Asp.net MVC强制要求提供NameIdentifier和IdentityProvider这两个值,这是默认的行为。但是这个默认的行为是可以改的。我们用ADFS,IdentityProvider这个值是没有的。根据错误消息的最后一句提示我们,可以修改AntiForgeryConfig.UniqueClaimTypeIdentifier的值,从而告诉Asp.Net MVC用别的ClaimsType的值来生成AntiForgeryToken。比如我们准备使用NameIdentifier,只需要在Global.asax.cs中添加下面一句话:

AntiForgeryConfig.UniqueClaimTypeIdentifier = System.Security.Claims.ClaimTypes.NameIdentifier;

重新编译运作之后,只要ADFS提供了NameIdentifier程序就不会再报错了。

参考资料:
  • Anti-forgery token issue (MVC 5)
  • MVC 4, AntiForgeryToken and Claims

原标题:Asp.Net MVC anti

关键词:ASP.NET

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。