【问题标题】:Adding new authentication schemes dynamically动态添加新的身份验证方案
【发布时间】:2018-08-01 06:42:45
【问题描述】:

我正在为我们需要做出的一些身份验证/授权讨论/决策构建一个 ASP.Net Core 2 PoC。

我目前处于一个用户刚刚定义了该应用程序想要支持的新 OpenID 提供程序的地步。

支持这一点的一种方法是在启动期间读取所有已配置的提供程序,并将它们全部配置在 ConfigureServices 中。但是有一些诱人的线索表明,也可以这样做而无需终止并重新启动应用程序。

IAuthenticationSchemeProvider 有一个看起来很理想的AddScheme 方法。现在我需要做的就是构造一个AuthenticationScheme 对象,我很幸运。它有一个构造函数 AuthenticationScheme(string name, string displayName, Type handlerType) 但我不确定如何正确使用来自 Microsoft.AspNetCore.Authentication.OpenIdConnect 的类型来正确构造此对象并允许我为此指定 OpenID Connect 特定选项。

我认为我想用于第三个参数的类型是 OpenIdConnectHandler 。但是我该如何处理我的选择呢? (或者在替代方案中 - 我如何做相当于能够提供 Action<OpenIdConnectOptions> 代表)


我发现 this github issue 也很有趣(没有 TryAddScheme 方法,因此可能存在例外情况,如果我们选择进一步研究这个 PoC,那么在摘要中很有趣)但是小样本根本没有谈论选项.

【问题讨论】:

  • 当你像往常一样注册 open id connect 时(通过AddOpenIdConnect()) - 一堆东西被添加到 DI 容器中。这一堆东西,除其他重要的东西外,还包括选项(通过IConfigureOptions<OpenIdConnectOptions)。然后将这些选项注入到 openidconnect 处理程序的构造函数中。所以要在运行时添加它,我认为你需要修改 DI 容器,这不是很好,但应该付出一些努力。
  • 澄清一下上面的评论:你传递给认证方案构造函数的handlerType将用于解析来自DI容器的处理程序。因此,处理程序本身及其依赖项(包括选项)也应该在容器中注册。所以仅仅给IAuthenticationSchemeProvider 添加scheme是不够的。
  • 如果你有兴趣,我开发了一个包来做到这一点:github.com/aguacongas/DymamicAuthProviders
  • @aguafrommars 您的包是否需要在每次添加或修改新配置时重新启动应用程序?
  • @DejanBogatinovski 不,我们的想法是不必重新启动应用程序。

标签: c# asp.net-core openid-connect


【解决方案1】:

这里有一个如何执行此操作的示例 - https://github.com/aspnet/AuthSamples/tree/master/samples/DynamicSchemes

请记住,对于 OAuth 方案,您需要做的不仅仅是调用 schemeProvider.AddSchemeoptionsCache.TryAdd - 通过常规方法添加选项时还有一个“后配置”步骤。这是课程 - https://github.com/aspnet/Security/blob/master/src/Microsoft.AspNetCore.Authentication.OAuth/OAuthPostConfigureOptions.cs

因此,您可以将类型 OAuthPostConfigureOptions<OAuthOptions, OAuthHandler<OAuthOptions>> 注册到您的 DI 容器中,然后通过构造函数获取它并在您的选项上调用 OAuthPostConfigureOptions.PostConfigure,然后将选项添加到 optionsCache

【讨论】:

    【解决方案2】:

    为了扩展上面的答案(https://stackoverflow.com/a/49825151/2200690),除了https://github.com/aspnet/AspNetCore/blob/release/2.2/src/Security/samples/DynamicSchemes/Controllers/AuthController.cs中的schemeProvider.AddScheme和optionsCache.TryAdd,我们还需要在Startup.cs和AuthController.cs中做进一步的修改:

    在 Startup.cs 中

    public void ConfigureServices(IServiceCollection services)
    {
        ...
        // register to the DI container so it can be injected in the AuthController constructor.
        services.AddSingleton<OpenIdConnectPostConfigureOptions>();
        ...
    }
    

    在 AuthController.cs 中

    在 AuthController 构造函数中注入OpenIdConnectPostConfigureOptions

    public AuthController(IAuthenticationSchemeProvider schemeProvider, 
        IOptionsMonitorCache<OpenIdConnectOptions> optionsCache,
        OpenIdConnectPostConfigureOptions postConfigureOptions)
    {
        _schemeProvider = schemeProvider;
        _optionsCache = optionsCache;
        _postConfigureOptions = postConfigureOptions;
    }
    

    在 AuthController.cs 的 AddOrUpdate 方法中,创建一个 OpenIdConnectOptions 类的实例,然后在后期配置步骤中使用它:

    var options = new OpenIdConnectOptions
    {
        Authority = "xxx-endpoint",
        CallbackPath = "/signin-oidc",
        ClientId = "XXX",
        ClientSecret = "XXX",
        ......
    };
    _postConfigureOptions.PostConfigure("oidc", options);                
    _optionsCache.TryAdd("oidc", options);
    

    【讨论】:

    • 我在以前的工作场所做过,但现在无法使用它了。
    • 看起来类似的方法适用于 Sustainsys.Saml2.AspNetCore2,它们有 PostConfigureSaml2Options
    猜你喜欢
    • 2020-10-13
    • 1970-01-01
    • 2021-04-28
    • 1970-01-01
    • 1970-01-01
    • 2015-11-21
    • 2022-01-06
    • 2018-02-23
    • 1970-01-01
    相关资源
    最近更新 更多