Sustainsys没有标识的ASP.NET Core WebAPI的SAML2示例

问题描述 投票:0回答:1

有没有人为Sustainsys Saml2库提供ASP.NET Core WebAPI项目(没有Mvc)的工作样本,没有ASP身份更重要的是什么? github上提供的示例强烈依赖于我不需要也不想使用的MVC和SignInManager。

我添加了Saml2身份验证,并且最初它与我的IdP(我还检查了Sustainsys提供的StubIdP)一起使用了前几步,所以:

  • IdP元数据得到正确加载
  • 我的API正确重定向到登录页面
  • 登录页面重定向到/ Saml2 / Acs页面,我在日志中看到它成功解析了结果

但是,我不知道如何从那里向前推进并提取用户登录和其他声明(我的IdP也提供了一封电子邮件,它包含在我在日志中确认的SAML响应中)。

在Web上找到一些样本并从GitHub稍微修改一下MVC Sample我做了以下事情:

在Startup.cs中:

...
.AddSaml2(Saml2Defaults.Scheme,
                       options =>
                       {
                           options.SPOptions.EntityId = new EntityId("...");
                           options.SPOptions.ServiceCertificates.Add(...));
                           options.SPOptions.Logger = new SerilogSaml2Adapter();
                           options.SPOptions.ReturnUrl = new Uri(Culture.Invariant($"https://localhost:44364/Account/Callback?returnUrl=%2F"));

                           var idp =
                               new IdentityProvider(new EntityId("..."), options.SPOptions)
                               {
                                   LoadMetadata = true,
                                   AllowUnsolicitedAuthnResponse = true, // At first /Saml2/Acs page throwed an exception that response was unsolicited so I set it to true
                                   MetadataLocation = "...",
                                   SingleSignOnServiceUrl = new Uri("...") // I need to set it explicitly because my IdP returns different url in the metadata
                               };
                           options.IdentityProviders.Add(idp);
                       });

在AccountContoller.cs中(我试图遵循how to implement google login in .net core without an entityframework provider描述的类似情况):

[Route("[controller]")]
[ApiController]
public class AccountController : ControllerBase
{
    private readonly ILog _log;

    public AccountController(ILog log)
    {
        _log = log;
    }

    [HttpGet("Login")]
    [AllowAnonymous]
    public IActionResult Login(string returnUrl)
    {
        return new ChallengeResult(
            Saml2Defaults.Scheme,
            new AuthenticationProperties
            {
                // It looks like this parameter is ignored, so I set ReturnUrl in Startup.cs
                RedirectUri = Url.Action(nameof(LoginCallback), new { returnUrl })
            });
    }

    [HttpGet("Callback")]
    [AllowAnonymous]
    public async Task<IActionResult> LoginCallback(string returnUrl)
    {

        var authenticateResult = await HttpContext.AuthenticateAsync(Constants.Auth.Schema.External);

        _log.Information("Authenticate result: {@authenticateResult}", authenticateResult);

// I get false here and no information on claims etc.
        if (!authenticateResult.Succeeded)
        {
            return Unauthorized();
        }

// HttpContext.User does not contain any data either


// code below is not executed
        var claimsIdentity = new ClaimsIdentity(Constants.Auth.Schema.Application);
claimsIdentity.AddClaim(authenticateResult.Principal.FindFirst(ClaimTypes.NameIdentifier));

        _log.Information("Logged in user with following claims: {@Claims}", authenticateResult.Principal.Claims);           

        await HttpContext.SignInAsync(Constants.Auth.Schema.Application, new ClaimsPrincipal(claimsIdentity));

        return LocalRedirect(returnUrl);
    }

TLDR:我的ASP.NET Core WebApi项目中的SAML配置看起来很好,我通过日志中检查的正确声明得到了成功响应。我不知道如何提取这些数据(返回url是错误的,或者我的回调方法应该以不同的方式工作)。此外,令人费解的是,为什么从SSO登录页面成功重定向被视为“未经请求”,这可能是问题所在?

谢谢你的帮助

docker asp.net-core asp.net-core-webapi sustainsys-saml2
1个回答
0
投票

事实证明,我得到的各种错误是由于我的解决方案被托管在容器内。这导致内部aspnet钥匙串出现一点故障。更多细节可以在这里找到(几乎在本文末尾提到了docker):

https://docs.microsoft.com/en-us/aspnet/core/security/data-protection/configuration/overview?tabs=aspnetcore2x&view=aspnetcore-2.2

简而言之,对于要运行的代码,我只需要添加以下行:

services.AddDataProtection()
        .PersistKeysToFileSystem(new DirectoryInfo("/some/volume/outside/docker")); // it needs to be outside container, even better if it's in redis or other common resource

它修复了一切,包括:

  • 对外部cookie的登录操作
  • 未经请求的SSO电话
  • 数据保护密钥链的例外情况

所以很难找到,因为代码抛出的异常没有指出发生了什么(并且未经请求的SSO调用让我认为SSO提供程序配置错误)。只有当我拆开Saml2软件包并逐个尝试各种代码时,我终于遇到了适当的异常(关于密钥链),这使得我转向了一篇关于aspnet数据保护的文章。

我提供这个答案,以便它可以帮助某人,并为正确的观众添加了docker标签。

© www.soinside.com 2019 - 2024. All rights reserved.