public async Task <JwtWithRefreshToken> Refresh(string token, string refreshToken) { IJWTService jwtService = SecurityServiceFactory.GetService(typeof(IJWTService)) as IJWTService; IIdentityUserService identityUserService = SecurityServiceFactory.GetService(typeof(IIdentityUserService)) as IIdentityUserService; var principal = jwtService.GetPrincipalFromExpiredToken(token); var username = principal.Identity.Name; var identityUserId = principal.Claims.Single(x => x.Type == "nameidentifier").Value; var applicationUser = await identityUserService.GetApplicationUserAsync(identityUserId); var savedRefreshToken = GetRefreshToken(identityUserId); //retrieve the refresh token from a data store if (savedRefreshToken != refreshToken) { throw new SecurityTokenException("Invalid refresh token"); } var newJwtToken = jwtService.GenerateJwtToken(applicationUser); var newRefreshToken = GenerateRefreshToken(); DeleteRefreshToken(username, refreshToken); SaveRefreshToken(username, newRefreshToken); return(new JwtWithRefreshToken { AccessToken = newJwtToken.AccessToken, Expires = newJwtToken.Expires, Issued = newJwtToken.Issued, RefreshToken = newRefreshToken, }); }
public async Task <Jwt> LoginWithoutEmailConfirmation(LoginModel model) { Jwt jwt = null; IJWTService jwtService = SecurityServiceFactory.GetService(typeof(IJWTService)) as IJWTService; ApplicationUser user = await UserManager.FindByEmailAsync(model.Email); if (user == null) { throw new InvalidOperationException("Incorrect email or password"); } bool isPasswordCorrect = await UserManager.CheckPasswordAsync(user, model.Password); if (!isPasswordCorrect) { throw new InvalidOperationException("Incorrect email or password"); } if (model.RememberMe) { jwt = await jwtService.GenerateJWtWithRefreshTokenAsync(user); } else { jwt = jwtService.GenerateJwtToken(user); } return(jwt); }
public async Task <string> Register(RegistrationDTO model) { var user = new StrategyGameUser { UserName = model.Email, Email = model.Email }; var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { try { await _orszagService.MakeOrszagUserConnection(user, model.CountryName); } catch (Exception e) { await _userManager.DeleteAsync(user); throw e; } await _signInManager.SignInAsync(user, false); return(await _jwtService.GenerateJwtToken(model.Email, user)); } throw new ApplicationException("UNKNOWN_ERROR"); }
public async Task <string> Login(LoginDTO model) { var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, false, false); if (result.Succeeded) { var appUser = _userManager.Users.SingleOrDefault(r => r.Email == model.Email); return(await _jwtService.GenerateJwtToken(model.Email, appUser)); } throw new ApplicationException("INVALID_LOGIN_ATTEMPT"); }
public async Task <IActionResult> Login([FromBody] LoginModel model) { IActionResult response = Unauthorized(); var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, false, false); if (result.Succeeded) { var appUser = _userManager.Users.SingleOrDefault(r => r.Email == model.Email); //string Token = await GenerateJwtToken(model.Email, appUser); var jwt = _jwtService.GenerateJwtToken(appUser); response = Ok(new { token = jwt }); } return(response); }
public async Task <Jwt> Login(LoginModel model, Func <string, string, string> generateConfirmationLink, string PathToEmailFile) { try { Jwt jwt = null; IJWTService jwtService = SecurityServiceFactory.GetService(typeof(IJWTService)) as IJWTService; IIdentityUserService identityService = SecurityServiceFactory.GetService(typeof(IIdentityUserService)) as IIdentityUserService; ApplicationUser user = await UserManager.FindByEmailAsync(model.Email); if (user == null) { throw new InvalidOperationException("Incorrect email or password"); } bool isPasswordCorrect = await UserManager.CheckPasswordAsync(user, model.Password); if (!isPasswordCorrect) { throw new InvalidOperationException("Incorrect email or password"); } var isEmailConfirmed = await UserManager.IsEmailConfirmedAsync(user); if (!isEmailConfirmed) { await identityService.ResendEmailConfirmationAsync(user.Email, generateConfirmationLink, PathToEmailFile); throw new InvalidOperationException($"{user.Email} Has Not Been Confirmed. Please Retry Email Confirmation"); } if (model.RememberMe) { jwt = await jwtService.GenerateJWtWithRefreshTokenAsync(user); } else { jwt = jwtService.GenerateJwtToken(user); } return(jwt); } catch (Exception ex) { throw ex; } }