public async Task <AuthResponse> ResetPassword(ResetPasswordRequestModel model) { AuthUser user = await this.userManager.FindByEmailAsync(model.Email); if (user == null) { return(ValidationResponseFactory <AuthResponse> .FailureAuthResponse("User not found", AuthErrorCodes.UserDoesNotExist)); } else { string confirmationToken = await this.userManager.GeneratePasswordResetTokenAsync(user); string confirmationLink = $"{this.apiSettings.ExternalBaseUrl}/confirmpasswordreset/{user.Id}/{UrlEncoder.Default.Encode(confirmationToken)}"; string command = "Click this link to reset your password"; if (this.apiSettings.DebugSendAuthEmailsToClient) { return(ValidationResponseFactory <AuthResponse> .SuccessAuthResponseWithLink(command, confirmationLink)); } else { string email = this.FormatLink(command, confirmationLink); await this.emailService.SendEmailAsync(model.Email, "Password Reset", email); return(ValidationResponseFactory <AuthResponse> .SuccessAuthResponse("Click the link sent to the provided email to reset your password")); } } }
public async Task <AuthResponse> Register(RegisterRequestModel model) { IdentityUser existingUser = await this.userManager.FindByEmailAsync(model.Email); if (existingUser == null) { AuthUser user = new AuthUser { Email = model.Email, UserName = model.Email, Id = this.guidService.NewGuid().ToString() }; IdentityResult result = await this.userManager.CreateAsync(user); if (result.Succeeded) { IdentityResult addPasswordResult = await this.userManager.AddPasswordAsync(user, model.Password); if (addPasswordResult.Succeeded) { string confirmationToken = await this.userManager.GenerateEmailConfirmationTokenAsync(user); string confirmationLink = $"{this.apiSettings.ExternalBaseUrl}/confirmregistration/{user.Id}/{UrlEncoder.Default.Encode(confirmationToken)}"; string command = "Click this link to complete registration"; string email = this.FormatLink(command, confirmationLink); await this.emailService.SendEmailAsync(model.Email, "Registration", email); if (this.apiSettings.DebugSendAuthEmailsToClient) { return(ValidationResponseFactory <AuthResponse> .SuccessAuthResponseWithLink(command, confirmationLink)); } else { return(ValidationResponseFactory <AuthResponse> .SuccessAuthResponse("Click the link sent to the provided email to complete registration")); } } else { return(ValidationResponseFactory <AuthResponse> .FailureAuthResponse(addPasswordResult, AuthErrorCodes.UnableToRegister)); } } else { return(ValidationResponseFactory <AuthResponse> .FailureAuthResponse(result, AuthErrorCodes.UnableToRegister)); } } else { return(ValidationResponseFactory <AuthResponse> .FailureAuthResponse("User already exists", AuthErrorCodes.UserAlreadyExists)); } }
public async Task <AuthResponse> ChangeEmail(ChangeEmailRequestModel model, string currentEmail) { AuthUser user = await this.userManager.FindByEmailAsync(currentEmail); if (user == null) { return(ValidationResponseFactory <AuthResponse> .FailureAuthResponse("User not found", AuthErrorCodes.UserDoesNotExist)); } else { if (this.userManager.PasswordHasher.VerifyHashedPassword(user, user.PasswordHash, model.Password) == PasswordVerificationResult.Success) { user.NewEmail = model.NewEmail; IdentityResult updateResult = await this.userManager.UpdateAsync(user); if (updateResult.Succeeded) { string confirmationToken = await this.userManager.GenerateChangeEmailTokenAsync(user, model.NewEmail); string confirmationLink = $"{this.apiSettings.ExternalBaseUrl}/confirmchangeemail/{user.Id}/{UrlEncoder.Default.Encode(confirmationToken)}"; string command = "Click the link to change your email"; string email = this.FormatLink(command, confirmationLink); await this.emailService.SendEmailAsync(model.NewEmail, "Change Email", email); if (this.apiSettings.DebugSendAuthEmailsToClient) { return(ValidationResponseFactory <AuthResponse> .SuccessAuthResponseWithLink(command, confirmationLink)); } else { return(ValidationResponseFactory <AuthResponse> .SuccessAuthResponse("Click the link sent to the provided email to complete changing your account email")); } } else { return(ValidationResponseFactory <AuthResponse> .FailureAuthResponse("Unable to update user", AuthErrorCodes.UnableToUpdateUser)); } } else { return(ValidationResponseFactory <AuthResponse> .FailureAuthResponse("Invalid email or password", AuthErrorCodes.InvalidUsernameOrPassword)); } } }