public async Task <ActionResult <UserDto> > Register(RegisterDto registerDto) { if (await UserExists(registerDto.Username)) { return(BadRequest("Username is taken")); } using var hmac = new HMACSHA512(); var user = new MtoUser { UserName = registerDto.Username.ToLower(), PasswordHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(registerDto.Password)), PasswordSalt = hmac.Key }; _context.Users.Add(user); await _context.SaveChangesAsync(); return(new UserDto { Username = user.UserName, Token = _tokenService.CreateToken(user) }); }
public string CreateToken(MtoUser user) { var claims = new List <Claim> { new Claim(JwtRegisteredClaimNames.NameId, user.UserName) }; var creds = new SigningCredentials(_key, SecurityAlgorithms.HmacSha512Signature); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(claims), Expires = DateTime.Now.AddDays(7), SigningCredentials = creds }; var tokenHandler = new JwtSecurityTokenHandler(); var token = tokenHandler.CreateToken(tokenDescriptor); return(tokenHandler.WriteToken(token)); }
public void Update(MtoUser user) { _context.Entry(user).State = EntityState.Modified; }