【问题标题】:IdentityServer4: audience is empty when calling web api?IdentityServer4:调用web api时观众是空的?
【发布时间】:2020-12-18 07:40:15
【问题描述】:

IdentityServer4 v4,通过 web blazor 客户端应用调用 web api 时发生错误。

Bearer error="invalid_token", error_description="The audience 'empty' is invalid" value in header 

这样在启动时添加范围,如何添加受众?

            .AddOpenIdConnect("oidc", options =>
            {
                options.Authority = "http://localhost:5000";
                options.RequireHttpsMetadata = false;
                options.ClientId = "testapp";
                options.ResponseType = "code";
                options.SaveTokens = true;
                options.GetClaimsFromUserInfoEndpoint = true;
                options.UseTokenLifetime = false;
                options.Scope.Add("openid");
                options.Scope.Add("profile");
                options.Scope.Add("offline_access");
                options.Scope.Add("account");
                options.Scope.Add("accountwrite");
                options.Scope.Add("accountread");
                options.Scope.Add("payment");
                options.Scope.Add("paymentwrite");
                options.Scope.Add("paymentread");
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    NameClaimType = "name"
                };
                options.Events = new OpenIdConnectEvents
                {
                    OnAccessDenied = context =>
                    {
                        context.HandleResponse();
                        context.Response.Redirect("/");
                        return Task.CompletedTask;
                    }
                };
            });

【问题讨论】:

    标签: c# asp.net-core oauth-2.0 identityserver4 openid-connect


    【解决方案1】:

    仅当您在 IdentityServer 中定义了 ApiScopes 和 ApiResources 时才会填充 Audience 声明。

    API 范围可以定义为:

    new ApiScope(name: "invoice",
            displayName: "Invoices access",
            userClaims: new List<string> { "level" }),
    

    而要定义一个合适的ApiResource,你可以定义一个:

    _apiResources = new List<ApiResource>()
    {
        new ApiResource("invoiceapi")
        {
            Scopes = { "invoice" }   //invoice is the name of the ApiScope
        }
    };
    

    那么你需要在你的客户中询问发票范围。

    【讨论】:

      猜你喜欢
      • 2018-08-05
      • 2018-02-15
      • 1970-01-01
      • 2010-11-21
      • 2017-07-21
      • 2020-12-15
      • 1970-01-01
      • 2020-10-20
      • 1970-01-01
      相关资源
      最近更新 更多