在c# . net 4.5中使用SAML 2.0

本文关键字:SAML net | 更新日期: 2023-09-27 18:04:50

我试图使用纯。net(没有外部类,控件,帮助器)来创建SAML消息。我在网上找到了一些代码;这是我的:

private static SamlAssertion createSamlAssertion()
{
    // Here we create some SAML assertion with ID and Issuer name. 
    SamlAssertion assertion = new SamlAssertion();
    assertion.AssertionId = "AssertionID";
    assertion.Issuer = "ISSUER";
    // Create some SAML subject. 
   SamlSubject samlSubject = new SamlSubject();
    samlSubject.Name = "My Subject";
    // 
    // Create one SAML attribute with few values. 
    SamlAttribute attr = new SamlAttribute();
    attr.Namespace = "http://daenet.eu/saml";
    attr.AttributeValues.Add("Some Value 1");
    //attr.AttributeValues.Add("Some Value 2");
    attr.Name = "My ATTR Value";
    // 
    // Now create the SAML statement containing one attribute and one subject. 
    SamlAttributeStatement samlAttributeStatement = new SamlAttributeStatement();
    samlAttributeStatement.Attributes.Add(attr);
    samlAttributeStatement.SamlSubject = samlSubject;
    // Append the statement to the SAML assertion. 
    assertion.Statements.Add(samlAttributeStatement);
    //return assertion
    return assertion;
}

,这里是我用来获取XML的代码:

var sb = new StringBuilder();
var settings = new XmlWriterSettings
{
    OmitXmlDeclaration = true,
    Encoding = Encoding.UTF8
};
using (var stringWriter = new StringWriter(sb))
using (var xmlWriter = XmlWriter.Create(stringWriter, settings))
using (var dictionaryWriter = XmlDictionaryWriter.CreateDictionaryWriter(xmlWriter))
{
    var samlAssertSerializer = new SamlSerializer();
    var secTokenSerializer = new WSSecurityTokenSerializer();
    assertion.WriteXml(
        dictionaryWriter,
        samlAssertSerializer,
        secTokenSerializer
    );
}

这似乎是要工作。但是,它生成的消息是SAML 1.0版本——我需要使用2.0版本。

我知道我可以做一些草率的工作,在这里和那里替换一些值,这个系统会很好地工作。信息上的差别很小,版本是最重要的。我很难找到关于。net的SAML 2.0的信息。我知道SAML 2.0最近在。net中实现了。我正在使用框架4.5,所以我应该可以访问它。SamlAssertion的MSDN页面说"majorVersion"是一个常量,总是设置为"1"。

我猜有另一个名称空间我可以工作,但我还没有找到它。我的要求只是获取XML SAML消息。我不需要用X509签名,我不需要令牌。只是SAML XML消息。

同样,这是一个试图找出如何在原生。net中做到这一点的问题。我已经找到了几个SAML助手和大量关于如何手动构建消息的代码-我正在试图找到正确的解决方案,如果它存在的话。

编辑:我发现我可以使用Saml2Assertion。但是,我现在无法找到一种方法来将SAML消息写入xml。

EDIT2:我已经找到了如何编写Saml2Assersion对象到xml。遗憾的是,它没有保留SAML语法,它使用纯XML编写,没有<saml>标记。

在c# . net 4.5中使用SAML 2.0

。. NET 4.5内置了WIF (Windows身份基础)。它现在支持SAML 2.0。要使用SAML 2.0,只需使用。net 4.5。类名是Saml2XXXX(其中XXXX是令牌、断言、序列化器等)。下面是SAML 2.0断言的链接:http://msdn.microsoft.com/en-us/library/microsoft.identitymodel.tokens.saml2.saml2assertion.aspx

这将创建一个SAML 2.0断言对象。要获取XML,下面是我使用的代码:

using System.Xml;
using System.IdentityModel.Tokens;
namespace YOUR.SPACE
{
    public class Saml2Serializer : Saml2SecurityTokenHandler
    {
        public Saml2Serializer()
        {
            Configuration = new SecurityTokenHandlerConfiguration()
                {
                };
        }
        public void WriteSaml2Assertion(XmlWriter writer, Saml2Assertion data)
        {
            base.WriteAssertion(writer, data);
        }
    }
}

这将把断言对象序列化为XML。这就是我遇到问题的地方。它将创建的XML不包含小名称空间(例如<saml:Assertion>)。我无法找到解决方案,所以必须使用Replace("<", "<saml:")

这是因为Saml2Assertion指的是令牌而不是协议。

WIF中使用的SAML令牌是1.0令牌。

. net中不支持saml2协议。

SAML 2有一个WIF CTP,但它已经很长时间没有升级了。