【发布时间】:2020-12-22 07:10:58
【问题描述】:
我不确定我是否完全理解 Microsoft.Identity.Web 的更改,但我正在关注一篇文章 (given by Microsoft here) 其中描述了如何在启动时进行更改
services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
.AddAzureAD(options => Configuration.Bind("AzureAd", options));
到
services.AddMicrosoftIdentityWebAppAuthentication(Configuration);
虽然这看起来不错且简单,但我还有一些工作要做,因为我现有的代码中有以下 sn-p
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,
};
});
为了给您一些背景信息,我们对这个应用程序有两个变体
- 用户登录并做一些工作(这里用户将获得 Microsoft 登录对话框以使用他/她的凭据登录)
- Microsoft Azure 使用一些令牌调用我们的端点,我们需要验证该令牌。
您在上面看到的 JWTvaliation 部分是第二项,一旦我们收到一个令牌,我们就会在没有登录和 UI 工作流的情况下验证该令牌。
问题:
上面的代码工作正常。这里唯一的问题是如果我们喜欢使用Microsoft.Identity,我们应该如何使用第二项(JWT),因为services.AddAuthentication().AddAzureAD 返回IAuthenticationBuilder,我们进一步使用它来添加AddJwtBearer,而services.AddMicrosoftIdentityWebAppAuthentication 不返回@987654330 @。
【问题讨论】:
标签: c# asp.net-core asp.net-core-3.1 microsoft-identity-platform