首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >停止验证Azure AD中webapi应用程序的受众

停止验证Azure AD中webapi应用程序的受众
EN

Stack Overflow用户
提问于 2020-05-07 16:17:35
回答 3查看 1.3K关注 0票数 2

我有一个使用Azure的ASP.NET WebAPICore3.1应用程序。我使用Microsoft.Identity.Web库。我把它设成这样:

代码语言:javascript
运行
复制
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));
.......
}

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

代码语言:javascript
运行
复制
ValidateIssuer = false

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

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-05-08 06:19:28

对于多租户应用程序,请将ValidateIssuer设置为false。这意味着应用程序将验证发行者。请参阅此article

JwtBearerEvents.TokenValidated事件中验证令牌颁发者。发行人是在"iss“索赔中发出的。

禁用颁发者验证是可能的,您可以引用下面的代码:

代码语言:javascript
运行
复制
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
EN

Stack Overflow用户

发布于 2020-05-08 03:09:02

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

代码语言:javascript
运行
复制
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;; });

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

票数 1
EN

Stack Overflow用户

发布于 2020-07-09 06:47:25

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

代码语言:javascript
运行
复制
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddProtectedWebApi(options =>
{
    options.TokenValidationParameters.ValidateIssuer = false;
},options => { Configuration.Bind("AzureAd", options); });

访问令牌包含aud (受众)和iss (Issuer)两个单独的声明。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61662703

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档