oidc-client-js未从Identity Server 4正确获取声明

时间:2018-03-12 08:49:43

标签: javascript asp.net identityserver4 openid-connect oidc-client-js

我有一个Identity Server 4的本地实例,我正在尝试关注this guide来创建一个Javascript客户端。这使用了oidc-client-js库,我正在使用signin弹出方法,所以我的登录事件处理程序如下所示:

HKWorkoutSession

身份验证似乎工作正常 - 我被重定向到我的Identity Server,它接受客户端请求,验证我的登录并将我返回到客户端应用程序。但是,文档说上面代码中的signin(e) { e.preventDefault(); this.oidcUserMgr.signinPopup({state:'some data'}).then(function(user) { console.log("signed in", user.profile); }).catch(function(err) { console.log(err); }); } 对象应该包含用户声明,但事实并非如此。这是我回来的user.profile

enter image description here

use.profile属性是刚刚进行身份验证的用户的正确ID。但我的Identity Server也发出了声明,以响应我的客户请求的其他范围(subprofile),因此我应该看到emailname,{ {1}}等等。我可以观察在IS4中调试preferred_username实现时发出的这些声明。此外,如果我使用随用户对象返回的email向本地运行的另一个API(ASP.NET Web API)发出请求,我会在IProfileService中看到这些声明:

enter image description here

那么如何在我的Javascript代码中掌握这些声明呢?

1 个答案:

答案 0 :(得分:4)

这些用户声明可能会进入ID令牌。要做到这一点,请检查您的IDP提供商的客户端配置中是否有AlwaysIncludeUserClaimsInIdToken = true,例如

        public static IEnumerable<Client> GetClients()
    {
        return new List<Client>()
        {
            new Client()
            {
                ClientName = "IDP Client",
                ClientId = "client",
                ClientSecrets = { new Secret("secret".Sha256()) },
                AllowedGrantTypes =  GrantTypes.Hybrid,
                RedirectUris = new List<string>()
                {
                    "http://localhost:60811/signin-oidc"
                },
                AllowedScopes =
                {
                    IdentityServerConstants.StandardScopes.OpenId,
                    IdentityServerConstants.StandardScopes.Profile,
                    "myapi"
                },
                AlwaysIncludeUserClaimsInIdToken = true,
                AllowOfflineAccess = true
            },