【问题标题】:Stop validating audience for webapi application in Azure AD停止验证 Azure AD 中 Web api 应用程序的受众
【发布时间】:2020-07-09 14:47:25
【问题描述】:

我有一个使用 Azure AD 的 ASP.NET WEB API Core 3.1 应用程序。 我使用 Microsoft.Identity.Web 库。 我是这样设置的:

public void ConfigureServices(IServiceCollection services)
{
    Trace.TraceInformation("Configuring services");
    services.AddProtectedWebApi(Configuration, subscribeToJwtBearerMiddlewareDiagnosticsEvents: true
            )
        .AddProtectedWebApiCallsProtectedWebApi(Configuration)
        .AddInMemoryTokenCaches();
    services.AddAuthentication(AzureADDefaults.BearerAuthenticationScheme)
        .AddAzureADBearer(options => Configuration.Bind("AzureAd", options));
.......
}

我的应用程序是多租户的,所以我需要取消验证观众。 最近我将该库用作一个单独的项目,因此我可以在其源代码中对其进行更改:

ValidateIssuer = false

但目前我使用的是 nuget 的库,所以我不能简单地更改源代码以避免验证受众(发行者)。

我应该如何配置库来实现它?

【问题讨论】:

    标签: asp.net-web-api azure-active-directory


    【解决方案1】:

    对于多租户应用程序,将 ValidateIssuer 设置为 false。这意味着应用程序将验证颁发者。参考这个article

    JwtBearerEvents.TokenValidated 事件中验证令牌颁发者。发行者在“iss”声明中发送。

    可以禁用颁发者验证,您可以参考以下代码:

    public void Configure(string name, JwtBearerOptions options)
    {
        options.Audience = AzureOptions.ClientId;
    
        options.TokenValidationParameters = new TokenValidationParameters{
            ValidateIssuer = false
        };
        options.Events = new JwtBearerEvents()
        {
            OnTokenValidated = (context) =>
            {
                if(!context.SecurityToken.Issuer.StartsWith("https://sts.windows.net/"))
                    throw new SecurityTokenValidationException();
                    return Task.FromResult(0);
            }
        };
    
        options.Authority = $"{AzureOptions.Instance}{AzureOptions.TenantId}";
    }
    

    【讨论】:

      【解决方案2】:

      我认为您可以注入自己的验证代码来处理多租户验证,而不是尝试禁用验证。 像

      services.Configure<JwtBearerOptions>(AzureADDefaults.JwtBearerAuthenticationScheme, options => {
      options.Authority += "/v2.0";
      
      // The web API accepts as audiences both the Client ID (options.Audience) and api://{ClientID}.
      options.TokenValidationParameters.ValidAudiences = new []
      {
       options.Audience,
       $"api://{options.Audience}"
      };
      
      // Instead of using the default validation (validating against a single tenant,
      // as we do in line-of-business apps),
      // we inject our own multitenant validation logic (which even accepts both v1 and v2 tokens).
      options.TokenValidationParameters.IssuerValidator = AadIssuerValidator.GetIssuerValidator(options.Authority).Validate;; });
      

      根据微软文档:https://docs.microsoft.com/en-us/azure/active-directory/develop/scenario-protected-web-api-app-configuration#code-initialization

      【讨论】:

        【解决方案3】:

        通过使用 Microsoft.Identity.Web 库,可以将 ValidateIssuer 设置为 false,如下所示:

        services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
        .AddProtectedWebApi(options =>
        {
            options.TokenValidationParameters.ValidateIssuer = false;
        },options => { Configuration.Bind("AzureAd", options); });
        

        访问令牌包含 aud(受众)和 iss(发行者)作为两个独立的声明。

        受众是令牌的预期接收者。受众是 Azure 门户中分配的 API 的应用程序 ID,不建议停止验证受众

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-03-11
          • 2021-03-21
          • 2018-10-10
          • 2019-05-28
          • 2022-09-23
          • 2022-10-25
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多