/// <summary> /// Authenticate user /// </summary> /// <param name="userLogOn"></param> /// <returns>Token</returns> public async Task<JwtSecurityToken> Authenticate(LogOnDto userLogOn) { UserAccountDto user = await this.FindUserByUsername(userLogOn.Username); if (user == null) { throw new System.Security.Authentication.InvalidCredentialException(); } var hash = Crypto.GetHashedPassword(user.Salt, userLogOn.Password); if (!hash.SequenceEqual(user.PasswordHash)) { throw new System.Security.Authentication.InvalidCredentialException(); } return GenerateToken(user); }
public async Task <IActionResult> LogIn([FromBody] LogOnDto model) { JwtSecurityToken token = null; if (ModelState.IsValid) { token = await _userAccountApplicationService.Authenticate(model); } else { ModelState.AddModelError("", "Error"); } return(Ok( new { response = new JwtSecurityTokenHandler().WriteToken(token) } )); }