public async Task <IActionResult> ChangePasswordSendEmailUserAsync(string email) { Guard.Against.NullString(email); ApplicationUser applicationUser = await _userManager.FindByEmailAsync(email); if (applicationUser == null) { return(NotFound(new { message = $"User not found with the specified email." })); } //Checking if the user can change password IList <UserLoginInfo> userLogins = await _userManager.GetLoginsAsync(applicationUser); if (userLogins.Count > 0) { return(StatusCode(401, new { message = $"Account not authorized to change password, please update the password with your login provider." })); } string token = await _tokenProviderService.GenerateJWTAsync(applicationUser); EmailModel emailModel = new EmailModel { EmailTo = applicationUser.Email, Body = $"<html><body><a href='{_configSettings.URL.ChangePasswordURL}/{token}'>Click here to Change Password</a><br></body></html>", Subject = "Visneto - Change Password" }; await _emailManager.SendEmailAsync(emailModel); return(Ok(new { message = $"Email sent for change password request, please check your registered email's inbox." })); }
public async Task <IActionResult> RegisterAsync([FromBody] RegisterUserModelDTO RegisterUserModelDTO) { Guard.Against.NullItem(RegisterUserModelDTO); //Check for duplicate user-names and e-mail ApplicationUser foundUser = await _userManager.Users.Where(x => x.Email == RegisterUserModelDTO.UserEmail || x.UserName == RegisterUserModelDTO.UserName).FirstOrDefaultAsync(); //Throw HTTP 409 Conflict then if (foundUser != null) { return(StatusCode(409, new { message = $"The username / email is already taken and is conflicting with other records, please give an unique username / email." })); } ApplicationUser applicationUser = new ApplicationUser { UserName = RegisterUserModelDTO.UserName, Email = RegisterUserModelDTO.UserEmail, FirstName = RegisterUserModelDTO.FirstName, LastName = RegisterUserModelDTO.LastName, PhoneNumber = RegisterUserModelDTO.PhoneNumber, EmailConfirmed = false }; IdentityResult createResult = await _userManager.CreateAsync(applicationUser, RegisterUserModelDTO.Password); //User creation failed because of some constraints if (!createResult.Succeeded) { return(BadRequest(new { message = createResult.GetIdentityResultErrorMessage() })); } await _signInManager.SignInAsync(applicationUser, false); await _userManager.AddToRoleAsync(applicationUser, UserType.User.ToString()); //Generate JWT now string jwtToken = await _tokenManager.GenerateJWTAsync(applicationUser); //Send verify email now EmailModel emailModel = new EmailModel { EmailTo = RegisterUserModelDTO.UserEmail, Body = $"<html><body><a href='{_configSettings.URL.VerifyEmailURL}/{jwtToken}'>Click here to verify Email</a><br></body></html>", Subject = "Verify your Email" }; await _emailManager.SendEmailAsync(emailModel); ReferralCode referralCode = GenerateReferralCode(applicationUser.UserName, applicationUser.Id); await _referralCodeService.AddReferralCode(referralCode); //Send Referral code mail now emailModel.Body = $"<html><body><fieldset><legend> Referral code for User - {applicationUser.UserName} </legend> {referralCode.RefCode} </fieldset></body></html>"; emailModel.Subject = $"Visneto Referral Code for new user - {applicationUser.UserName}"; await _emailManager.SendEmailAsync(emailModel); //Return HTTP 201 Created for new user return(StatusCode(201, new { role = UserType.User.ToString(), access_token = jwtToken, expires = 3600, email = string.Empty, user_name = RegisterUserModelDTO.UserName })); }