首页 文章

IdentityServer4受众验证失败

提问于
浏览
1

好让我迷茫 . 我有一个Angular应用程序,可以使用IdentityServer4登录 . Angular应用程序是clientId web . 当我解码jwt时,aud只有web .

我创建了一个api资源 - api .

现在,当我尝试使用访问令牌调用我的.NET Core Api时,我得到了受众验证失败错误 . 基本上说它期待网络,并且它作为 Spectator 得到了api .

.AddIdentityServerAuthentication(options =>
        {
            options.Authority = "http://localhost:5000";
            options.RequireHttpsMetadata = false;
            options.ApiName = "api";
            options.SaveToken = true;
        });

如果我将ApiName更改为web,它可以工作 . jwt中列出的范围都是我所期望的 . 为了进一步增加我的困惑,当我查看dbo.PersistedGrants时,数据中列出的受众是

"Audiences": [
    "http://localhost:5000/resources",
    "api"
    ]

客户:

new Client
            {
                ClientId = "web",
                ClientName = "Web",

                AccessTokenType = AccessTokenType.Jwt,

                AllowedGrantTypes = GrantTypes.Implicit,
                AllowAccessTokensViaBrowser = true,
                AlwaysIncludeUserClaimsInIdToken = true,

                RedirectUris = { "http://localhost:5005" },
                PostLogoutRedirectUris = { "http://localhost:5005/logout" },
                AllowedCorsOrigins = { "http://localhost:5005", "https://localhost:5005" },

                AllowedScopes =
                {
                    IdentityServerConstants.StandardScopes.OpenId,
                    IdentityServerConstants.StandardScopes.Profile,
                    IdentityServerConstants.StandardScopes.Email,
                    "role",
                    "api"
                },

                RequireConsent = false
            }

API

new ApiResource("api", "API")

IdentityResource

return new[]
        {
            new IdentityResources.OpenId(),
            new IdentityResources.Profile(),
            new IdentityResources.Email(),
            new IdentityResource("role", new[] { JwtClaimTypes.Role })
            {
                Required = true
            }
        };

我希望jwt对 Spectator 有相同的 Value .

我的理解是错误的还是错过了配置IdentityServer4的东西?

1 回答

  • 0

    确保在您的身份验证请求中包含 scope: "api" . 如果您不对IdentityServer(或任何OIDC提供商)进行身份验证,则不会在您的jwt受众属性中获取api资源名称 .

    您的请求应如下所示:

    http://my.identiyserverBaseUrl/connect/authorize?client_id= D&redirect_uri =&response_type = token& scope=api &state = [OPTIONAL_STATE]

    如果您正在使用npm oidc-client,那么您的配置对象可能如下所示:

    const idsConfig = {
      authority:  'YOUR_IDENITY_SERVER_BASE_URL',
      client_id: 'CLIENT_ID',
      redirect_uri: 'YOUR_CLIENT_REDIRECT_URL',
      scope: 'api',
      response_type: 'token',
      client_secret: 'SECRET',
      post_logout_redirect_url: `LOGOUT_REDIRECT_URL`,
      userStore: new WebStorageStateStore({store: window.localStorage}),
      automaticSilentRenew: true, // If you want to enable silent renew
      silent_redirect_uri: 'REDIRECT_URL_FOR_SILENT_RENEW'
    };
    

    注意 scope . 您可以要求多个范围,例如 scope: 'api openid email' . 如果您要求openid范围,则必须在响应类型中包含 id_token .

    Here's the documentationhere for the JavaScript client setup

相关问题