Identityserver4 Identity server正在获取自定义内存中标识资源

Identityserver4 Identity server正在获取自定义内存中标识资源,identityserver4,Identityserver4,在花了两天的时间试图找出我的错误所在后,我接受了我需要一些帮助来为我指明正确的方向 我正处于使用Identity server的真正早期阶段,仍然只是简单地使用inMemory客户机和作用域,只是想了解正在发生的事情以及它们是如何联系在一起的 我试图从Identity server向我的angular应用程序返回自定义声明列表,但失败了。我已尝试扩展IProfileService,它成功地添加了自定义声明,但删除了我在TestUser中定义的其他声明 已注册MyProfileService 未

在花了两天的时间试图找出我的错误所在后,我接受了我需要一些帮助来为我指明正确的方向

我正处于使用Identity server的真正早期阶段,仍然只是简单地使用inMemory客户机和作用域,只是想了解正在发生的事情以及它们是如何联系在一起的

我试图从Identity server向我的angular应用程序返回自定义声明列表,但失败了。我已尝试扩展
IProfileService
,它成功地添加了自定义声明,但删除了我在
TestUser
中定义的其他声明

已注册MyProfileService

未注册MyProfileService

Startup.cs

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddIdentityServer()
                .AddDeveloperSigningCredential()
                .AddInMemoryApiResources(Resources.GetApiResources())
                .AddInMemoryIdentityResources(Resources.GetIdentityResources())
                .AddInMemoryClients(Clients.Get())
                .AddTestUsers(Users.Get())
                .AddDeveloperSigningCredential();

        //services.AddTransient<IProfileService, MyProfileService>();

        services.AddMvc();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole();

#if DEBUG
        app.UseDeveloperExceptionPage();
#endif

        app.UseIdentityServer();

        app.UseStaticFiles();

        app.UseMvcWithDefaultRoute();
    }
资源中心

public static IEnumerable<IdentityResource> GetIdentityResources()
    {
        return new List<IdentityResource> {
            new IdentityResources.OpenId(),
            new IdentityResources.Profile(),
            new IdentityResources.Email(),
            new IdentityResource {
                Name = "role",
                UserClaims = new List<string> {"role"}
            },
            new IdentityResource
            {
                Name = "tenant.info",
                DisplayName = "Tenant Information",
                UserClaims = new List<string>
                {
                    "tenantid",
                    "subscriptionid"
                }
            }
        };
    }

    public static IEnumerable<ApiResource> GetApiResources()
    {
        return new List<ApiResource> {
            new ApiResource("api1", "api1")
        };
    }
在客户端生成结果:显示租户,但不显示我在TestUser上设置的其他声明

profile:
amr: ["pwd"]
auth_time: 1553024858
idp: "local"
sid: "34f36d1c0056ad3d65d1671e339e73aa"
sub: "5BE86359-073C-434B-AD2D-A3932222DABE"
tenantId: "123456"
__proto__: Object
profile:
amr: ["pwd"]
auth_time: 1553025311
idp: "local"
name: "scott"
sid: "831a89053b54f3df7c9ca1bca92e1e10"
sub: "5BE86359-073C-434B-AD2D-A3932222DABE"
tenantId: "123456"
  • 将subject.claims添加到issedClaims

    public Task GetProfileDataAsync(ProfileDataRequestContext context)
    {
        context.IssuedClaims.Add(new System.Security.Claims.Claim("tenantId", "123456"));
        context.IssuedClaims.AddRange(context.Subject.Claims);
    }
    
    结果在客户端中:显示租户ID和名称(指用户名),但不显示我在TestUser上设置的声明

    profile:
    amr: ["pwd"]
    auth_time: 1553024858
    idp: "local"
    sid: "34f36d1c0056ad3d65d1671e339e73aa"
    sub: "5BE86359-073C-434B-AD2D-A3932222DABE"
    tenantId: "123456"
    __proto__: Object
    
    profile:
    amr: ["pwd"]
    auth_time: 1553025311
    idp: "local"
    name: "scott"
    sid: "831a89053b54f3df7c9ca1bca92e1e10"
    sub: "5BE86359-073C-434B-AD2D-A3932222DABE"
    tenantId: "123456"
    
  • 定义自定义标识资源()

    我删除了MyProfileService

    public static IEnumerable<IdentityResource> GetIdentityResources()
    {
        var customProfile = new IdentityResource(
                                name: "custom.profile",
                                displayName: "Custom profile",
                                claimTypes: new[] {
                                    "name",
                                    "given_name",
                                    "family_name",
                                    "email",
                                    "email_verified",
                                    "website",
                                    "address",
                                    "status",
                                    "tenantid" });
    
    return new List<IdentityResource>
    {
        new IdentityResources.OpenId(),
        new IdentityResources.Profile(),
        customProfile
    };
    }
    

  • 问题是您已经删除了默认行为。因此,您需要通过将以下行添加到您的配置文件服务(它存在于DefaultProfileService中)来恢复它:

    但是没有必要实现您自己的IProfileService。在这种情况下,只需配置客户端的作用域即可:

    AllowedScopes = new List<string>
        {
            IdentityServerConstants.StandardScopes.OpenId,
            IdentityServerConstants.StandardScopes.Profile,
            "tenant.info",
            "api1"
        },
    
    options.Scope.Add("tenant.info");
    

    这应该足以包括tenantId索赔。

    我找不到oidc客户端中的
    options.Scope.Add
    函数的位置请查看。我希望您的项目中也有一个配置文件。您可以搜索字符串“openid profile api1”并将其更改为:“openid profile api1 tenant.info”。选项。范围是C代码。我没注意到你有一个棱角分明的客户。谢谢你的回复。是的,我有一个Angular客户端,我在
    allowedScopes
    中添加了“tenant.info”,并在我的
    UserManagerSettings.scope
    属性中添加了“tenant.info”。现在当我导航到identity server时,我得到了
    无效的\u范围
    我刚刚重新阅读了我的问题,很抱歉没有添加我使用的是Angular客户端,我想这会有所不同吗?请注意,默认情况下令牌保持较小。id令牌将仅包含子声明。为了获取更多信息,应该使用UserInfo端点。但您可以在上设置“AlwaysIncludeUserClaimsInIdToken”以始终包含索赔。
    profile:
    amr: ["pwd"]
    auth_time: 1553026892
    family_name: "FamilyName"
    given_name: "Scott givenName"
    idp: "local"
    name: "Scott name"
    sid: "47ae7f9b5240742e2b2b94a739bed5fa"
    sub: "5BE86359-073C-434B-AD2D-A3932222DABE"
    website: "http://scott.com"
    
    context.AddRequestedClaims(context.Subject.Claims);
    
    AllowedScopes = new List<string>
        {
            IdentityServerConstants.StandardScopes.OpenId,
            IdentityServerConstants.StandardScopes.Profile,
            "tenant.info",
            "api1"
        },
    
    options.Scope.Add("tenant.info");