【问题标题】:How to Add JwtBearer along with AddMicrosoftIdentityWebAppAuthentication如何添加 JwtBearer 和 AddMicrosoftIdentityWebAppAuthentication
【发布时间】: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,
       };
  });

为了给您一些背景信息,我们对这个应用程序有两个变体

  1. 用户登录并做一些工作(这里用户将获得 Microsoft 登录对话框以使用他/她的凭据登录)
  2. 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


    【解决方案1】:

    AddMicrosoftIdentityWebAppAuthentication 实际上是just a fancy way 来执行以下操作:

    services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
        .AddMicrosoftIdentityWebApp(…)
    

    因此它将默认方案配置为 OIDC 方案并运行 AddMicrosoftIdentityWebApp 来配置最终执行的任何操作。

    现在,AddAuthentication 实际上可以在服务集合上多次调用。您只需要小心不要错误地重新配置事物。无参函数不这样做,所以访问IAuthenticationBuilder进一步配置认证是一个好方法。

    这意味着您可以像这样更改代码:

    // 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(Configuration) 之类的东西,换句话说,只需先在管道中添加 JWTBeaer,然后添加 MicrosoftIdentityWebAppAuthentication - 这也与您的示例相同吗?
    • @BrijeshShah 因为必须在服务集合上调用AddMicrosoftIdentityWebAppAuthentication,所以不能在AddJwtBearer 之后直接运行它。您将不得不再次致电services。但是,是的,您可以在我的示例中切换这两个语句而不影响任何内容。顺序只对配置很重要(以后的配置可能会覆盖以前的配置)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-19
    • 1970-01-01
    • 2021-03-16
    • 2022-12-28
    • 2019-11-13
    • 1970-01-01
    • 2021-11-01
    相关资源
    最近更新 更多