简体   繁体   English

在 C# .NET 4.5 中使用 SAML 2.0

[英]Working with SAML 2.0 in C# .NET 4.5

I am trying to use pure .NET (no external classes, controls, helpers) to create a SAML message.我正在尝试使用纯 .NET(没有外部类、控件、助手)来创建 SAML 消息。 I found some code on the interwebs;我在互联网上找到了一些代码; this is what I have:这就是我所拥有的:

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;

}

and here is the code I am using to get the XML:这是我用来获取 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
    );
}

This seemed like it was going to work.这似乎会奏效。 However, the message is produces is SAML version 1.0 - I need to work with 2.0.但是,生成的消息是 SAML 版本 1.0 - 我需要使用 2.0。

I know I can do some sloppy work and replace some values here and there and this system would work fine.我知道我可以做一些草率的工作并在这里和那里替换一些值,这个系统可以正常工作。 There are very little differences in the message, version being the most important.消息中的差异很小,版本是最重要的。 I am having a hard time finding information on SAML 2.0 for .NET.我很难找到有关 .NET 的 SAML 2.0 的信息。 I do know SAML 2.0 was implemented into .NET recently.我知道 SAML 2.0 最近已在 .NET 中实现。 I am using Framework 4.5 so I should have access to it.我正在使用 Framework 4.5,所以我应该可以访问它。 The MSDN page for SamlAssertion says the "majorVersion" is a constant, always set to '1'. SamlAssertion 的 MSDN 页面说“majorVersion”是一个常量,始终设置为“1”。

I'm guessing there is another namespace I could be working with, but I haven't found it.我猜我可以使用另一个命名空间,但我还没有找到它。 My requirement is just to get the XML SAML message.我的要求只是获取 XML SAML 消息。 I don't need to sign with X509, I don't need the token.我不需要用 X509 签名,我不需要令牌。 Just the SAML XML message.只是 SAML XML 消息。

Again, this is a question trying to find out how to do this in native .NET.同样,这是一个试图找出如何在本机 .NET 中执行此操作的问题。 I have found several SAML helpers and lots of code on how to build the message manually- I'm trying to find the CORRECT solution, if it exists.我找到了几个 SAML 助手和大量关于如何手动构建消息的代码——我正在尝试找到正确的解决方案(如果存在)。

EDIT: I have found I can use Saml2Assertion.编辑:我发现我可以使用 Saml2Assertion。 However, I am unable to find a way to get the SAML message written to xml now.但是,我现在无法找到将 SAML 消息写入 xml 的方法。

EDIT2: I have found how to write the Saml2Assersion object to xml. EDIT2:我找到了如何将 Saml2Assersion 对象写入 xml。 Sadly, it does not keep the SAML syntax, it writes in pure XML without <saml> tags.遗憾的是,它没有保留 SAML 语法,它以纯 XML 编写,没有<saml>标记。

.NET 4.5 has WIF (Windows Identity Foundation) built into it. .NET 4.5 内置了 WIF(Windows Identity Foundation)。 This now supports SAML 2.0.这现在支持 SAML 2.0。 To make use of SAML 2.0, just use .NET 4.5.要使用 SAML 2.0,只需使用 .NET 4.5。 The class name is Saml2XXXX (where XXXX is the token, assertion, serializer etc) Here is a link to SAML 2.0 Assertion: http://msdn.microsoft.com/en-us/library/microsoft.identitymodel.tokens.saml2.saml2assertion.aspx类名是 Saml2XXXX(其中 XXXX 是令牌、断言、序列化程序等)这是 SAML 2.0 断言的链接:http: //msdn.microsoft.com/en-us/library/microsoft.identitymodel.tokens.saml2。 saml2assertion.aspx

This will create a SAML 2.0 Assertion object.这将创建一个 SAML 2.0 断言对象。 To get the XML, this is the code I used:要获取 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);
        }
    }
}

This will serialize your assertion object into XML.这会将您的断言对象序列化为 XML。 This is where I ran into problems.这是我遇到问题的地方。 The XML is will create does NOT contain the saml namespace (eg <saml:Assertion> ).将创建的 XML 不包含 saml 命名空间(例如<saml:Assertion> )。 I was not able to find a solution for this, so a Replace("<", "<saml:") had to be used.我无法找到解决方案,因此必须使用Replace("<", "<saml:")

That's because Saml2Assertion refers to the token not the protocol.那是因为 Saml2Assertion 指的是令牌而不是协议。

The SAML token used in WIF is a 1.0 token. WIF 中使用的 SAML 令牌是 1.0 令牌。

There is no SAML 2 protocol support in .NET. .NET 中没有 SAML 2 协议支持。

There is a WIF CTP for SAML 2 but it hasn't been upgraded for ages. SAML 2 有一个 WIF CTP ,但它已经很久没有升级了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM