【问题标题】:SAML2 Idp Metadata generation with .Net Framework使用 .Net Framework 生成 SAML2 Idp 元数据
【发布时间】:2021-07-05 04:35:47
【问题描述】:

用于 SAML2 断言的 Net Framework 库。我能够使用 .net 库创建 SAML2 xml。但我现在的要求是生成该 SAML2 断言的元数据,以提供给客户进行开发。是否有任何 .net 框架方法可以从 SAML 断言对象生成 SAML2 元数据?

我使用 System.IdentityModel.Tokens Saml2Assertion() 类进行 SAML 断言。 我知道有一个 .net 库来创建元数据,即 System.IdentityModel.Metadata 并从该库创建 EntityDescriptor。但我也不想手动创建元数据。理想情况下,根据我的理解,应该从 SAML 断言 xml/object 生成 SAML2 元数据,而无需任何手动干预。只是想检查是否有任何方法可以使用任何 .net 库,因为我无法在任何地方进行处理。

我正在使用 .net 框架 - 4.8 断言代码:

private static Saml2Assertion createSamlAssertion()
    {
        // Here we create some SAML assertion with ID and Issuer name. 
        Saml2NameIdentifier nameidentifier = new Saml2NameIdentifier("XXX");
        Saml2Assertion assertion = new Saml2Assertion(nameidentifier);
        assertionid = "SamlAssertion-" + Guid.NewGuid().ToString();
        assertion.Id = new Saml2Id(assertionid);            
        assertion.Issuer = new Saml2NameIdentifier("XXXX");
        assertion.IssueInstant = Framework.ApplicationTime.GetCurrentTime();

        // Create some SAML subject. 

        Saml2SubjectConfirmation subcon = new Saml2SubjectConfirmation(new Uri("urn:oasis:names:tc:SAML:2.0:cm:bearer"));
        subcon.Method = new Uri("urn:oasis:names:tc:SAML:2.0:cm:bearer");
        subcon.NameIdentifier = new Saml2NameIdentifier("XXXX");
        Saml2SubjectConfirmationData subcondata = new Saml2SubjectConfirmationData();
        subcondata.NotBefore = GetCurrentTime();
        subcondata.NotOnOrAfter = GetCurrentTime().AddMinutes(60);
        subcon.SubjectConfirmationData = subcondata;
        Saml2Subject samlSubject = new Saml2Subject(subcon);
        
        samlSubject.NameId = new Saml2NameIdentifier("XXXX");


        assertion.Subject = samlSubject;

        // 
        // Create one SAML attribute with few values. 

        // Now create the SAML statement containing one attribute and one subject. 
        Saml2AttributeStatement samlAttributeStatement = new Saml2AttributeStatement();
        samlAttributeStatement.Attributes.Add(attr);

        attr = new Saml2Attribute("First Name");
        attr.Values.Add("JOHN");
        attr.Name = "First Name";
        attr.NameFormat = new Uri("urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified");
        samlAttributeStatement.Attributes.Add(attr);

        attr = new Saml2Attribute("Name");
        attr.Values.Add("");
        attr.Name = "Name";
        attr.NameFormat = new Uri("urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified");
        samlAttributeStatement.Attributes.Add(attr);


        attr = new Saml2Attribute("Street Name 1");
        attr.Values.Add("35 MAIN ST");
        attr.Name = "Street Name 1";
        attr.NameFormat = new Uri("urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified");
        samlAttributeStatement.Attributes.Add(attr);

        attr = new Saml2Attribute("Street Name 2");
        attr.Values.Add("APT 204");
        attr.Name = "Street Name 2";
        attr.NameFormat = new Uri("urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified");
        samlAttributeStatement.Attributes.Add(attr);

        attr = new Saml2Attribute("City Name");
        attr.Values.Add("LEXINGTON");
        attr.Name = "City Name";
        attr.NameFormat = new Uri("urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified");
        samlAttributeStatement.Attributes.Add(attr);


        attr = new Saml2Attribute("Zip Code");
        attr.Values.Add("405117883");
        attr.Name = "Zip Code";
        attr.NameFormat = new Uri("urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified");
        samlAttributeStatement.Attributes.Add(attr);

        attr = new Saml2Attribute("Contact Email Address");
        attr.Values.Add("john.doe@email.com");
        attr.Name = "Contact Email Address";
        attr.NameFormat = new Uri("urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified");
        samlAttributeStatement.Attributes.Add(attr);

        attributes = samlAttributeStatement.Attributes;

        // Append the statement to the SAML assertion. 
        assertion.Statements.Add(samlAttributeStatement);

        Saml2AuthenticationContext authcon = new Saml2AuthenticationContext();
        authcon.ClassReference = new Uri("urn:oasis:names:tc:SAML:2.0:ac:classes:Password");
        Saml2AuthenticationStatement auth = new Saml2AuthenticationStatement(authcon);
        auth.AuthenticationInstant = GetCurrentTime();

        assertion.Statements.Add(auth);

        return assertion;           

    } 

【问题讨论】:

  • 您想要断言或元数据?这些是不同的概念。
  • 我希望从断言中生成元数据。我已经有了断言 xml。
  • 您是指 SAML 令牌还是 metadata?元数据与断言无关。相反,它只描述了您的身份提供者(或服务提供者)所做的事情(例如“我是身份提供者,这些是我的服务,这些是我的端点”)。我的猜测是您需要一个用于断言的 SAML 令牌,而不是元数据,但由于某种原因,您将 SAML 令牌称为“元数据”。是这样吗?

标签: c# .net metadata saml saml-2.0


【解决方案1】:

某些 SAML client-side libraries 会这样做。

我已经使用 Sustainsys 和 ComponentSpace 来生成元数据。

【讨论】:

  • 前段时间我发布了另一个参赛者,OldMusicBox.Saml2(MIT 许可证)。支持基本的客户端流程。
  • 我是否可以使用 Sustainsys(或任何其他开源库)为我使用 .net 框架创建的 SAML 断言生成元数据?如果是的话..你能指点我参考的代码吗。
  • 代码和文档在我给的参考文献里。
  • 很好@WiktorZychla 我会把它添加到列表中。
  • @rbrayb 你在说哪个参考?
猜你喜欢
  • 2016-09-01
  • 2014-01-12
  • 1970-01-01
  • 2016-02-06
  • 2021-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-11
相关资源
最近更新 更多