积极答复者
使用IdentityServer后出现了问题

问题
-
您好,
我在项目中使用了IdentityServer3后, 在@Html.AntiForgeryToken() 代码处抛出了如下异常。
System.InvalidOperationException:“提供的 ClaimsIdentity 上不存在类型“http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier”或“http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider”的声明。若要使用基于声明的身份验证启用防伪标记支持,请确认配置的声明提供程序在它生成的 ClaimsIdentity 实例上同时提供这两个声明。如果配置的声明提供程序使用其他声明类型作为唯一标识符,则可以通过设置静态属性 AntiForgeryConfig.UniqueClaimTypeIdentifier 来配置该提供程序。”
我尝试加入了nameidentifier和identityprovider claim后问题还是存在,请大神们帮忙解决这个问题。
答案
-
你好,
从你的错误信息来看,你好像并没有设置Claim类的类型。
Claim类的类型是一个url,就像错误信息里面贴的一样。
http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier
http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider”
我建议你可以考虑声明claim类的时候添加这个类型,然后再尝试一下。
代码类似这样:
nid.AddClaim(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier"),n.ProtocolMessage.IdToken));
详细的你可以参照MSDN的Claim类说明。
https://msdn.microsoft.com/zh-cn/library/microsoft.identitymodel.claims.claim.aspx
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
- 已编辑 Brando Zhang 2017年11月1日 3:17
- 已标记为答案 王小胖哥哥 2017年11月1日 4:57
全部回复
-
你好,
从你的错误信息来看,你好像并没有设置Claim类的类型。
Claim类的类型是一个url,就像错误信息里面贴的一样。
http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier
http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider”
我建议你可以考虑声明claim类的时候添加这个类型,然后再尝试一下。
代码类似这样:
nid.AddClaim(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier"),n.ProtocolMessage.IdToken));
详细的你可以参照MSDN的Claim类说明。
https://msdn.microsoft.com/zh-cn/library/microsoft.identitymodel.claims.claim.aspx
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
- 已编辑 Brando Zhang 2017年11月1日 3:17
- 已标记为答案 王小胖哥哥 2017年11月1日 4:57