【问题标题】:Create SAML response from assertion in C# 4.5 (WIF)从 C# 4.5 (WIF) 中的断言创建 SAML 响应
【发布时间】:2017-09-15 15:19:10
【问题描述】:

我需要一种将<samlp:response> 消息发布到某个 URL 的方法,它相当简单,.NET 可以帮助我使用Saml2Assertion 类,但我似乎无法找到一种方法来将该断言包装在响应中并且是否已序列化(甚至无需手动发送即可发送)?

Saml2Assertion assert = new Saml2Assertion(new Saml2NameIdentifier("SAMLIssuer"));
assert.Subject = new Saml2Subject(new Saml2NameIdentifier("10001", new Uri("urn:oasis:names:tc:SAML:2.0:nameid-format:persistent")));
Saml2AuthenticationContext context = new Saml2AuthenticationContext(new Uri("urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport"));
assert.Statements.Add(new Saml2AuthenticationStatement(context, DateTime.Now));

string assertion;
using (var sw = new StringWriter())
{
    var xws = new XmlWriterSettings();
    using (var xw = XmlWriter.Create(sw, xws))
    {
        var handler = new Saml2SecurityTokenHandler();
        handler.WriteToken(xw, new Saml2SecurityToken(assert));
    }
    assertion = sw.ToString();
}

我为 assert 得到的 XML 看起来不错:

<?xml version="1.0" encoding="utf-16"?>
<Assertion ID="_fc348927-c0bf-4955-b98f-483043d8dedd" IssueInstant="2017-04-19T11:29:38.464Z" Version="2.0" xmlns="urn:oasis:names:tc:SAML:2.0:assertion">
    <Issuer>SAMLIssuer</Issuer>
    <Subject>
        <NameID Format="urn:oasis:names:tc:SAML:2.0:nameid-format:persistent">10001</NameID>
    </Subject>
    <AuthnStatement AuthnInstant="2017-04-19T11:29:39.040Z">
        <AuthnContext>
           <AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport</AuthnContextClassRef>
        </AuthnContext>
    </AuthnStatement>
</Assertion>

那么,现在呢?我如何从我的代码中获取:

<samlp:Response
    xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
    xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"
    ID="new id"
    InResponseTo="old id"
    Version="2.0"
    IssueInstant="2017-04-19T11:29:39.040Z"
    Destination="some url">
    <saml:Issuer>SAMLIssuer</saml:Issuer>
    <samlp:Status>
      <samlp:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/>
    </samlp:Status>
    <saml:Assertion ....

不使用外部库或制作我自己的包装器/字符串连接?我似乎在 .NET 4.5 的 WIF 实现中找不到任何可以帮助我的东西。

【问题讨论】:

    标签: c# .net-4.5 saml-2.0 wif


    【解决方案1】:

    AFAIK .NET 没有对 SAMLp 协议的内置支持。它仅支持 WsFederation。也看看这里:How should I implement SAMLP 2.0 in an ASP.NET MVC 4 service provider?

    【讨论】:

    • 笨蛋,看来你是对的,所以指向你。我越来越不喜欢这个新的 MS。 :(
    猜你喜欢
    • 2015-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    相关资源
    最近更新 更多