从SAML令牌中读取SAML属性

10

我正在从XML文件中加载SAML令牌。

string certificatePath = @"D:\Projects\SAMLDemo\Server.pfx";
X509Certificate2 cert = new X509Certificate2(certificatePath, "shani");

string samlFilePath = @"D:\Projects\SAMLDemo\saml.xml";
XmlReader reader = XmlReader.Create(samlFilePath);

List<SecurityToken> tokens = new List<SecurityToken>();
tokens.Add(new X509SecurityToken(cert));

SecurityTokenResolver outOfBandTokenResolver = SecurityTokenResolver.CreateDefaultSecurityTokenResolver(new ReadOnlyCollection<SecurityToken>(tokens), true);
SecurityToken securityToken = WSSecurityTokenSerializer.DefaultInstance.ReadToken(reader, outOfBandTokenResolver);

SamlSecurityToken deserializedSaml = securityToken as SamlSecurityToken;

我如何从反序列化的SAML中读取属性?

我需要属性的字符串值。


没有必要在主题行中加入"C#",因为你已经在标签中标明了。 - John Saunders
这是 SAML 1 还是 2?System.IdentityModel 类文档似乎涉及的是 SAML 1.1 而不是 2。 - Rory
啊,现在我看到了,.net 4.5 有一些类的名字是像 Saml2XXX 的,例如 Saml2Assertion http://msdn.microsoft.com/en-us/library/microsoft.identitymodel.tokens.saml2.saml2assertion.aspx - Rory
1个回答

9

这个不起作用吗?

foreach (SamlStatement statement in deserializedSaml.Assertion.Statements)
{
  SamlAttributeStatement attributeStatement = statement as SamlAttributeStatement;
  if (null != attributeStatement)
  {
    foreach (SamlAttribute attribute in attributeStatement.Attributes)
    {
      DoWhateverYouLikeWith(attribute);
    }
  }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接