在C#.NET 4.5中使用SAML 2.0

bug*_*ker 34 .net c# saml saml-2.0

我正在尝试使用纯.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;

}
Run Code Online (Sandbox Code Playgroud)

这是我用来获取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
    );
}
Run Code Online (Sandbox Code Playgroud)

这看起来好像会起作用.但是,生成的消息是SAML版本1.0 - 我需要使用2.0.

我知道我可以做一些草率的工作,并在这里和那里替换一些值,这个系统可以正常工作.消息中的差异很小,版本是最重要的.我很难找到有关SAML 2.0 for .NET的信息.我知道最近在.NET中实现了SAML 2.0.我正在使用Framework 4.5,所以我应该可以访问它.SamlAssertion的MSDN页面说"majorVersion"是一个常量,总是设置为'1'.

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

同样,这是一个试图找出如何在本机.NET中执行此操作的问题.我已经找到了几个SAML助手和许多关于如何手动构建消息的代码 - 我正在尝试找到CORRECT解决方案,如果它存在的话.

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

EDIT2:我已经找到了如何将Saml2Assersion对象写入xml.遗憾的是,它没有保留SAML语法,它在没有<saml>标签的纯XML中编写.

bug*_*ker 34

.NET 4.5内置了WIF(Windows Identity Foundation).这现在支持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 Assertion对象.要获取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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这会将断言对象序列化为XML.这是我遇到问题的地方.XML将创建不包含saml命名空间(例如<saml:Assertion>).我无法找到解决方案,因此Replace("<", "<saml:")必须使用.

  • 只是为了制作`WriteAssertion`方法而不是继承`Saml2SecurityTokenHandler`,你可以像这样使用`WriteToken`:`handler.WriteToken(writer,new Saml2SecurityToken(assertion));` (6认同)