/// <summary> /// 生成jwt的token数据 /// </summary> /// <param name="_requirement"></param> /// <param name="jcm"></param> /// <returns>jwtData</returns> public static dynamic BuildJwtToken(PermissionRequirement _requirement, JwtClaimModel jcm) { // 写入jwt里的数据 var claims = new List <Claim> { // 原始Jwt用户Guid new Claim(JwtClaimTypes.JwtGuidUser, jcm.JwtGuidUser), // 当前/代理GuidUser new Claim(JwtClaimTypes.GuidUser, jcm.GuidUser), // 当前/代理GuidGroup new Claim(JwtClaimTypes.GuidGroup, jcm.GuidGroup), // 当前/代理Type new Claim(JwtClaimTypes.Type, jcm.Type), // 过期时间(2小时):2020/3/18 10:39:54 new Claim(JwtClaimTypes.Expiration, DateTime.Now.AddSeconds(_requirement.Expiration.TotalSeconds).ToString()) }; // 生成jwt的token数据 dynamic jwtData = JwtToken.BuildJwtToken(claims.ToArray(), _requirement); return(jwtData); }
public async Task <IActionResult> Alert() { // 请求响应实体工厂类 // 定义JwtClaimModel,初次登录 JwtClaimModel jcm = new JwtClaimModel(); jcm.JwtGuidUser = Guid.NewGuid().ToString("N");; jcm.GuidUser = jcm.JwtGuidUser; jcm.GuidGroup = ""; jcm.Type = ""; // 生成jwt的token数据 dynamic jwtData = AuthHelper.Cas.Jwt.BuildJwtToken(_requirement, jcm); // 将用户数据写入Redis, 1分钟有效,老系统使用后销毁 string redisKey = "SerpLogin:" + jwtData.token_md5; response.SetData(jwtData); return(Ok(response)); }
public async Task <IActionResult> Register(string userName, string password, string vcode, string email) { // 请求响应实体工厂类 var response = ResponseModelFactory.CreateInstance; // 定义JwtClaimModel,初次登录 JwtClaimModel jcm = new JwtClaimModel(); jcm.JwtGuidUser = Guid.NewGuid().ToString("N");; jcm.GuidUser = jcm.JwtGuidUser; jcm.GuidGroup = ""; jcm.Type = ""; // 生成jwt的token数据 dynamic jwtData = AuthHelper.Cas.Jwt.BuildJwtToken(_requirement, jcm); // 将用户数据写入Redis, 1分钟有效,老系统使用后销毁 string redisKey = "SerpLogin:" + jwtData.token_md5; response.SetData(jwtData); return(Ok(response)); }
public JwtDto GenerateToken(JwtClaimModel model) { var jwtToken = GetJwtModel(); var secretKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtToken.JwtKey)); Type t = model.GetType(); var claims = new List <Claim>(); foreach (var property in t.GetProperties()) { claims.Add(new Claim(property.Name, (string)t.GetProperty(property.Name).GetValue(model) ?? "")); } claims.Add(new Claim(JwtRegisteredClaimNames.Acr, new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds().ToString())); claims.Add(new Claim(JwtRegisteredClaimNames.Nbf, new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds().ToString())); claims.Add(new Claim(JwtRegisteredClaimNames.Exp, new DateTimeOffset(DateTime.Now.AddDays(1)).ToUnixTimeSeconds().ToString())); var token = new JwtSecurityToken( issuer: jwtToken.JwtIssuer, audience: jwtToken.JwtAudience, claims: claims, notBefore: DateTime.Now, expires: DateTime.Now.AddDays(jwtToken.JwtExpireDays), signingCredentials: new SigningCredentials(secretKey, SecurityAlgorithms.HmacSha256) ); return(new JwtDto() { Type = JwtBearerDefaults.AuthenticationScheme, AccessToken = new JwtSecurityTokenHandler().WriteToken(token), Expires = DateTime.Now.AddDays(jwtToken.JwtExpireDays) }); }