ABP core 扩展 AbpSession

自己的项目用ABP原来的AbpSession,需要增加一个自定义的公司ID属性,这里参考:https://www.jianshu.com/p/a6e9ace79345作者的文章自行扩展了一下AbpSession,代码如下:

1,在以.Core结尾的项目中增加扩展类:


 

代码如下:

 public static class CustomerJwtExtension    {        

        public static int GetCompanyId(this IAbpSession abpSession)        {            

                var re = GetClaimValue(AbpClaimTypes.CompanyId);           

                 return string.IsNullOrEmpty(re) ? 0 : Convert.ToInt32(re);        

}       

private static string GetClaimValue(string claimType)        {            

        var PrincipalAccessor = IocManager.Instance.Resolve(); // 使用IOC容器获取当前用户身份认证信息

                var claimsPrincipal = PrincipalAccessor.Principal;

                var claim = claimsPrincipal?.Claims.FirstOrDefault(c => c.Type == claimType);

                if (string.IsNullOrEmpty(claim?.Value))

                    return null;

                return claim.Value;

        }

}

自定义结构体:

public struct AbpClaimTypes

public const string CompanyId = "CompanyId";

使用:

var com = AbpSession.GetCompanyId();

猜你喜欢

转载自www.cnblogs.com/rokeyyan/p/rokeyyan.html
ABP