public async Task <IActionResult> Login(LoginModel model) { var user = await _userManager.FindByNameAsync(model.UserName).ConfigureAwait(false); if (user != null) { var result = await _signInManager.CheckPasswordSignInAsync(user, model.Password, lockoutOnFailure : true).ConfigureAwait(false); if (result.Succeeded == false) { throw new UnauthorizedAccessException(AccountController.InvalidDataException); } var claims = AccountController.FillClaims(user); var token = _jwtTokenService.GenerateJwtToken(claims, model.IsRememberMe); var refreshToken = _jwtTokenService.GenerateRefreshToken(); user.RefreshToken = refreshToken; await _userManager.UpdateAsync(user).ConfigureAwait(false); return(Ok(new { token = token, refreshToken = refreshToken })); } throw new KeyNotFoundException(AccountController.UserNotFoundException); }
public LoginResponse AuthenticateUser(LoginRequest loginRequest) { var loginResponse = new LoginResponse(); if (!userRepository.IsUserExists(loginRequest.UserName)) { return(loginResponse); } loginResponse.IsUserExists = true; if (!userRepository.IsUserCredentialsValid(loginRequest.UserName, loginRequest.Password)) { return(loginResponse); } var user = userRepository.GetUserDetails(loginRequest.UserName); var jwtTokenRequest = new JwtTokenRequest { Email = user.Email, FirstName = user.FirstName, LastName = user.LastName, JsonWebToken = landmarkService.GetJsonWebTokenSection() }; loginResponse.Token = jwtTokenService.GenerateJwtToken(jwtTokenRequest); return(loginResponse); }
public void SaveUserIdamDataToCookie(IEnumerable <Claim> claims, HttpContext context) { // store claims into a secure cookie var jwtToken = _jwtTokenService.GenerateJwtToken(claims, Settings.JwtTokenSecurityKey); var encodedJwtToken = _encryptionService.Encode(jwtToken); var userDataCookie = new CookieHelper(Settings.UserDataCookieName, context); userDataCookie.SetValue(Settings.UserDataCookieKey, encodedJwtToken); userDataCookie.Save(); }
public async Task <object> LogIn([FromBody] CredentialsViewModel credentials) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = await _userService.LogIn(credentials.Login, credentials.Password); if (user == null) { ModelState.AddModelError(ErrorKeyUnableToLogIn, string.Empty); return(BadRequest(ModelState)); } var role = await _userService.GetUserRole(user); return(Task.FromResult(_jwtTokenService.GenerateJwtToken(user, role))); }
public async Task <IActionResult> Login(UserForLogin userForLogin) { var user = await userManager.FindByNameAsync(userForLogin.Username); if (user == null) { return(Unauthorized()); } var result = await signInManager.CheckPasswordSignInAsync(user, userForLogin.Password, false); if (result.Succeeded) { var roles = await userManager.GetRolesAsync(user); return(Ok(new { token = jwtTokenService.GenerateJwtToken(user, roles) })); } return(Unauthorized()); }
public async Task <SignInResponse> SignIn(SignInRequest request) { var user = await _userRepository.GetByUserName(request.Username); if (user == null) { throw new WrongCredentials("Wrong credentials!"); } if (!PasswordService.VerifyPasswordHash(request.Password, user.PasswordHash, user.PasswordSalt)) { throw new WrongCredentials("Wrong credentials!"); } var token = _jwtTokenService.GenerateJwtToken(user); return(new SignInResponse() { Token = token }); }
private async Task <string> Authenticate(string username, string password) { var loginResult = await _signInManager.PasswordSignInAsync(username, password, false, false); if (!loginResult.Succeeded) { _logger.LogError("Bad Api Authentication Attempt", new { username, loginResult, ipAddress = HttpContext.Connection.RemoteIpAddress }); return(null); } var user = await _userService.Find(username); if (user == null) { return(null); } var token = _jwtTokenService.GenerateJwtToken(user.Id); return(token); }
public async Task <IActionResult> Refresh(RefreshTokenModel requestModel) { var principal = _jwtTokenService.GetPrincipalFromExpiredToken(requestModel.Token); var username = principal.Claims.SingleOrDefault(x => x.Type == TokenClaims.UserName).Value; var user = await _userManager.FindByNameAsync(username).ConfigureAwait(false); if (user == null || user.RefreshToken != requestModel.RefreshToken) { return(BadRequest()); } var newJwtToken = _jwtTokenService.GenerateJwtToken(principal.Claims, null); var newRefreshToken = _jwtTokenService.GenerateRefreshToken(); user.RefreshToken = newRefreshToken; await _userManager.UpdateAsync(user).ConfigureAwait(false); return(Ok(new { token = newJwtToken, refreshToken = newRefreshToken })); }
public async Task <UserDataDTO> Login(UserForLoginDTO userForLoginDto) { var user = await _userManager.FindByNameAsync(userForLoginDto.UserName); var result = await _signInManager.CheckPasswordSignInAsync(user, userForLoginDto.Password, false); if (result.Succeeded) { var appUser = await _userManager.Users.FirstOrDefaultAsync(x => x.NormalizedUserName == userForLoginDto.UserName.ToUpper()); var claims = GetClaims(user); return(new UserDataDTO { UserToReturnDTO = new UserToReturnDTO { Id = user.Id, UserName = user.UserName }, Token = await _jwtTokenService.GenerateJwtToken(claims) }); } throw new UnauthorizedException("Invalid creidentionals"); }
/// <summary> /// Generates a HmacSha256 encoded <see cref="Token"/> from a <see cref="GenericPrincipal"/> and an expiration <see cref="TimeSpan"/> /// </summary> /// <param name="request"></param> /// <returns></returns> public Task <Token> GenerateJwtTokenAsync(TokenRequest request) { return(Task.FromResult(jwtTokenService.GenerateJwtToken(request))); }