【问题标题】:Audience claim on identityserver4 code flow access token is wrong对 identityserver4 代码流访问令牌的受众声明是错误的
【发布时间】:2021-06-11 11:31:22
【问题描述】:

我正在使用带有 Identityserver4 的 vuejs 客户端的代码流。 我添加了RequirePkce,我可以从 oidc-client 获取访问令牌和 id 令牌。 但访问令牌aud 声明指向 Identityserver4 基地址,而不是我的 api 资源。 有什么不对吗?

客户:

new Client
                {
                    ClientId = "js.admin",
                    ClientName = "admin dashboard vuejs client.",

                    RequirePkce = true,
                    RequireClientSecret = false,
                    RequireConsent = false,
                    AllowedGrantTypes = GrantTypes.Code,
                    AllowAccessTokensViaBrowser = true,

                    RedirectUris = new List<string>
                    {
                        "http://localhost:8080",
                        "http://localhost:8080/logincb.html",
                        "http://localhost:8080/silent-renew.html"
                    },
                    PostLogoutRedirectUris = new List<string>
                    {
                        "http://localhost:8080/",
                        "http://localhost:8080"
                    },
                    AllowedCorsOrigins = new List<string>
                    {
                        "http://localhost:8080"
                    },
                    AllowedScopes = new List<string>
                    {
                        "openid",
                        "role",
                        "profile",
                        "api1.rw",
                        "email",
                        "phone"
                    }
                }

oidc 客户端设置:

const clientSettings = {
      userStore: new WebStorageStateStore({ store: window.localStorage }),
      authority: STS_DOMAIN,
      client_id: "js.admin",
      redirect_uri: "http://localhost:8080/logincb.html",
      automaticSilentRenew: true,
      silent_redirect_uri: "http://localhost:8080/silent-renew.html",
      response_type: "code",
      scope: "openid profile api1.rw role email phone",
      post_logout_redirect_uri: "http://localhost:8080/",
      filterProtocolClaims: true
    };

访问令牌解码:

 "iss": "http://localhost:5001",
 "aud": "http://localhost:5001/resources",

如您所见,发行者和受众的声明都是错误的。

但即使是范围也是正确的。 非常感谢任何帮助。

Bearer was not authenticated. Failure message: IDX10214: Audience validation failed. Audiences: 'http://localhost:5001/resources'. Did not match: validationParameters.ValidAudience: 'api1' or validationParameters.ValidAudiences: 'null'.

这是我得到的最后一个错误。

【问题讨论】:

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


    【解决方案1】:

    http://localhost:5001/resources 是一个通用资源,当您没有定义任何 ApiResources 或将其与请求的 ApiScope 关联时添加。

    从文档here 中,它说:

    使用仅限范围模型时,不会添加任何 aud(受众)声明 到令牌,因为这个概念不适用。如果你需要一个 aud 声明,您可以启用 EmitStaticAudienceClaim 设置 选项。这将在 issuer_name/resources 中发出 aud 声明 格式。如果您需要对 aud 声明进行更多控制,请使用 API 资源。

    要让 api1.rw 成为您的受众,您需要将 ApiResource 添加到您的 IdentityServer 配置中。您可以将 ApiResourceApiScope 命名为 api1.rw

    【讨论】:

    • 我没有放代码,但我也定义了 api 资源。并且 api1.rw 是我的 apiscope 附加到 api 资源 api1
    • 您能否将您的 ApiScope 和 ApiResource 定义发布到您的问题中?
    • 您还可以将此标志 EmitStaticAudienceClaim(在 IdentityServer 中)设置为 false 以删除通用资源受众。
    • 我知道我可能看起来有点笨,但错误是由于将内存 api 资源添加到服务的错误方式造成的。 :) 真的很感谢你的时间。你拯救了我的一天
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 2020-06-29
    • 2019-07-21
    • 1970-01-01
    • 2020-09-09
    • 2020-03-11
    相关资源
    最近更新 更多