コード例 #1
0
        /// <summary>
        /// 生成Claims
        /// </summary>
        /// <param name="userInfo">用户信息</param>
        /// <param name="loginTime">登录时间</param>
        /// <returns></returns>
        private Claim[] GenerateClaims(UserInfoPrincipal userInfo, DateTime loginTime)
        {
            var claims = new List <Claim>()
            {
                new Claim(CustomClaimTypes.StaffId, userInfo.StaffId.ToString()),
                new Claim(CustomClaimTypes.Account, userInfo.Account),
                new Claim(ClaimTypes.Name, userInfo.Name),
                new Claim(CustomClaimTypes.StaffType, userInfo.StaffType.ToString()),
                new Claim(CustomClaimTypes.Roles, Json.ToJson(userInfo.Roles)),
                new Claim(CustomClaimTypes.OfficeName, userInfo.OfficeName),
                new Claim(CustomClaimTypes.OfficeId, userInfo.OfficeName),
            };

            claims.AddRange(_identity.AppendClaims(loginTime));//追加身份自有Claims
            return(claims.ToArray());
        }
コード例 #2
0
        /// <summary>
        /// 生成token
        /// <remarks>create by xingbo 18/12/29</remarks>
        /// </summary>
        /// <param name="userInfo">用户信息</param>
        /// <param name="loginTime">登录时间</param>
        /// <returns></returns>
        public string GenerateToken(UserInfoPrincipal userInfo, DateTime?loginTime = null)
        {
            var claims = GenerateClaims(userInfo, loginTime ?? DateTime.Now);

            return(_identity.GenerateToken(SecurityKey, claims));
        }