public static string Gen(string username) { var tokenHandler = new System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler(); ConfigurationTocken ctkn = new ConfigurationTocken(); var key = System.Text.Encoding.ASCII.GetBytes(ctkn.SecretKey); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, username) }), Expires = DateTime.UtcNow.AddMinutes(ctkn.timeExpire), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) }; var token = tokenHandler.CreateToken(tokenDescriptor); var tokenString = tokenHandler.WriteToken(token); //var userClaims = await _userManager.GetRolesAsync(user); List <Claim> claims = new List <Claim>(); claims.Add(new Claim(JwtRegisteredClaimNames.Sub, username)); claims.Add(new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())); claims.Add(new Claim(JwtRegisteredClaimNames.Iat, DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString(), ClaimValueTypes.Integer64)); var jwt = new JwtSecurityToken( claims: claims); var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt); return(encodedJwt); }
public static string GenerateTokenJwt(string username) { var ctkn = new ConfigurationTocken(); var secretKey = ctkn.SecretKey; var audienceToken = ctkn.host; var issuerToken = ctkn.host; var expireTime = ctkn.timeExpire; var securityKey = new SymmetricSecurityKey(System.Text.Encoding.Default.GetBytes(secretKey)); var signingCredentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256Signature); ClaimsIdentity claimsIdentity = new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, username) }); var tokenHandler = new System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler(); var jwtSecurityToken = tokenHandler.CreateJwtSecurityToken( audience: audienceToken, issuer: issuerToken, subject: claimsIdentity, notBefore: DateTime.UtcNow, expires: DateTime.UtcNow.AddMinutes(Convert.ToInt32(expireTime)), signingCredentials: signingCredentials); var jwtTokenString = tokenHandler.WriteToken(jwtSecurityToken); return(jwtTokenString); }