一、将信息存储到claim中
claims.AddRange(new[] { //新增身份,添加租户id new Claim("RoleName","管理员111"), new Claim(JwtRegisteredClaimNames.Sub, nameIdClaim.Value), new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()), new Claim(JwtRegisteredClaimNames.Iat, DateTimeOffset.Now.ToUnixTimeSeconds().ToString(), ClaimValueTypes.Integer64) });
像这样,将“管理员111”添加到claim中
二、扩展AbpSession
我们在application层新增AbpSession文件夹,然后添加IAbpSessionExtension
接口继承自IAbpSession
namespace ZhuanKou.AbpSession { public interface IAbpSessionExtension : IAbpSession { string RoleName { get; } } }
添加AbpSessionExtension
类,基础自ClaimsAbpSession
并实现IAbpSessionExtension
接口
namespace ZhuanKou.AbpSession { public class AbpSessionExtension : ClaimsAbpSession, IAbpSessionExtension { public AbpSessionExtension(IPrincipalAccessor principalAccessor, IMultiTenancyConfig multiTenancy, ITenantResolver tenantResolver, IAmbientScopeProvider<SessionOverride> sessionOverrideScopeProvider) : base(principalAccessor, multiTenancy, tenantResolver, sessionOverrideScopeProvider) { } public string RoleName => GetClaimValue("RoleName"); private string GetClaimValue(string claimType) { var claimsPrincipal = PrincipalAccessor.Principal; var claim = claimsPrincipal?.Claims.FirstOrDefault(c => c.Type == claimType); if (string.IsNullOrEmpty(claim?.Value)) return null; return claim.Value; } } }
注入到application的base基类中
public new IAbpSessionExtension AbpSession { get; set; }
再注入web core的ZhuanKouControllerBase中
然后就可以开始使用AbpSession.RoleName了