代码之家  ›  专栏  ›  技术社区  ›  Tom Troughton

oidc客户端js无法从Identity Server 4正确获取声明

  •  6
  • Tom Troughton  · 技术社区  · 7 年前

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

    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);
        });
    } 
    

    身份验证似乎工作正常-我被重定向到我的Identity Server,它接受客户端请求,对我的登录进行身份验证,并将我返回到客户端应用程序。然而,医生说 user.profile 上面代码中的对象应该包含用户声明,但它不包含 use.profile 我回来了:

    enter image description here

    这个 sub 属性是刚通过身份验证的用户的正确ID。但我的Identity Server也发布了声明,以响应我的客户端请求的其他作用域( profile email )所以我应该看到这样的说法 name ,则, preferred_username ,则, 电子邮件 等等)。在调试 IProfileService IS4中的实现。此外,如果我使用 access_token 随user对象返回以向本地运行的另一个API(ASP.NET Web API)发出请求,我在中确实看到了这些声明 this.User.Claims :

    enter image description here

    那么,如何在Javascript代码中获得这些声明呢?

    1 回复  |  直到 7 年前
        1
  •  7
  •   Alex Buyny    7 年前

    这些用户声明很可能出现在ID令牌中。要让这一切顺利进行,请检查您是否 AlwaysIncludeUserClaimsInIdToken = true 在IDP提供商的客户端配置中,如

            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
                },
    
    推荐文章