public async Task <ActionResult <TokenResult> > Refresh([FromForm] string token, [FromForm] string refreshToken) { ClaimsPrincipal principal = _tokenService.GetPrincipalFromExpiredToken(token); string name = GetNameFromClaimsPrincipal(principal); string email = GetEmailFromClaimsPrincipal(principal); User user = await _userManager.FindByEmailAsync(email); string savedRefreshToken = user.RefreshToken; if (savedRefreshToken != refreshToken) { throw new SecurityTokenException("Invalid refresh token"); } string newJwtToken = _tokenService.BuildToken(name, email); string newRefreshToken = _tokenService.GenerateRefreshToken(); user.RefreshToken = newRefreshToken; await _userManager.UpdateAsync(user); return(Ok(new TokenResult { Token = newJwtToken, RefreshToken = newRefreshToken })); }
public IActionResult GenerateToken([FromBody] TokenViewModel vm) { // todo call the command to validate user and then create token var token = _tokenService.BuildToken(vm.Email); return(Ok(new { token })); }
public async Task <string> LoginWithExternalIdentifier() { var loginInfo = await _signInManager.GetExternalLoginInfoAsync(); if (loginInfo == null) { throw new AppException(ExceptionEvent.LoginFailed); } var result = await _signInManager.ExternalLoginSignInAsync(loginInfo.LoginProvider, loginInfo.ProviderKey, isPersistent : false, bypassTwoFactor : true); if (!result.Succeeded) { await RegisterWithLogin(loginInfo); } var user = await _signInManager.UserManager.FindByLoginAsync(loginInfo.LoginProvider, loginInfo.ProviderKey); var roles = await _signInManager.UserManager.GetRolesAsync(user); return(_tokenService.BuildToken(user, roles)); }
//[AllowAnonymous] //[SkipInactiveUserFilter] public async Task <ActionResult <Jwt> > LoginAsync([FromBody] LoginVm loginVm) { var user = await _usersRepository.ReadByEmailAndPasswordAsync(loginVm.Email, Encryption.Encrypt(loginVm.Password)); if (user == null) { _logger.LogInformation("Unauthorized user attempted log in"); return(Unauthorized()); } var tokenString = _jwtTokenService.BuildToken(user); _logger.LogInformation($"User {user.Email} logged in"); return(Ok(new Jwt() { Token = tokenString })); }
public async Task <LoginResult> Handle(AuthCommand request, CancellationToken cancellationToken) { User user = await _userManager.FindByEmailAsync(request.Email); bool validCredentials = await _userManager.CheckPasswordAsync(user, request.Password); if (!validCredentials) { throw new InvalidCredentialException("Username or password is incorrect"); } string newJwtToken = _tokenService.BuildToken($"{user.FirstName} {user.LastName}", request.Email); string newRefreshToken = _tokenService.GenerateRefreshToken(); user.RefreshToken = newRefreshToken; await _userManager.UpdateAsync(user); return(new LoginResult { Token = newJwtToken, RefreshToken = newRefreshToken }); }
public async Task <JsonResult> Login([FromBody] LoginViewModel loginViewModel) { var response = new TokenViewModel(); var validator = new LoginValidator(); var validatorResult = validator.Validate(loginViewModel); if (validatorResult.IsValid) { var user = serviceOfAccount.Get(a => a.UserName == loginViewModel.Login); var correctUser = await userManager.CheckPasswordAsync(user, loginViewModel.Password); if (!correctUser) { response.Errors = new List <string>(); (response.Errors as List <string>).Add("Username or password is incorrect!"); return(Json(response)); } IList <string> roles = await userManager.GetRolesAsync(user); var claims = new List <Claim> { new Claim(ClaimsIdentity.DefaultNameClaimType, user.UserName), new Claim(ClaimsIdentity.DefaultRoleClaimType, roles.FirstOrDefault()), new Claim("UserId", user.Id) }; response.Token = _tokenService.BuildToken(user.Email, claims); } else { response.Errors = validatorResult.Errors.Select(a => a.ErrorMessage).ToList(); } return(Json(response)); }
private async Task <string> GenerateToken(IdentityUser user) => await tokenService.BuildToken(user);
// Generates a token from the token service and returns it as a string private string GenerateToken(string email) { var token = _tokenService.BuildToken(email); return(token); }
private string GenerateToken(User user, string role) { return(_tokenService.BuildToken(user, role)); }
public IActionResult GenerateToken([FromBody] Account account) { var token = _tokenService.BuildToken(account.Email); return(Ok(new { token })); }
private string GenerateToken(ApplicationUser applicationUser) { return(_tokenService.BuildToken(applicationUser)); }
public IActionResult GenerateToken([FromBody] TokenViewModel vm) { var token = _tokenService.BuildToken(vm.Email); return(Ok(new { token })); }
public IActionResult GeneraToken(ModelToken modelToken) { var Token = TokenServices.BuildToken(modelToken.Email); return(Ok(new { Token })); }
public IActionResult GenerateToken([FromBody] DisplayPersonModel vm) { var token = _tokenService.BuildToken(vm.EmailAddreess); return(Ok(new { token })); }
public string GenerateToken(string email) { var token = tokenService.BuildToken(email); return(token); }