Asp.Net MVC anti-forgery token的问题:nameidentifier or identityprovider not present
2016-03-02 17:42
796 查看
当使用ClaimsIdentity的时候,Asp.Net MVC在生成AntiForgeryToken的时候会默认使用User.Identity中两种ClaimsType的值:NameIdentifier (http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier )和IdentityProvider (http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider)。如果我们使用的STS(Security Token Service)没有提供两种ClaimsType的值,那么MVC就会报AntiForgeryToken生成失败的错误。
详细的错误信息是这样的:
Anti-forgery token issue (MVC 5)
MVC 4, AntiForgeryToken and Claims
详细的错误信息是这样的:
A claim of type 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier' or 'http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider' was not present on the provided ClaimsIdentity. To enable anti-forgery token support with claims-based authentication, please verify that the configured claims provider is providing both of these claims on the ClaimsIdentity instances it generates. 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 ActionResult的其它返回值
- ASP.Net MVC开发基础学习笔记(2):HtmlHelper与扩展方法
- 细说 ASP.NET Cache 及其高级用法
- ASP.NET MVC Html.BeginForm用法1
- ASP.NET Mvc开发之删除修改数据
- ASP.NET Web API 简介
- ASP.NET MVC NonActionAttribute使用说明
- ASP.NET MVC Html.ActionLink使用说明
- IIS发布asp.net项目,要做的准备工作及添加项目
- ASP.Net MVC开发基础学习笔记(3):Razor视图引擎、控制器与路由机制学习
- ASP.NET MVC ActionResult的实现
- 自学MVC看这里——全网最全ASP.NET MVC 教程汇总
- ASP.NET MVC 传值方法ViewData与ViewBag的区别
- Metasploit 对 IE 浏览器的极光漏洞进行渗透利用
- ASP.NET在IE9,IE10,IE11中Form表单身份验证失效问题解决方法
- ASP.NET学习路线图
- ASP.NET常被忽视的一些细节
- 如何在ASP.NET应用程序中初始化
- 报表中的Excel操作之Aspose.Cells(Excel模板)
- ASP.NET MVC 中@Html.Partial,@Html.Action,@Html.RenderPartial,@Html.RenderAction差别