【问题标题】:IdentityServer4 : How so you include additional claims and get the value in an API controller?IdentityServer4:如何在 API 控制器中包含附加声明并获取值?
【发布时间】:2017-11-16 03:50:40
【问题描述】:

【问题讨论】:

    标签: c# model-view-controller asp.net-core identityserver4


    【解决方案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
                    }
                }
            };
        }
    

    【讨论】:

    猜你喜欢
    • 2020-06-16
    • 2021-10-06
    • 2017-05-31
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 2017-07-08
    • 1970-01-01
    • 2020-03-27
    相关资源
    最近更新 更多