IAuthenticationSchemeProvider
您可以在运行时使用和添加身份验证方案IOptionsMonitorCache
。
public class AuthInitializer
{
private IAuthenticationSchemeProvider _provider;
private IOptionsMonitorCache<OpenIdConnectOptions> _optionsCache;
// other services you need
public AuthInitializer(IAuthenticationSchemeProvider provider, IOptionsMonitorCache<OpenIdConnectOptions> options)
{
_provider = provider;
_optionsCache = options;
}
public Task InitializeAsync(CancellationToken cancellationToken = default)
{
// execute some business logic
// ...
var schemeName = "OidcCustom1"; // must be unique for different schemes
var schemeOptions = new OpenIdConnectOptions()
{
Authority = "https://demo.identityserver.io/",
ClientId = "m2m", // fetch credentials from another service or database
ClientSecret = "secret",
Events = {
OnTokenValidated = async context => {
var someService = context.HttpContext.RequestServices.GetRequiredService<ISomeService>();
var result = await someService.DoSomethingAsync();
// handle the event
}
}
};
var scheme = new AuthenticationScheme(schemeName, displayName:null, typeof(OpenIdConnectHandler));
_provider.TryAddScheme(scheme);
_optionsCache.TryAdd(
schemeName,
schemeOptions
);
return Task.CompletedTask;
}
}
在 DI 中注册这个类:
services.AddTransient<AuthInitializer>();
然后在构建主机后执行它:
public class Program
{
public static async Task Main(string[] args)
{
var host = CreateHostBuilder(args).Build();
using var scope = host.Services.CreateScope();
var authInitializer = scope.ServiceProvider.GetRequiredService<AuthInitializer>();
await authInitializer.InitializeAsync();
await host.RunAsync();
}
}
然后你可以像往常一样参考这个认证方案:
services.AddAuthentication(
options =>
{
options.DefaultScheme = "OidcCustom";
});
或者
[Authorize(AuthenticationSchemes = "OidcCustom")]
[HttpGet]
public async Task<IActionResult> Index(CancellationToken cancellationToken) { ... }