public ActionResult RegisterWithEmail(RegisterWithEmailViewModel registerwithemail) { if (ModelState.IsValid) { if (!db.Users.Any(u => u.UserEmail == registerwithemail.UserEmail)) { string MyHash = FormsAuthentication.HashPasswordForStoringInConfigFile(registerwithemail.UserPassword, "MD5"); Random random = new Random(); int MyUserCode = random.Next(10000000, 99000000); int MyVerificationCode = 0; bool isValidCod = false; while (!isValidCod) { Random random1 = new Random(); MyVerificationCode = random1.Next(100000, 990000); var userverificationcode = db.Users.FirstOrDefault(j => j.UserVerificationCode == MyVerificationCode.ToString()); if (userverificationcode == null) { isValidCod = true; } } User user = new User() { RoleId = 2, UserCode = MyUserCode.ToString(), UserEmail = registerwithemail.UserEmail, UserPassword = MyHash, UserVerificationCode = MyVerificationCode.ToString(), }; db.Users.Add(user); db.SaveChanges(); SendEmail.Send(registerwithemail.UserEmail, "VerificationCode", "VerificationCode :" + " " + MyVerificationCode.ToString()); return(RedirectToAction("VerificationCode")); } else { ModelState.AddModelError("UserEmail", "You are already Registered"); } } return(View(registerwithemail)); }
public async Task <IActionResult> RegisterByEmail(RegisterWithEmailViewModel model) { if (!ModelState.IsValid) { return(View(model)); } if (model.UserId == null || model.Code == null) { return(View("Error")); } var user = await _userManager.FindByIdAsync(model.UserId); if (user == null) { return(View("Error")); } var resultConfirmEmail = await _userManager.ConfirmEmailAsync(user, model.Code); if (resultConfirmEmail.Succeeded) { // set password to user var result = await _userManager.AddPasswordAsync(user, model.Password); if (result.Succeeded) { await _signInManager.SignInAsync(user, isPersistent : false); _logger.LogInformation(3, "User created a new account with password."); return(RedirectToAction(nameof(HomeController.Index), "Home")); } } //if (result.Succeeded) //{ // return RedirectToAction(nameof(AccountController.ResetPasswordConfirmation), "Account"); //} //AddErrors(result); //return View(); return(View("Error")); }
public void TestRegister_EmailPresentButVerified_DifferentUserName() { var email = "test@test"; var username = "******"; var password = "******"; AddExistingUserWithEmail(_serviceProvider, email, password, username, true); var usernameReplace = "userReplace"; var passwordReplace = "Defdef~456"; var registerViewModel = new RegisterWithEmailViewModel() { Email = email, Password = passwordReplace, ConfirmPassword = passwordReplace, UserName = usernameReplace }; var result = _controller.register(registerViewModel).GetAwaiter().GetResult(); Assert.IsInstanceOf <Microsoft.AspNetCore.Mvc.ViewResult>(result); var viewResult = result as Microsoft.AspNetCore.Mvc.ViewResult; Assert.AreEqual("RegisterConfirmation", viewResult.ViewName); Assert.AreEqual(0, _controller.ModelState.ErrorCount); // Assert that password was NOT updated var userStore = _serviceProvider.GetService <IUserStore <ApplicationUser> >(); var normalizedUserName = _serviceProvider.GetService <ILookupNormalizer>().NormalizeName(username); var user = userStore.FindByNameAsync(normalizedUserName, CancellationToken.None).GetAwaiter().GetResult(); Assert.IsNotNull(user); var signinManager = _serviceProvider.GetService <ApplicationSignInManager <ApplicationUser> >(); var pwVerificationResult = signinManager.UserManager.PasswordHasher.VerifyHashedPassword(user, user.PasswordHash, password); Assert.AreEqual(PasswordVerificationResult.Success, pwVerificationResult); // Assert that "Email already registered" message was sent Assert.AreEqual(1, _emailGenerator.Emails.Count); Assert.AreEqual(EmailType.EmailAlreadyRegistered, _emailGenerator.Emails[0].emailType); }
public IActionResult RegisterByEmail(string userId, string code) { //ViewData["ReturnUrl"] = returnUrl; var user = _userManager.FindByIdAsync(userId).Result; if (user == null) { return(View("Error")); } var resultConfirmEmail = _userManager.ConfirmEmailAsync(user, code).Result; var model = new RegisterWithEmailViewModel() { UserId = user.Id.ToString(), Code = code, Email = user.Email }; return(View(model)); }
public void TestRegister_Success() { var email = "test@test"; var username = "******"; var password = "******"; var registerViewModel = new RegisterWithEmailViewModel() { Email = email, Password = password, ConfirmPassword = password, UserName = username }; var result = _controller.register(registerViewModel).GetAwaiter().GetResult(); Assert.IsInstanceOf <Microsoft.AspNetCore.Mvc.ViewResult>(result); var viewResult = result as Microsoft.AspNetCore.Mvc.ViewResult; Assert.AreEqual("RegisterConfirmation", viewResult.ViewName); Assert.AreEqual(0, _controller.ModelState.ErrorCount); // Assert that user was created var userStore = _serviceProvider.GetService <IUserStore <ApplicationUser> >(); var normalizedUserName = _serviceProvider.GetService <ILookupNormalizer>().NormalizeName(username); var user = userStore.FindByNameAsync(normalizedUserName, CancellationToken.None).GetAwaiter().GetResult(); Assert.IsNotNull(user); Assert.AreEqual(username, user.UserName); Assert.AreEqual(email, user.Email); Assert.IsFalse(user.EmailConfirmed); // Assert that verification email was sent Assert.AreEqual(1, _emailGenerator.Emails.Count); Assert.AreEqual(EmailType.EmailVerification, _emailGenerator.Emails[0].emailType); }
public async Task <IActionResult> register(RegisterWithEmailViewModel model) { ViewData["IsLoggedIn"] = IsLoggedIn; ViewData[Constants.VIEWDATA_NOPRIVACYCONSENT] = true; var pwForm = model.Password; model.Password = String.Empty; model.ConfirmPassword = String.Empty; if (ModelState.IsValid) { IdentityResult result = null; var userFromEmail = await _userManager.FindByEmailAsync(model.Email); var userFromUserName = await _userManager.FindByNameAsync(model.UserName); // If using somebody else's confirmed e-mail address, send a warning to that e-mail address // TODO: log this if (userFromEmail?.EmailConfirmed == true) { await _emailGenerator.GenerateEmailAsync(userFromEmail.Email, EmailType.EmailAlreadyRegistered); } // Very specific condition of a user trying to re-register with exactly // the same information. Reset the password. if (userFromUserName != null && userFromEmail != null && userFromUserName.EmailConfirmed == false && userFromEmail.EmailConfirmed == false && userFromUserName.NormalizedEmail == userFromEmail.NormalizedEmail && userFromUserName.NormalizedUserName == userFromEmail.NormalizedUserName ) { result = await _signInManager.ChangeUserPasswordAsync(userFromEmail, pwForm); } else { if (userFromUserName != null) { result = IdentityResult.Failed(new IdentityError { Description = USER_ALREADY_TAKEN_MESSAGE }); } else { if (userFromEmail == null) { userFromEmail = new ApplicationUser { UserName = model.UserName, Email = model.Email }; result = await _userManager.CreateAsync(userFromEmail, pwForm); } else { if (userFromEmail.EmailConfirmed) { // We've warned the real user; pretend like nothing happened // but we need to short-circuit the success return(View("RegisterConfirmation", model)); } else { result = await _signInManager.ChangeUserPasswordAsync(userFromEmail, pwForm); } } } } if (result.Succeeded) { var code = await _userManager.GenerateEmailConfirmationTokenAsync(userFromEmail); // Note that Url is null when we create the controller as part of a unit test var link = Url?.Action(nameof(accountController.confirmemail), "account", new { userFromEmail.UserName, code }, Request.Scheme); await _emailGenerator.GenerateEmailAsync(userFromEmail.Email, EmailType.EmailVerification, link); // Note that we do *not* sign in the user return(View("RegisterConfirmation", model)); } else { foreach (var error in result.Errors) { ModelState.AddModelError(String.Empty, error.Description); } } } // If we got this far, something failed; redisplay form return(View(model)); }