c# - 如何从 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 emailaddress
和User.email
Attribute 并将它们分配给对象构造函数中的所有属性EmailAddress
。这是一个如何在控制台应用程序中使用它的示例。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);
这是响应类。
public class SamlResponse
{
public string EmailAddress { get; }
public string Email { get; }
public SamlResponse(string saml2)
{
if (string.IsNullOrEmpty(saml2)) throw new ArgumentNullException(nameof(saml2));
using (MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(saml2)))
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(AttributeStatement));
AttributeStatement response = (AttributeStatement)xmlSerializer.Deserialize(ms);
EmailAddress = response.Attribute.FirstOrDefault(x => x.Name == "emailaddress").AttributeValue?.TrimEnd();
Email = response.Attribute.FirstOrDefault(x => x.Name == "User.email").AttributeValue?.TrimEnd();
}
}
/// <remarks/>
[Serializable()]
[System.ComponentModel.DesignerCategory("code")]
[XmlType(AnonymousType = true, Namespace = "urn:oasis:names:tc:SAML:2.0:assertion")]
[XmlRoot(Namespace = "urn:oasis:names:tc:SAML:2.0:assertion", IsNullable = false)]
public partial class AttributeStatement
{
private AttributeStatementAttribute[] attributeField;
/// <remarks/>
[XmlElement("Attribute")]
public AttributeStatementAttribute[] Attribute
{
get
{
return this.attributeField;
}
set
{
this.attributeField = value;
}
}
}
/// <remarks/>
[Serializable()]
[System.ComponentModel.DesignerCategory("code")]
[XmlType(AnonymousType = true, Namespace = "urn:oasis:names:tc:SAML:2.0:assertion")]
public partial class AttributeStatementAttribute
{
private string attributeValueField;
private string nameField;
private string nameFormatField;
/// <remarks/>
public string AttributeValue
{
get
{
return this.attributeValueField;
}
set
{
this.attributeValueField = value;
}
}
/// <remarks/>
[XmlAttribute()]
public string Name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
/// <remarks/>
[XmlAttribute()]
public string NameFormat
{
get
{
return this.nameFormatField;
}
set
{
this.nameFormatField = value;
}
}
}
}
推荐阅读
- javascript - 即使我已经登录,也出现角度错误 401
- javascript - 如何在 html 和 JavaScript 中制作星级?
- redirect - Netlify 重写,为什么 url 不一样?
- reactjs - React 大日历重命名启动访问器
- laravel-8 - 如何使用 KnpLabs/php-github-api v3 客户端通过搜索 api 获取 github 用户的详细信息
- java - 如何模拟类似于 InputStream.read() 的方法块,直到输入数据可用?
- date - 以哪种格式给出日期?
- javascript - 公式为
svg中的动态间距 - r - 简单的功能 SF,循环一列作为填充 ggplot2 图
- flutter - Flutter中如何从用户ID中获取特定的用户详细信息