ASP.Net和WIF只提供了一项索赔,但其他索赔显示在跟踪中

时间:2016-02-26 03:45:41

标签: asp.net wif claims-based-identity

这个问题令我困惑。在用户进行身份验证后,我似乎无法在我的c#代码中获得nameidentifier声明。这是一个.Net 4.5 Web表单站点。我能得到的唯一声明是http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name

很容易迭代索赔,只有一个可用。清楚地看一下跟踪名称标识符,但是在迭代ClaimsPrincipal.Current.Claims或ClaimsPrincipal.Current.Identities []时,我的代码中无法访问它。声明。

以下是跟踪示例:



<TraceRecord xmlns="http://schemas.microsoft.com/2009/10/IdentityModel/TraceRecord" Severity="Information">
<Description>Setting Thread.CurrentPrincipal from session token.</Description>
<AppDomain>/LM/W3SVC/2/ROOT/qat-5-131009300157520403</AppDomain>
<ClaimsPrincipalTraceRecord xmlns="http://schemas.microsoft.com/2009/06/IdentityModel/ClaimsPrincipalTraceRecord">
<ClaimsPrincipal Identity.Name="Adfs">
<ClaimsIdentity Name="Adfs" Label="" RoleClaimType="http://schemas.microsoft.com/ws/2008/06/identity/claims/role" NameClaimType="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name">
<Claim Type="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier" ValueType="http://www.w3.org/2001/XMLSchema#string" Value="something"/>
<Claim Type="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name" ValueType="http://www.w3.org/2001/XMLSchema#string" Value="Adfs"/>
<Claim Type="http://schemas.microsoft.com/ws/2008/06/identity/claims/role" ValueType="http://www.w3.org/2001/XMLSchema#string" Value="Adfs.email@somewhere"/>
<Claim Type="http://schemas.microsoft.com/ws/2008/06/identity/claims/authenticationmethod" ValueType="http://www.w3.org/2001/XMLSchema#string" Value="urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport"/>
<Claim Type="http://schemas.microsoft.com/ws/2008/06/identity/claims/authenticationinstant" ValueType="http://www.w3.org/2001/XMLSchema#dateTime" Value="2016-02-26T01:53:04.289Z"/>
</ClaimsIdentity>
</ClaimsPrincipal>
</ClaimsPrincipalTraceRecord>
</TraceRecord>
&#13;
&#13;
&#13;

我的代码可以返回身份和一个名称声明。但是包含电子邮件地址和nameidentifier的角色并不存在。只有一个主张。

我已经完成了所有事情。我的应用程序只是简单的单页测试应用程序。没有什么有趣的,没有任何配置可以放弃一些声明或任何东西。

任何人都可以建议我应该查看的地方或我可以做的任何事情来获取nameidentifier(或任何其他声明)。

我无法理解为什么他们会在跟踪中显示,但在我的应用程序中不可用。我认为它们只会出现在ClaimsPrincipal.Current或其中一个ClaimsPrincipal.Current.Identities中的一个Claim集合中(只有一个标识与跟踪输出相匹配)。

任何帮助真的很感激我还有一点头发!提前谢谢。

1 个答案:

答案 0 :(得分:1)

我将回答我自己的问题。在此之后,我没有留下太多头发。问题可能会抓住其他人。

使用以下内容访问ClaimsPrincipal时,我使用的是System.IdentityModel命名空间。这只给了我一个主张。虽然启用了WIF跟踪,但我可以看到更多的声明被提供给我的应用程序。

using System.IdentityModel;
...
ClaimsPrincipal claimsPrincipal = ClaimsPrincipal.Current;

解决方案只是使用以下内容:

using Microsoft.IdentityModel.Claims;
...
IClaimsPrincipal claimsPrincipal = (IClaimsPrincipal)Thread.CurrentPrincipal;

然后,突然所有其他声明都可用。在发现这一点后,我研究了一下,发现了一些关于这两者之间差异的堆栈溢出文章。 Microsoft命名空间的一个人mentioned here用于扩展处理Microsoft功能的标准命名空间,并会解释这一点。同样this answer对我来说也是最有意义的。