public async Task <AuthResult <Token> > RestorePassword(RestorePasswordDTO restorePasswordDto) { if (restorePasswordDto == null || string.IsNullOrEmpty(restorePasswordDto.Email) || string.IsNullOrEmpty(restorePasswordDto.Token) || string.IsNullOrEmpty(restorePasswordDto.NewPassword) || string.IsNullOrEmpty(restorePasswordDto.ConfirmPassword) || string.IsNullOrEmpty(restorePasswordDto.ConfirmPassword) || restorePasswordDto.ConfirmPassword != restorePasswordDto.NewPassword ) { return(AuthResult <Token> .UnvalidatedResult); } var user = await userManager.FindByEmailAsync(restorePasswordDto.Email); if (user != null) { var result = await userManager.ResetPasswordAsync(user, restorePasswordDto.Token, restorePasswordDto.NewPassword); if (result.Succeeded) { var token = jwtManager.GenerateToken(user); return(AuthResult <Token> .TokenResult(token)); } } return(AuthResult <Token> .UnvalidatedResult); }
public async Task <AuthResult <Token> > RefreshToken(RefreshTokenDTO refreshTokenDto) { var refreshToken = refreshTokenDto?.Token?.Refresh_token; if (string.IsNullOrEmpty(refreshToken)) { return(AuthResult <Token> .UnvalidatedResult); } try { var principal = jwtManager.GetPrincipal(refreshToken, isAccessToken: false); var userId = principal.GetUserId(); var user = await userManager.FindByIdAsync(userId.ToString()); if (user != null) { var token = jwtManager.GenerateToken(user); return(AuthResult <Token> .TokenResult(token)); } } catch (Exception) { return(AuthResult <Token> .UnauthorizedResult); } return(AuthResult <Token> .UnauthorizedResult); }
public async Task <AuthResult <Token> > RefreshToken(RefreshTokenDTO refreshTokenDto) { var refreshToken = refreshTokenDto?.Token?.Refresh_token; if (string.IsNullOrEmpty(refreshToken)) { return(AuthResult <Token> .UnvalidatedResult); } try { //var principal = JwtManager.GetPrincipal(refreshToken, isAccessToken: false); //int.TryParse(principal.Identity.GetUserId(), out var currentUserId); var user = new IdentityAppUser(); //await _userManager.FindByIdAsync(currentUserId); JwtSecurityToken if (user != null && !string.IsNullOrEmpty(user.Id) && !user.Deleted) { var token = new Token();// JwtManager.GenerateToken(await _userManager.CreateIdentityAsync(user)); return(await Task.FromResult(AuthResult <Token> .TokenResult(token))); } } catch (Exception) { return(AuthResult <Token> .UnauthorizedResult); } return(AuthResult <Token> .UnauthorizedResult); }
public async Task <AuthResult <Token> > RestorePassword(RestorePasswordDTO restorePasswordDto) { if (restorePasswordDto == null || string.IsNullOrEmpty(restorePasswordDto.Email) || string.IsNullOrEmpty(restorePasswordDto.Token) || string.IsNullOrEmpty(restorePasswordDto.NewPassword) || string.IsNullOrEmpty(restorePasswordDto.ConfirmPassword) || string.IsNullOrEmpty(restorePasswordDto.ConfirmPassword) || restorePasswordDto.ConfirmPassword != restorePasswordDto.NewPassword ) { return(AuthResult <Token> .UnvalidatedResult); } var user = await _userManager.FindByEmailAsync(restorePasswordDto.Email); if (user != null && !string.IsNullOrEmpty(user.Id) && !user.Deleted) { var result = await _userManager.ResetPasswordAsync(user, restorePasswordDto.Token, restorePasswordDto.NewPassword); if (result.Succeeded) { var token = new Token(); // JwtManager.GenerateToken(await _userManager.claim CreateIdentityAsync( (user, )); return(AuthResult <Token> .TokenResult(token)); } } return(AuthResult <Token> .UnvalidatedResult); }
public async Task <AuthResult <Token> > SignUp(SignUpDTO signUpDto) { if (signUpDto == null || string.IsNullOrEmpty(signUpDto.Username) || string.IsNullOrEmpty(signUpDto.Password) || string.IsNullOrEmpty(signUpDto.ConfirmPassword) || string.IsNullOrEmpty(signUpDto.FullName) || string.IsNullOrEmpty(signUpDto.Email) || signUpDto.Password != signUpDto.ConfirmPassword ) { return(AuthResult <Token> .UnvalidatedResult); } var newUser = new TUser { Username = signUpDto.Username, Email = signUpDto.Email, FullName = signUpDto.FullName }; var succeeded = await userManager.CreateAsync(newUser, signUpDto.Password); if (succeeded) { if (newUser.Id > 0) { await userManager.AddToRoleAsync(newUser.Id, Roles.User); var token = jwtManager.GenerateToken(newUser); return(AuthResult <Token> .TokenResult(token)); } } return(AuthResult <Token> .UnauthorizedResult); }
public async Task <AuthResult <Token> > RefreshToken(RefreshTokenDTO refreshTokenDto) { var refreshToken = refreshTokenDto?.Token?.Refresh_token; if (string.IsNullOrEmpty(refreshToken)) { return(AuthResult <Token> .UnvalidatedResult); } try { var principal = jwtManager.GetPrincipal(refreshToken, isAccessToken: false); int.TryParse(principal.Identity.GetUserId(), out var currentUserId); var user = await userManager.FindByIdAsync(currentUserId); if (user != null && user.Id > 0 && !user.IsDeleted) { var token = jwtManager.GenerateToken(user); return(AuthResult <Token> .TokenResult(token)); } } catch (Exception) { return(AuthResult <Token> .UnauthorizedResult); } return(AuthResult <Token> .UnauthorizedResult); }
public async Task <AuthResult <string> > RequestPassword(RequestPasswordDTO requestPasswordDto) { if (requestPasswordDto == null || string.IsNullOrEmpty(requestPasswordDto.Email)) { return(AuthResult <string> .UnvalidatedResult); } var user = await userManager.FindByEmailAsync(requestPasswordDto.Email); if (user != null) { var passwordResetToken = await userManager.GeneratePasswordResetTokenAsync(user); return(AuthResult <string> .TokenResult(passwordResetToken)); } return(AuthResult <string> .UnvalidatedResult); }
public async Task <AuthResult <Token> > Login(LoginDTO loginDto) { if (loginDto == null || string.IsNullOrEmpty(loginDto.Email) || string.IsNullOrEmpty(loginDto.Password)) { return(AuthResult <Token> .UnvalidatedResult); } var user = await userManager.FindByEmailAsync(loginDto.Email); if (user != null) { if (await userManager.CheckPasswordAsync(user, loginDto.Password)) { var token = jwtManager.GenerateToken(user); return(AuthResult <Token> .TokenResult(token)); } } return(AuthResult <Token> .UnauthorizedResult); }
public async Task <AuthResult <Token> > SignUp(SignUpDTO signUpDto) { try { if (signUpDto == null || string.IsNullOrEmpty(signUpDto.Email) || string.IsNullOrEmpty(signUpDto.Password) || string.IsNullOrEmpty(signUpDto.ConfirmPassword) || string.IsNullOrEmpty(signUpDto.FirstName) || string.IsNullOrEmpty(signUpDto.LastName) || signUpDto.Password != signUpDto.ConfirmPassword ) { return(AuthResult <Token> .UnvalidatedResult); } var newUser = new TUser { SecondName = signUpDto.LastName, FirstName = signUpDto.FirstName, Email = signUpDto.Email, UserName = signUpDto.Email }; var result = await userManager.CreateAsync(newUser, signUpDto.Password); if (result.Succeeded) { //await userManager.AddToRoleAsync(newUser, "User"); var token = jwtManager.GenerateToken(newUser); return(AuthResult <Token> .TokenResult(token)); } return(AuthResult <Token> .UnauthorizedResult); } catch (Exception e) { Console.Write(e.Message); throw; } }