10

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Shani
  • 447
  • 5
  • 18
  • there is really no need to put "C#" in the subject line, since you've got it in the tags. – John Saunders Jan 05 '11 at 02:50
  • is this SAML 1 or 2? The `System.IdentityModel` class documentation seems to refer to SAML 1.1 rather than 2. – Rory Feb 12 '16 at 23:15
  • Ah, now I see .net 4.5 has classes named like `Saml2XXX`, e.g. `Saml2Assertion` http://msdn.microsoft.com/en-us/library/microsoft.identitymodel.tokens.saml2.saml2assertion.aspx – Rory Feb 12 '16 at 23:43

1 Answers1

9

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);
    }
  }
}
Chris Dickson
  • 11,964
  • 1
  • 39
  • 60