当使用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生成失败的错误。

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

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程序就不会再报错了。

参考资料:

Asp.Net MVC anti-forgery token的问题:nameidentifier or identityprovider not present的更多相关文章

  1. asp.net MVC中防止跨站请求攻击(CSRF)的ajax用法

    参考: Preventing Cross-Site Request Forgery (CSRF) AttacksValidating .NET MVC 4 anti forgery tokens in ...

  2. Asp.net MVC 3 防止 Cross-Site Request Forgery (CSRF)原理及扩展 安全 注入

    原理:http://blog.csdn.net/cpytiger/article/details/8781457 原文地址:http://www.cnblogs.com/wintersun/archi ...

  3. ABP Zero示例项目登录报错“Empty or invalid anti forgery header token.”问题解决

    ABP Zero项目,登录时出现如图"Empty or invalid anti forgery header token."错误提示的解决方法: 在 WebModule.cs的P ...

  4. asp.net mvc 微信公众号token验证

    本人的公众号要申请成为开发者,必须经过token认证.微信公众号的官方代码只列出了PHP代码的实例,明显是歧视.net用户.我用的asp.net mvc中的web api,结果调了好久都没有成功,最后 ...

  5. ASP.NET MVC中防止跨站请求攻击(CSRF)

    转载   http://kevintsengtw.blogspot.co.nz/2013/01/aspnet-mvc-validateantiforgerytoken.html 在 ASP.NET M ...

  6. 认识ASP.NET MVC的5种AuthorizationFilter

    在总体介绍了筛选器及其提供机制(<深入探讨ASP.NET MVC的筛选器>)之后,我们按照执行的先后顺序对四种不同的筛选器进行单独介绍,首先来介绍最先执行的AuthorizationFil ...

  7. Anti-Forgery Request Recipes For ASP.NET MVC And AJAX

    Background (Normal scenario of form submitting) To secure websites from cross-site request forgery ( ...

  8. ASP.NET MVC Ajax 伪造请求

    1.前言 CSRF(Cross-site request forgery)跨站请求伪造,ASP.NET MVC 应用通过使用AJAX请求来提升用户体验,浏览器开发者工具可以一览众山小,就很容易伪造了请 ...

  9. ASP.NET MVC 防止CSRF攻击

    简介 MVC中的Html.AntiForgeryToken()是用来防止跨站请求伪造(CSRF:Cross-site request forgery)攻击的一个措施,它跟XSS(XSS又叫CSS:Cr ...

随机推荐

  1. mongoDB id 导出,dump,sed,count,mysql import等用法示例

    #count collectiondb.news.count({"lpublishtime":{"$gte":1358697600000}}); #mongo导 ...

  2. Android平台Native开发与JNI机制详解

    源文链接: http://mysuperbaby.iteye.com/blog/915425 一个Native Method就是一个Java调用非Java代码的接口.一个Native Method是这 ...

  3. the filename directory name or volume label syntax is incorrect

    使用virtual PC 时出现的一些问题 1.问题点:在安装virtual PC的时候,需要先安装WindowsXPMode_en-us.exe 我下载的英文版,路径默认在C:\360安全浏览器下载 ...

  4. 小程序排错(redis导致)

    小程序突然出问题,题库加载不了,程序正常,测试环境同样环境,同样代码都正常,但是线上数据秒过期,怀疑redis过期时间设置有问题,但是检查配置没问题,写入数据带过期时间也正常. redis设置key: ...

  5. ie6、ie7下JSON.parse JSON未定义的解决方法

    解决方法一: var jsons = req.responseText; var s; if (typeof(JSON) == 'undefined'){ s = eval("(" ...

  6. tony_nginx的安装和配置

    yum安装nginx Centos默认的yum源里没有nginx,需要手动添加源,有两种方法: 使用nginx提供的一个源设置安装包 nginx下载页面:http://nginx.org/en/dow ...

  7. CAN总线过滤规则

    奇怪的设计 If (AFMR & Message_ID) == (AFMR & AFIR) then Capture Message AFIR 1 接收 AFMR 0 接收 设置某接收 ...

  8. Unity3d 引擎原理详细介绍、Unity3D引擎架构设计 - zhibolife

    时间 2014-03-24 11:18:00  博客园-所有随笔区原文  http://www.cnblogs.com/zhibolife/p/3620440.html 体系结构 为了更好地理解游戏的 ...

  9. hud项目lcd调试

    lcd规格: 像素:480x280 bpp:16 pix_format:RGB565 在开发板终端中执行: ls /dev/fb0 -l ---> crw-rw----    1 root   ...

  10. JIRA /mnt/server/atlassian-jira-6.3.6-standalone/bin/start-jira.sh

    JIRA 敏捷开发平台部署记录   分类: 敏捷开发   1.1 jira说明 JIRA是Atlassian公司出品的项目与事务跟踪工具,被广泛应用于缺陷跟踪.客户服务.需求收集.流程审批.任务跟踪. ...