Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading SAML Attributes from SAML Token

Tags:

c#

wcf

saml

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.

like image 582
Shani Avatar asked Jan 05 '11 02:01

Shani


People also ask

What are the contents of a SAML token?

The SAML token is signed with a certificate associated with the security token service and contains a proof key encrypted for the target service. The client also receives a copy of the proof key.

What are SAML attributes?

An attribute is a characteristic or trait of an entity that describes the entity. It is a name:value pair. The attributes included in the SAML assertion correspond to certain attributes of the service provider to: Convey user information from Verify to the service provider .


1 Answers

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);
    }
  }
}
like image 84
Chris Dickson Avatar answered Sep 19 '22 10:09

Chris Dickson