private string CreateJWT(UsersJWT user) { var secretKey = configuration.GetSection("AppSettings:Key").Value; var key = new SymmetricSecurityKey(Encoding.UTF8 .GetBytes(secretKey)); var claims = new Claim[] { new Claim(ClaimTypes.Name, user.Username), new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()) }; var signingCredentials = new SigningCredentials( key, SecurityAlgorithms.HmacSha256Signature); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(claims), Expires = DateTime.UtcNow.AddMinutes(1), SigningCredentials = signingCredentials }; var tokenHandler = new JwtSecurityTokenHandler(); var token = tokenHandler.CreateToken(tokenDescriptor); return(tokenHandler.WriteToken(token)); }
public void Register(string userName, string password) { byte[] passwordHash, passwordKey; using (var hmac = new HMACSHA512()) { passwordKey = hmac.Key; passwordHash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password)); } UsersJWT user = new UsersJWT(); user.Username = userName; user.Password = passwordHash; user.PasswordKey = passwordKey; dc.Users.Add(user); }