Asp.net core 从配置文件配置JWTBeareOptions

Asp.net core 从配置文件配置JWTBeareOptions,asp.net-core,.net-core,Asp.net Core,.net Core,我试图找到一份文档,说明如何使用microsoft预定义的混淆部分/键从配置文件中配置jwt承载器及其在asp.net核心中的功能。Mucrosoft文档中没有关于这是否可能的解释。我觉得这应该是可能的,因为.net核心版本中的所有内容都在使用选项模式 如何使用相同的技术配置红隼主机 这不是对最初问题的真正回答。不过,我对这个解决方案非常满意 在深入研究AspNetCore源代码几个小时后,我发现JwtBearerOptions是。这意味着您不能在不编写代码的情况下从配置文件提供配置。然而,我找

我试图找到一份文档,说明如何使用microsoft预定义的混淆部分/键从配置文件中配置jwt承载器及其在asp.net核心中的功能。Mucrosoft文档中没有关于这是否可能的解释。我觉得这应该是可能的,因为.net核心版本中的所有内容都在使用选项模式

如何使用相同的技术配置红隼主机


这不是对最初问题的真正回答。不过,我对这个解决方案非常满意

在深入研究AspNetCore源代码几个小时后,我发现JwtBearerOptions是。这意味着您不能在不编写代码的情况下从配置文件提供配置。然而,我找到了一个可接受的解决方案,它将适用于大多数情况

我没有所有可用密钥的列表,这里的示例只显示了其中的两个。您可以检查JWTBeareOptions的公共属性,并将它们添加到appsettings.json中。框架将挑选并使用它们

有关如何工作的详细信息,请参见下面的代码和注释:

appsettings.json

{
    "Cronus": {
        "Api": {
            "JwtAuthentication": {
                "Authority": "https://example.com",
                "Audience": "https://example.com/resources"
            }
        }
    }
}
Startup.cs

public class Startup
{
    const string JwtSectionName = "Cronus:Api:JwtAuthentication";

    private readonly IConfiguration configuration;

    public Startup(IConfiguration configuration)
    {
        this.configuration = configuration;
    }

    public void ConfigureServices(IServiceCollection services)
    {
        // Gets the settings from a configuration section. Notice how we specify the name for the JwtBearerOptions to be JwtBearerDefaults.AuthenticationScheme.
        services.Configure<JwtBearerOptions>(JwtBearerDefaults.AuthenticationScheme, configuration.GetSection(JwtSectionName));

        // OR

        // Gets the settings from a configuration. Notice how we specify the name for the JwtBearerOptions to be JwtBearerDefaults.AuthenticationScheme.
        services.Configure<JwtBearerOptions>(JwtBearerDefaults.AuthenticationScheme, configuration);

        services.AddAuthentication(o =>
        {
            // AspNetCore uses the DefaultAuthenticateScheme as a name for the JwtBearerOptions. You can skip these settings because .AddJwtBearer() is doing exactly this.
            o.DefaultAuthenticateScheme = Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerDefaults.AuthenticationScheme;
            o.DefaultChallengeScheme = Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerDefaults.AuthenticationScheme;
        })
        .AddJwtBearer();
    }
}
公共类启动
{
const string JwtSectionName=“Cronus:Api:JwtAuthentication”;
专用只读IConfiguration配置;
公共启动(IConfiguration配置)
{
this.configuration=配置;
}
public void配置服务(IServiceCollection服务)
{
//从配置节获取设置。请注意我们如何将JwtBearerOptions的名称指定为JwtBearerDefaults.AuthenticationScheme。
Configure(JwtBearerDefaults.AuthenticationScheme,configuration.GetSection(JwtSectionName));
//或
//从配置获取设置。请注意,我们如何将JwtBearerOptions的名称指定为JwtBearerDefaults.AuthenticationScheme。
配置(JwtBearerDefaults.AuthenticationScheme,配置);
services.AddAuthentication(o=>
{
//AspNetCore使用DefaultAuthenticateScheme作为JWTBeareOptions的名称。您可以跳过这些设置,因为.AddJwtBearer()正是这样做的。
o、 DefaultAuthenticateScheme=Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerDefaults.AuthenticationScheme;
o、 DefaultChallengeScheme=Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer();
}
}
在哪里 applicationsettings.json

{
"JwtBearerOptions": {
    "Audience": "Your aud" 
  }
}
{
"JwtBearerOptions": {
    "Audience": "Your aud" 
  }
}