【问题标题】:How to set multiple audiences in Asp.Net Core 2.0 "AddJwtBearer" middleware?如何在 Asp.Net Core 2.0“AddJwtBearer”中间件中设置多个受众?
【发布时间】:2018-04-09 23:20:04
【问题描述】:

我有一个针对 AAD 进行身份验证的 Asp.Net Core 2.0 WebApi:

            services.AddAuthentication(options => { options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; })
                .AddJwtBearer(options =>
                            {
                                options.Authority = "https://login.microsoftonline.com/TENANT.onmicrosoft.com";
                                options.Audience = "CLIENT_ID";
                            });

我的 SPA 应用从 AAD 获取令牌并将其作为 bearer 标头发送。一切正常。

我在 Azure 调度程序中创建了一个作业并设置了Active Directory OAuth

运行作业后出现此错误:Bearer error="invalid_token", error_description="The audience is invalid"

当我将AddJwtBearer(...) 中的options.Audience 设置为https://management.core.windows.net/ 时,作业有效,但SPA 无效。

我想,我需要将Audience 设置为数组['CLIENT_ID', "https://management.core.windows.net/"],但options.Audiencestring 的类型。如果我根本没有设置Audience,Spa 和 Job 都不起作用(401 未经身份验证)。将Audience 设置为CLIENT_ID,https://management.core.windows.net/ 也不起作用。

有没有办法在AddJwtBearer 中启用多个受众?

【问题讨论】:

    标签: jwt azure-active-directory asp.net-core-2.0


    【解决方案1】:

    我想我遇到了和你一样的问题。为了让它发挥作用,我将观众从options 转移到TokenValidationParameters,它接受多个条目。检查下面的代码:

    .AddJwtBearer(options =>
    {
        options.Authority = "https://login.windows.net/trades.no";
        options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
        {
            ValidateIssuer = true,
            ValidAudiences = new List<string> 
            {
                "AUDIENCE1",
                "AUDIENCE2" 
            }
        };
    

    【讨论】:

    猜你喜欢
    • 2018-06-13
    • 2018-01-23
    • 1970-01-01
    • 1970-01-01
    • 2019-01-18
    • 1970-01-01
    • 2018-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多