Dam*_*ver 9 c# openid-connect asp.net-core
我正在构建一个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>
代表)
我发现这个github问题也很有意义(没有TryAddScheme
方法,所以异常是可能的,如果我们选择进一步说服这个PoC那么抽象有趣)但是小样本根本没有讨论选项.
这里有一个如何执行此操作的示例 - https://github.com/aspnet/AuthSamples/tree/master/samples/DynamicSchemes
请记住,对于OAuth方案,您必须执行更多操作,然后只需调用schemeProvider.AddScheme和optionsCache.TryAdd - 在通过常规方法添加选项时,还有一个"postconfigure"步骤.这是类 - https://github.com/aspnet/Security/blob/master/src/Microsoft.AspNetCore.Authentication.OAuth/OAuthPostConfigureOptions.cs
因此,您可以在DI容器中注册"OAuthPostConfigureOptions>"类型,然后通过构造函数获取它,并在将选项添加到optionsCache之前调用选项上的OAuthPostConfigureOptions.PostConfigure.
要在上面的回答(扩大/sf/answers/3487760601/),除了schemeProvider.AddScheme和optionsCache.TryAdd在https://github.com/aspnet/AspNetCore/blob/release/ 2.2/src/Security/samples/DynamicSchemes/Controllers/AuthController.cs,我们需要在 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>();
...
}
Run Code Online (Sandbox Code Playgroud)
在 AuthController.cs 中
OpenIdConnectPostConfigureOptions
在 AuthController 构造函数中注入:
public AuthController(IAuthenticationSchemeProvider schemeProvider,
IOptionsMonitorCache<OpenIdConnectOptions> optionsCache,
OpenIdConnectPostConfigureOptions postConfigureOptions)
{
_schemeProvider = schemeProvider;
_optionsCache = optionsCache;
_postConfigureOptions = postConfigureOptions;
}
Run Code Online (Sandbox Code Playgroud)
在AddOrUpdate
AuthController.cs的方法中,创建OpenIdConnectOptions
该类的一个实例,然后在配置后步骤中使用它:
var options = new OpenIdConnectOptions
{
Authority = "xxx-endpoint",
CallbackPath = "/signin-oidc",
ClientId = "XXX",
ClientSecret = "XXX",
......
};
_postConfigureOptions.PostConfigure("oidc", options);
_optionsCache.TryAdd("oidc", options);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2481 次 |
最近记录: |