首页 > 解决方案 > 身份服务器检索用户详细信息

问题描述

我一直在尝试按照本教程使用我的 Angular 应用程序来实现身份服务器。到目前为止,我发现它真的很有帮助。

我想获取用户名和一些自定义详细信息,但我不知道该怎么做。

在该部分

export function getClientSettings(): UserManagerSettings {
    return {
        authority: 'http://localhost:5555/',
        client_id: 'angular_spa',
        redirect_uri: 'http://localhost:4200/auth-callback',
        post_logout_redirect_uri: 'http://localhost:4200/',
        response_type:"id_token token",
        scope:"openid profile api1",
        filterProtocolClaims: true,
        loadUserInfo: true
    };
}

我已经确定我已经设置了loadUserInfo: true. 但是当我查看返回对象的内容时,我看不到任何用户信息。

对象

我需要在服务器上做些什么来返回用户名等吗?我目前正在使用 TestUser 类,但我希望在客户端的 user.profile 中看到用户名。在服务器上,我的用户设置为

new TestUser {
    SubjectId = "5BE86359-073C-434B-AD2D-A3932222DABE",
    Username = "scott",
    Password = "password"
}

如上所示,我可以看到SubjectId(虽然我觉得它映射到 sub 而不是 sid 很奇怪)

我理解本教程的方式是,一旦成功登录 oicd-client 将使用检索到的令牌再次调用服务器,以便在您设置时获取用户详细信息loadUserInfo: true

为什么我看不到详细信息?

编辑:

再次浏览教程,我注意到这一行:

如果 loadUserInfo 设置为 true,它还将调用用户信息端点以获取它已被授权访问的任何额外身份数据。

如何授权用户信息端点访问我希望它访问的数据?

标签: angularidentityserver4

解决方案


您需要配置将返回哪些用户数据,我没有配置任何!

确保Startup.cs您具备以下条件:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddIdentityServer()
                ...
                .AddTestUsers(Users.Get())
                ...
    }

然后添加claims到您的用户

public static List<TestUser> Get()
    {
        return new List<TestUser> {
            new TestUser {
                SubjectId = "5BE86359-073C-434B-AD2D-A3932222DABE",
                Username = "scott",
                Password = "password",
                Claims = new List<Claim>
                {
                    new Claim("name", "scott"),
                    new Claim("website", "https://scott.com")
                }
            }
        };
    }

然后,声明将出现在客户端的配置文件属性中。


推荐阅读