public async Task <UserTokenResponse> CreateTokenAsync(UserCredentialsRequest userCredentialsRequest) { var uri = new Uri($"{AuthUrl}/token"); try { var userJson = JsonConvert.SerializeObject(userCredentialsRequest); var content = new StringContent(userJson, Encoding.UTF8, "application/json"); var response = await _client.PostAsync(uri, content).ConfigureAwait(false); if (response.IsSuccessStatusCode) { var userTokenResponseJson = await response.Content.ReadAsStringAsync().ConfigureAwait(false); var userTokenResponse = JsonConvert.DeserializeObject <UserTokenResponse>(userTokenResponseJson); return(await Task.FromResult(userTokenResponse).ConfigureAwait(false)); } } catch (Exception e) { Debug.WriteLine(e.Message); } return(null); }
public async Task <AuthResponse> LoginAsync(UserCredentialsRequest credentials) { AuthResponse response = new(); User existingUser = await _unitOfWork.UserManager.FindByNameAsync(credentials.Username); if (existingUser is null) { response.Errors = new[] { _locale["UserDoesNotExist"].Value }; return(response); } var signInResult = await _unitOfWork.SignInManager.PasswordSignInAsync(credentials.Username, credentials.Password, isPersistent : true, lockoutOnFailure : true); if (!signInResult.Succeeded) { string message = signInResult.IsLockedOut ? _locale["UserLockedOut"].Value : _locale["IncorrectPassword"].Value; response.Errors = new[] { message }; return(response); } response.Token = await GenerateJwtToken(existingUser); return(response); }
public IActionResult Authenticate([FromBody] UserCredentialsRequest userParam) { var user = _userService.Authenticate(userParam.Username, userParam.Password); if (user == null) { return(BadRequest(new { message = "Username or password is incorrect" })); } return(Ok(user)); }
public async Task <ActionResult <AuthResponse> > Login([FromBody] UserCredentialsRequest credentials) { var authResponse = await _identityService.LoginAsync(credentials); if (authResponse.Errors is not null) { return(BadRequest(authResponse)); } return(authResponse); }
public async Task <ActionResult <AuthResponse> > ChangePassword([FromBody] UserCredentialsRequest credentials) { string userId = HttpContext.User.FindFirst("id").Value; bool userHasName = await _identityService.CheckIfUserHasName(userId, credentials.Username); if (!userHasName && !HttpContext.User.IsInRole("Admin")) { return(Forbid()); } var response = await _identityService.ChangePasswordAsync(credentials); if (response.Errors is null) { return(NoContent()); } return(BadRequest(response)); }
public async Task <AuthResponse> ChangePasswordAsync(UserCredentialsRequest credentials) { AuthResponse response = new(); User existingUser = await _unitOfWork.UserManager.FindByNameAsync(credentials.Username); if (existingUser is null) { response.Errors = new[] { _locale["UserDoesNotExist"].Value }; return(response); } string resetToken = await _unitOfWork.UserManager.GeneratePasswordResetTokenAsync(existingUser); var resetResult = await _unitOfWork.UserManager.ResetPasswordAsync(existingUser, resetToken, credentials.Password); if (!resetResult.Succeeded) { response.Errors = resetResult.Errors.Select(er => er.Description).ToArray(); } return(response); }