Working with SAML 2.0 in C# .NET 4.5

后端 未结 2 1224
醉话见心
醉话见心 2020-12-02 09:13

I am trying to use pure .NET (no external classes, controls, helpers) to create a SAML message. I found some code on the interwebs; this is what I have:

priv         


        
相关标签:
2条回答
  • 2020-12-02 09:57

    .NET 4.5 has WIF (Windows Identity Foundation) built into it. This now supports SAML 2.0. To make use of SAML 2.0, just use .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

    This will create a SAML 2.0 Assertion object. To get the XML, this is the code I used:

    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. This is where I ran into problems. The XML is will create does NOT contain the saml namespace (e.g. <saml:Assertion>). I was not able to find a solution for this, so a Replace("<", "<saml:") had to be used.

    0 讨论(0)
  • 2020-12-02 10:03

    That's because Saml2Assertion refers to the token not the protocol.

    The SAML token used in WIF is a 1.0 token.

    There is no SAML 2 protocol support in .NET.

    There is a WIF CTP for SAML 2 but it hasn't been upgraded for ages.

    0 讨论(0)
提交回复
热议问题