C# 如何将JwtBearer与AddMicrosoftIdentityWebAppAuthentication一起添加
我不确定自己是否完全理解了Microsoft.Identity.Web的更改,但我正在阅读一篇文章(其中描述了如何在启动时进行更改)C# 如何将JwtBearer与AddMicrosoftIdentityWebAppAuthentication一起添加,c#,asp.net-core,asp.net-core-3.1,microsoft-identity-platform,C#,Asp.net Core,Asp.net Core 3.1,Microsoft Identity Platform,我不确定自己是否完全理解了Microsoft.Identity.Web的更改,但我正在阅读一篇文章(其中描述了如何在启动时进行更改) services.AddAuthentication(AzureADDefaults.AuthenticationScheme) .AddAzureAD(options => Configuration.Bind("AzureAd", options)); 到 虽然这看起来很好,也很简单,但我还有一些工作要做,因为我现有的代
services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
.AddAzureAD(options => Configuration.Bind("AzureAd", options));
到
虽然这看起来很好,也很简单,但我还有一些工作要做,因为我现有的代码中有以下代码片段
services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
.AddAzureAD(options => this.configuration.Bind("AzureAd", options))
.AddJwtBearer(options =>
{
//this code used to validate signing keys
string stsDiscoveryEndpoint = "https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration";
IConfigurationManager<OpenIdConnectConfiguration> configurationManager = new ConfigurationManager<OpenIdConnectConfiguration>(stsDiscoveryEndpoint, new OpenIdConnectConfigurationRetriever());
OpenIdConnectConfiguration openIdConfig = configurationManager.GetConfigurationAsync(CancellationToken.None).GetAwaiter().GetResult();
var tenantId = this.configuration["TenantId"];
var validIssuer = $"https://sts.windows.net/{tenantId}/";
options.TokenValidationParameters = new TokenValidationParameters()
{
ValidIssuer = validIssuer,
ValidAudience = this.configuration["ClientId"],
IssuerSigningKeys = openIdConfig.SigningKeys,
};
});
services.AddAuthentication(AzureAddFaults.AuthenticationScheme)
.AddAzureAD(options=>this.configuration.Bind(“AzureAd”,options))
.AddJwtBearer(选项=>
{
//此代码用于验证签名密钥
字符串stsDiscoveryEndpoint=”https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration";
IConfigurationManager configurationManager=new configurationManager(stsDiscoveryEndpoint,new OpenIdConnectConfigurationRetriever());
OpenIdConnectConfiguration openIdConfig=configurationManager.GetConfigurationAsync(CancellationToken.None).GetAwaiter().GetResult();
var tenantId=this.configuration[“tenantId”];
var validIssuer=$”https://sts.windows.net/{tenantId}/“;
options.TokenValidationParameters=新的TokenValidationParameters()
{
ValidIssuer=ValidIssuer,
Validudience=this.configuration[“ClientId”],
IssuerSigningKeys=openIdConfig.SigningKeys,
};
});
为了给你一点背景知识,我们有两种不同的应用程序
上面的代码工作正常。这里唯一的问题是如果我们想使用
Microsoft.Identity
我们应该如何使用第二项(JWT),因为services.AddAuthentication().AddAzureAD
返回IAuthenticationBuilder
,我们进一步使用它来添加AddJwtBearer
,而服务。AddMicrosoftIdentityWebAppAuthentication
不返回IAuthenticationBuilderAddMicrosoftIdentityWebAppAuthentication
实际上是执行以下操作:
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(…)
因此,它将默认方案配置为OIDC方案,并运行AddMicrosoftIdentityWebApp
来配置最终执行的操作
现在,实际上可以在服务集合上多次调用。您只需小心不要错误地重新配置。无参数函数不能做到这一点,因此访问IAAuthenticationBuilder
是进一步配置身份验证的好方法
这意味着您可以像这样更改代码:
// configure Microsoft Identity Web first
// this also sets the default authentication to OIDC
services.AddMicrosoftIdentityWebAppAuthentication(Configuration);
// retrieve an authentication builder without changing the default
services.AddAuthentication()
// add JWT bearer now
.AddJwtBearer(options =>
{
// …
});
AddMicrosoftIdentityWebAppAuthentication
实际上是执行以下操作:
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(…)
因此,它将默认方案配置为OIDC方案,并运行AddMicrosoftIdentityWebApp
来配置最终执行的操作
现在,实际上可以在服务集合上多次调用。您只需小心不要错误地重新配置。无参数函数不能做到这一点,因此访问IAAuthenticationBuilder
是进一步配置身份验证的好方法
这意味着您可以像这样更改代码:
// configure Microsoft Identity Web first
// this also sets the default authentication to OIDC
services.AddMicrosoftIdentityWebAppAuthentication(Configuration);
// retrieve an authentication builder without changing the default
services.AddAuthentication()
// add JWT bearer now
.AddJwtBearer(options =>
{
// …
});
我喜欢你的解释,也许这也是正确的答案。然而,我想知道一个非常快速的替代方案,这是正确的理解,还是会改变目的。我们可以使用类似于services.AddAuthentication().AddJwtBearer().AddMicrosoftIdentityWebAppAuthentication(配置)的内容来代替你编写的代码换句话说,只需先在管道中添加JWTBeaer,然后添加MicrosoftIdentityWebAppAuthentication-这是否与您的示例相同?@BrijeshShah因为必须对服务集合调用AddMicrosoftIdentityWebAppAuthentication
,所以您不能在AddJwtBearer
之后直接运行它。您必须调用再次打开services
。但是,是的,您可以在不影响任何内容的情况下切换我的示例中的这两个语句。顺序只对配置有影响(以后的配置可能会覆盖以前的配置)。我喜欢你的解释,也许这也是正确的答案。但是,我想知道一个非常快速的替代方案,这是正确的理解还是会改变目的。我们可以使用类似于services.AddAuthentication().AddJwtBearer().AddMicrosoftIdentityWebAppAuthentication(配置)的内容来代替你编写的代码换句话说,只需先在管道中添加JWTBeaer,然后添加MicrosoftIdentityWebAppAuthentication-这是否与您的示例相同?@BrijeshShah因为必须对服务集合调用AddMicrosoftIdentityWebAppAuthentication
,所以您不能在AddJwtBearer
之后直接运行它。您必须调用再次打开services
。但是,是的,在我的示例中,您可以在不影响任何内容的情况下切换这两个语句。顺序只对配置有影响(以后的配置可能会覆盖以前的配置)。