2011-01-05 116 views
9

我從XML文件加載SAML令牌。從SAML令牌讀取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; 

如何從deserializedSaml讀取SAML屬性?

我需要屬性的字符串值。

+0

確實沒有必要在主題行中放置「C#」,因爲您已將它放在標記中。 – 2011-01-05 02:50:38

+0

這是SAML 1還是2? 'System.IdentityModel'類文檔似乎是指SAML 1.1而不是2。 – Rory 2016-02-12 23:15:32

+0

啊,現在我明白了.net 4.5有類似於'Saml2XXX'的類,例如, 'Saml2Assertion' http://msdn.microsoft.com/en-us/library/microsoft.identitymodel.tokens.saml2.saml2assertion.aspx – Rory 2016-02-12 23:43:27

回答

9

這不工作?

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