/// <summary> /// 生成用户token信息 /// </summary> /// <param name="userCode"></param> /// <returns></returns> JwtTokenInfo GenerateToken(string userCode) { DateTime now = DateTime.Now; JwtTokenInfo tokenInfo = new JwtTokenInfo(); var claims = new[] { new Claim(JwtRegisteredClaimNames.Nbf, $"{new DateTimeOffset(now).ToUnixTimeSeconds()}"), new Claim(JwtRegisteredClaimNames.Exp, $"{new DateTimeOffset(now.AddMinutes(_jwtSettings.EffectMinutes)).ToUnixTimeSeconds()}"), new Claim(ClaimTypes.Name, userCode), new Claim(ClaimTypes.Role, "roleAdmin") }; var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtSettings.SecurityKey)); var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); var token = new JwtSecurityToken( issuer: _jwtSettings.Domain, audience: _jwtSettings.Domain, claims: claims, expires: DateTime.Now.AddMinutes(30), signingCredentials: creds); tokenInfo.Token = new JwtSecurityTokenHandler().WriteToken(token); tokenInfo.TokenId = Encrypion.GenerateMD5(tokenInfo.Token); tokenInfo.IssuedAt = now; tokenInfo.Expires = now.AddMinutes(_jwtSettings.EffectMinutes); tokenInfo.Issuer = _jwtSettings.Domain; return(tokenInfo); }
/// <summary> /// 保存用户的token信息 /// </summary> /// <param name="token"></param> /// <returns></returns> public void SaveUserToken(JwtTokenInfo token) { _userRepository.Add(_mapper.Map <UserToken>(token)); _unitOfWork.Commit(); }