在Startup.cs文件中配置默认身份验证方案(Default Authentication Scheme)。在AddAuthentication方法中,使用DefaultScheme属性指定默认身份验证方案,使用DefaultChallengeScheme属性指定默认的挑战方案。
示例代码:
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(o =>
{
o.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
o.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(o =>
{
o.Authority = "https://login.microsoftonline.com/{tenantId}";
o.TokenValidationParameters = new TokenValidationParameters
{
ValidAudiences = new[] { "api://{clientId}" }
};
});
services.AddControllers();
}