2011-01-05 31 views
9

Estoy cargando el token SAML del archivo XML.Lectura de los atributos SAML del token 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; 

¿Cómo puedo leer los atributos SAML de deserializedSaml?

Necesito valores de cadena para los atributos.

+0

realmente no hay necesidad de poner "C#" en el asunto, ya que lo tienes en las etiquetas. –

+0

es este SAML 1 o 2? La documentación de la clase 'System.IdentityModel' parece referirse a SAML 1.1 en lugar de 2. – Rory

+0

Ah, ahora veo .net 4.5 tiene clases nombradas como' Saml2XXX', p. 'Saml2Assertion' http://msdn.microsoft.com/en-us/library/microsoft.identitymodel.tokens.saml2.saml2assertion.aspx – Rory

Respuesta

9

¿Esto no funciona?

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