微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从SAML响应中获取所有属性和值?

如何解决如何从SAML响应中获取所有属性和值?

我已经使用Azure ADFS实现了SSO,并使用了SAML,以下是我来自Azure的SAML响应。

<saml2:AttributeStatement xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion">
<saml2:Attribute Name="User.email" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
    <saml2:AttributeValue 
        xmlns:xs="http://www.w3.org/2001/XMLSchema" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">Rudresh@gmail.com
    </saml2:AttributeValue></saml2:Attribute>
    
<saml2:Attribute Name="emailaddress" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
    <saml2:AttributeValue 
        xmlns:xs="http://www.w3.org/2001/XMLSchema" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">rudresh@google.com
    </saml2:AttributeValue></saml2:Attribute>
</saml2:AttributeStatement>

我想从上述SAML响应中提取所有属性和值,如下所示。

User.Email = 'Rudresh@gmail'
emailAddress = 'rudresh@google.com'

解决方法

您可以将响应转换为xml对象,然后将值映射到所需的属性。

总而言之,一旦XmlSerializer完成并将响应转换为对象,
使用Linq,我们可以找到FirstOrDefault emailaddressUser.email属性,并将它们分配给EmailAddress和{ {1}}属性全部在对象构造函数中。

下面是在控制台应用程序中如何使用它的示例。

Email

这是响应类。

// Your above payload.
string examplePayload = System.IO.File.ReadAllText("TextFile1.txt");

// Creating the saml response.
SamlResponse samlResponse = new SamlResponse(examplePayload);

// writing the output,rudresh@google.com
Console.WriteLine(samlResponse.EmailAddress);
,

您如何实施SAML?您使用堆栈了吗?

最简单的方法是使用SAML library,它将为您完成所有工作-并检查签名等。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。