C# 使用外部配置服务添加OpenIDConnect

C# 使用外部配置服务添加OpenIDConnect,c#,.net-core,openid,asp.net-core-3.1,C#,.net Core,Openid,Asp.net Core 3.1,我正在将OpenIdConnect添加到我的应用程序,如下所示: .AddOpenIdConnect("oidc", options => { var clientSecret = Configuration.GetValue<string>("clientSecret"); options.ClientSecret = clientSecret; }); 如何执行此操作?可以执行可以注入服务的post配置类。 像这样: 公共类OpenIdConnectP

我正在将OpenIdConnect添加到我的应用程序,如下所示:

.AddOpenIdConnect("oidc", options =>
{
     var clientSecret = Configuration.GetValue<string>("clientSecret");
     options.ClientSecret = clientSecret;
});

如何执行此操作?

可以执行可以注入服务的post配置类。 像这样:

公共类OpenIdConnectPostConfigureOptions:IPostConfigureOptions
{
私有只读iCretsService(U secretsService);
公共OpenIdConnectPostConfigureOptions(iCretsService secretsService)
{
_secretsService=secretsService;
}
公共异步void PostConfigure(字符串名称、OpenIdConnectOptions选项)
{
options.ClientSecret=wait_secretsService.Get(“ClientSecret”);
}
}

在上述情况下,我建议在操作中扩展配置,而不是使用DI。

要访问机密,您可以添加配置提供程序,并继续在ConfigureServices方法中使用Configuration.GetValue

对于Azure Key Vault,它位于nuget软件包下

public static void Main(string[] args)
{
    CreateWebHostBuilder(args)
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
            if (env.IsLocal())
            {
                ...
            }
            else
            {
                config.AddAzureKeyVault(keyVaultUri);
            }
        })
        .Build()
        .Run();
}

对于AWS-

您如何保守秘密?Azure或AWS。用于开发的本地dotnet机密此答案与OpenIdConnect无关。它通常只是意味着如何获得使用秘密管理,而不是如何在OpenIdConnection中使用它们。我的意思是,在所描述的情况下,我不会使用注入服务,而是扩展配置。问题是,AddOpenConnectiond没有为您提供IServiceProvider。您的解决方案没有提供配置此情况的方法。我也明白我应该扩展配置,但我在写问题时没有意识到这一点。简言之,如果我使用你的解决方案,它根本帮不了我
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="3.1.0" />
public class OpenIdConnectPostConfigureOptions : IPostConfigureOptions<OpenIdConnectOptions>
{
    private readonly ISecretsService _secretsService;

    public OpenIdConnectPostConfigureOptions(ISecretsService secretsService)
    {
        _secretsService = secretsService;
    }

    public async void PostConfigure(string name, OpenIdConnectOptions options)
    {
        options.ClientSecret = await _secretsService.Get("clientSecret");
    }
}
public static void Main(string[] args)
{
    CreateWebHostBuilder(args)
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
            if (env.IsLocal())
            {
                ...
            }
            else
            {
                config.AddAzureKeyVault(keyVaultUri);
            }
        })
        .Build()
        .Run();
}