首页 > 解决方案 > 如何从 Azure 函数发出 IdP SAML 请求?

问题描述

我有一个简单的 C# Web 应用程序作为一系列 Azure Functions 实现。我希望能够将用户重定向到外部网站,当他们单击我的应用程序中的按钮时使用 SSO 登录。外部网站是 Absorb 的一个实例。Absorb提供了通过 SSO 使用商业产品ComponentSpace库来完成此操作的指南。

我想使用ITfoxtec SAML 2.0库,因为它是免费和开源的(尽管任何免费/开源库都可以)。

我的问题如下:

  1. 如何将 Saml2Assertion 添加到 Saml2AuthnResponse?
  2. 如何使用 redirectBinding 返回 HttpResponseMessage?

这是我到目前为止所拥有的:

[FunctionName("GoToAbsorb")]
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)]HttpRequestMessage request, TraceWriter log)
{
    //  User is authenticated and here.
    

    //  SAML part starts here.
    Saml2Configuration configuration = new Saml2Configuration();
    configuration.SigningCertificate = GetCertificate(); // Returns an X509 Certificate
    configuration.AllowedAudienceUris.Add(configuration.Issuer);

    Saml2AuthnResponse samlResponse = new Saml2AuthnResponse(configuration);
    samlResponse.Destination = new Uri("https://sample.myabsorb.com/account/saml");
    samlResponse.Issuer = Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME");
    samlResponse.Status = ITfoxtec.Identity.Saml2.Schemas.Saml2StatusCodes.Success;

    Saml2Subject subject = new Saml2Subject(new Saml2NameIdentifier("Id"));
    subject.SubjectConfirmations.Add(new Saml2SubjectConfirmation(new Uri("https://sample.myabsorb.com/account/saml")));

    var assertion = new Saml2Assertion(new Saml2NameIdentifier(Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME")));
    assertion.Conditions = new Saml2Conditions()
    {
        NotBefore = DateTime.Now,
        NotOnOrAfter = DateTime.Now.AddMinutes(3),
    };
    assertion.Statements.Add(new Saml2AuthenticationStatement(new Saml2AuthenticationContext()
    {
        //  There does not appear to be an equivalent to the following line
        //  AuthnContextClassRef = new AuthnContextClassRef(SAMLIdentifiers.AuthnContextClasses.Password)
    }));


    //  How do I add the assertion to the response?
                

    Saml2RedirectBinding redirectBinding = new Saml2RedirectBinding();
    redirectBinding.Bind(samlResponse);
    
    //  What do I return as the response message?
    return new HttpResponseMessage(...);
}

标签: itfoxtec-identity-saml2

解决方案


我认为不可能使用 SAML 2.0 保护 Azure 函数。SAML 2.0 用于 Web 应用程序,而不是 API。您可以通过发送 SAML 2.0 令牌作为 http 标头的一部分来保护 API,并在 API 中验证 SAML 2.0 令牌。


推荐阅读