当然不要忘记,官网才是最好的老师:docs.microsoft.com/zh-cn/dotnet/core/
沙盒学习指南: 免费环境docs.microsoft.com/zh-cn/learn/browse
posted @ 2020 初久的私房菜 推荐出品

Abp vNext 番外篇丨Claims扩展

 

正文

缘起

abp默认提供的Claims不够用,当系统需要添加自定义Claims.

正文

首先abp默认提供了'IAbpClaimsPrincipalContributor'接口来帮助实现,该接口实现会在颁发token的时候调用.

 public async Task ContributeAsync(AbpClaimsPrincipalContributorContext context)
        {
             //只是个简单例子
             var claimsIdentity = context.ClaimsPrincipal.Identities.FirstOrDefault();
             claimsIdentity.AddOrReplace(new Claim(xxxClaimsPrincipal.TenantPath, tenant.Name));
             claimsIdentity.AddOrReplace(new Claim(xxxClaimsPrincipal.TenantEnterpriseId, kuforeTenant.Data.EnterpriseByPath.Id));
             claimsIdentity.AddOrReplace(new Claim(xxxClaimsPrincipal.TenantOwnerUserId, kuforeTenant.Data.EnterpriseByPath.OwnerUserId));
             claimsIdentity.AddOrReplace(new Claim(xxxClaimsPrincipal.TenantActiveEdition, kuforeTenant.Data.EnterpriseByPath.ActiveEdition.ToString()));
        }

重点来了如果你只是这样用了,你会发现你在需要读取Claims的时候,你读不到数据,但是你断点你又发现命名赋值成功了。

这里我们找到'IdentityServerDataSeedContributor.cs',看下面代码应该懂得都懂了,如果你ids4的Claims中不包含会在颁发的时候移除掉你的Claims

        private async Task CreateApiResourcesAsync()
        {
            var commonApiUserClaims = new[]
            {
                "email",
                "email_verified",
                "name",
                "phone_number",
                "phone_number_verified",
                "role",
                xxxClaimsPrincipal.TenantPath,
                xxxClaimsPrincipal.TenantEnterpriseId,
                xxxClaimsPrincipal.TenantOwnerUserId,
                xxxClaimsPrincipal.TenantActiveEdition
            };

            await CreateApiResourceAsync("xxx", commonApiUserClaims);
        }

结语

也欢迎大家阅读我的Abp vNext系列教程

联系作者:加群:867095512 @MrChuJiu

posted @   初久的私房菜  阅读(2814)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
作者:初久的私房菜
好好学习,天天向上
返回顶部小火箭
好友榜:
如果愿意,把你的博客地址放这里
张弛:https://blog.zhangchi.fun/
点击右上角即可分享
微信分享提示