【问题标题】:how can i access the raw xml of the saml token?如何访问 saml 令牌的原始 xml?
【发布时间】:2017-10-17 04:38:47
【问题描述】:

我可以使用如下代码获取所有声明值:

ClaimsPrincipal cp = Thread.CurrentPrincipal as ClaimsPrincipal;            
ClaimsIdentity cid = (ClaimsIdentity)cp.Identity;

foreach (Claim claim in cid.Claims)
{
   ...
}

但我希望能够读取整个令牌,因为我怀疑我需要的一些信息位于令牌的非属性部分。 我已经阅读了有关安全令牌可视化器控件的信息,但我不能使用它,因为我不希望最终用户看到控件输出,而是希望通过电子邮件发送原始令牌 xml。

【问题讨论】:

    标签: c# saml wif ws-federation


    【解决方案1】:

    首先你必须配置WIF来保存“BootstrapContext”:

    <system.identityModel>
      <identityConfiguration saveBootstrapContext="true">
    

    然后您可以在“ClaimsIdentity”上使用属性“BootstrapContext”

    ClaimsPrincipal cp = Thread.CurrentPrincipal as ClaimsPrincipal;
    ClaimsIdentity cid = (ClaimsIdentity)cp.Identity; 
    BootstrapContext bc = cid.BootstrapContext as BootstrapContext;
    

    并使用 BootstrapContext 上的“令牌”属性来获取 saml 令牌的原始 xml。

    另见:

    https://msdn.microsoft.com/en-us/library/system.security.claims.claimsidentity.bootstrapcontext(v=vs.110).aspx

    https://msdn.microsoft.com/en-us/library/system.identitymodel.tokens.bootstrapcontext(v=vs.110).aspx

    http://www.cloudidentity.com/blog/2012/11/30/using-the-bootstrapcontext-property-in-net-4-5-2/

    【讨论】:

    • 我已经接受了答案。我得到一个 BootstrapContext 引用,但 .Token 属性为空。 .SecurityToken 属性看起来不错,但我需要 .Token 属性的原始文本。有什么建议吗?
    • 解决了! SecurityToken bootstrapToken = bc.SecurityToken;使用 (var wrtr = XmlWriter.Create(sbToken)) { new Saml2SecurityTokenHandler(new SamlSecurityTokenRequirement()).WriteToken(wrtr, bootstrapToken); } theXml = sbToken.ToString();
    猜你喜欢
    • 1970-01-01
    • 2016-06-26
    • 1970-01-01
    • 2011-04-25
    • 1970-01-01
    • 2021-02-16
    • 2017-05-09
    • 1970-01-01
    • 2016-10-05
    相关资源
    最近更新 更多