首页 文章

IdentityServer4:如何在API控制器中包含其他声明并获取值?

提问于
浏览

1 回答

  • 1

    定义api资源(看看Config.cs)时,可以这样做:

    new ApiResource
    {
        Name = "api",
        DisplayName = "My API",
    
        UserClaims =
        {
            JwtClaimTypes.Id,
            JwtClaimTypes.Subject,
            JwtClaimTypes.Email
        }
    }
    

    它定义了您的API将接收这些声明 .

    编辑:

    如果将关联资源添加到GetIdentityResources函数中会更好(请参阅Config.cs)

    在官方文档中浏览一下以获得更好的图片http://docs.identityserver.io/en/release/topics/resources.html .

    我从个人项目中给出了一个完整的例子:

    public static IEnumerable<IdentityResource> GetIdentityResources()
        {
            //>Declaration
            var lIdentityResources = new List<IdentityResource>
            {
                new IdentityResources.OpenId(),
                new IdentityResources.Profile(),
                new IdentityResources.Email()
            };
    
            //>Processing
            foreach (var lAPIResource in GetApiResources())
            {
                lIdentityResources.Add(new IdentityResource(lAPIResource.Name,
                                                            lAPIResource.UserClaims));
            }
    
            //>Return
            return lIdentityResources;
        }
    
        public static IEnumerable<ApiResource> GetApiResources()
        {
            return new List<ApiResource>
            {
                new ApiResource
                {
                    Name = "api1",
                    DisplayName = "api1 API",
    
                    UserClaims =
                    {
                        JwtClaimTypes.Id,
                        JwtClaimTypes.Subject,
                        JwtClaimTypes.Email
                    }
                }
            };
        }
    

相关问题