Question

I am loading SAML Token from XML file.

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;

How can I read the SAML attributes from deserializedSaml ?

I need string values for the attributes.

Was it helpful?

Solution

Doesn't this work?

foreach (SamlStatement statement in deserializedSaml.Assertion.Statements)
{
  SamlAttributeStatement attributeStatement = statement as SamlAttributeStatement;
  if (null != attributeStatement)
  {
    foreach (SamlAttribute attribute in attributeStatement.Attributes)
    {
      DoWhateverYouLikeWith(attribute);
    }
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top