2016-02-26 64 views
0

这个问题令我感到困惑。我似乎无法在用户通过身份验证后在我的C#代码中获得nameidentifier声明。这是一个.Net 4.5网站表单网站。我可以得到的唯一的索赔是http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameASP.Net和WIF只有一个索赔可用,但其他索赔显示在跟踪

这很容易遍历索赔,并且只有一个可用。清楚地看到跟踪名称标识符已经通过,但在遍历ClaimsPrincipal.Current.Claims或ClaimsPrincipal.Current.Identities []。Claims时,在我的代码中无法访问。

下面是跟踪示例:

<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="[email protected]"/> 
 
<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>

我的代码可以返回的身份和名字一个要求。但是包含电子邮件地址和名称标识的角色却不在那里。只有一个说法。

我已经经历了一切。我的应用程序只是简单的单页测试应用程序。没有什么有趣的,没有任何配置放弃一些索赔或任何东西

任何人都可以建议我应该看什么或我能做些什么来获得nameidentifier(或任何其他声明)。

我不明白他们为什么显示在跟踪中,但在我的应用程序中不可用。我认为他们只会在ClaimsPrincipal.Current或ClaimPrincipal.Current.Identities之一下提供的其中一个声明集合中(只有一个标识符与跟踪输出相匹配)。

任何帮助真的很感激我留下了一点头发!提前致谢。

+0

你的ADFS声明规则是什么样子的? – nzpcmad

+0

嗨,我会在哪里找到那些?我所配置的所有内容都是web.config,其中列出了三条未注释的声明,但WIF框架没有使用这些声明,我知道它们仅适用于fedutil工具。上面tracerecord中的信息显示了正在发送的内容。我不确定ADFS是否是身份验证服务器,因为它可能是别的。 –

+0

从跟踪。您需要查看ADFS端。您的索赔看起来不正确[email protected]不是它的电子邮件地址。 – nzpcmad

回答

1

我会回答我自己的问题。在这之后我没有留下太多头发。这个问题可能会把别人赶出去。

当使用下面的内容访问ClaimsPrincipal时,我使用的是System.IdentityModel命名空间。这只给了我一个索赔。尽管启用了WIF跟踪功能,我仍然可以看到更多的声明被提供给我的应用程序。

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

的解决方案是只使用替代以下几点:

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

这时,突然所有其他债权可用。在发现这一点之后,我进行了一些研究,并找到了关于这两者之间差异的一些堆栈溢出文章。一个人mentioned here微软命名空间是扩展标准命名空间,处理微软功能,并会解释这一点。此外,this answer对我来说也是最有意义的。