public async Task <IActionResult> addEmail(AddEmailViewModel model) { var user = await GetCurrentUserAsync(); UserEmailAddresses email = new UserEmailAddresses(); email.ApplicationUser = user; email.ApplicationUserId = user.Id; email.EmailType = model.emailTypes; email.emailAddress = model.email; email.Confirmed = false; var success = _emailProvider.createEmailForUser(email); if (success) { var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); //Add the email address to the end of the token. When the token is returned we will know for which email. code = code + "./" + model.email; var callbackUrl = Url.Action("ConfirmAddEmail", "Identity", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme); //This method will send an email from [email protected] to the email the user inputted. await _authEmail.SendEmailAsync(model.email, "Confirm Email", "Please click this <a href=\"" + callbackUrl + "\">link</a> to finish adding this email to your Identity Right Account."); return(RedirectToAction("ManageUserEmails", new { Message = ManageMessageId.AddEmailSuccessVerify })); } return(RedirectToAction("ManageUserEmails", new { Message = ManageMessageId.AddEmailFail })); }
public IActionResult deleteEmailAddress(UserEmailAddresses email) { var success = _emailProvider.deleteEmailAddress(email); if (success) { return(RedirectToAction("ManageUserEmails", new { Message = ManageMessageId.DeleteEmailSuccessful })); } return(RedirectToAction("ManageUserEmails", new { Message = ManageMessageId.DeleteEmailFail })); }
/// <summary> /// Check if an email for the user already exists. /// </summary> /// <param name="email">UserEmailAddress</param> /// <returns>Boolean if email already exists for the user</returns> public bool doesEmailAlreadyExist(UserEmailAddresses email) { var emails = _dbContext.UserEmailAddress.Where(x => x.ApplicationUserId == email.ApplicationUserId) .Where(y => y.emailAddress == email.emailAddress).ToList(); if (emails.Count == 0) { //If no email exists return false return(false); } return(true); }
public bool deleteEmailAddress(UserEmailAddresses email) { _dbContext.UserEmailAddress.Remove(email); _dbContext.SaveChanges(); var exists = doesEmailAlreadyExist(email); if (!exists) { return(true); } //something went wrong and email still exists for the user. return(false); }
/// <summary> /// Add email to the database /// </summary> /// <param name="email">UserEmailAddress object</param> /// <returns>Boolean if the add was successful or not</returns> public bool createEmailForUser(UserEmailAddresses email) { //Check if the email address already exits bool exist = doesEmailAlreadyExist(email); //If it doesn't add the email address to the database. if (!exist) { _dbContext.UserEmailAddress.Add(email); _dbContext.SaveChanges(); return(true); } return(false); }
public async Task <IActionResult> ConfirmEmail(string userId, string code) { if (userId == null || code == null) { return(View("Error")); } var user = await _userManager.FindByIdAsync(userId); if (user == null) { return(View("Error")); } //If the email is alraedy confirmed go to home page. if (user.EmailConfirmed) { return(View("Login")); } //Confirm the token var result = await _userManager.ConfirmEmailAsync(user, code); //If the token was correct, add the email to the UserEmailAddress model aswell. bool emailCreated = false; if (result.Succeeded) { UserEmailAddresses email = new UserEmailAddresses(); email.ApplicationUser = user; email.ApplicationUserId = user.Id; email.Confirmed = true; email.emailAddress = user.UserName; email.EmailType = EmailTypes.Primary; //Create the email address in the database emailCreated = _emailProvider.createEmailForUser(email); } return(View((result.Succeeded && emailCreated) ? "ConfirmEmail" : "Error")); }