public IActionResult CompleteRegistration(string email) { CompleteRegistrationViewModel model = new CompleteRegistrationViewModel(); model.Email = email; return(View(model)); }
public async Task <ActionResult> CompleteRegistration(string userId, string code) { // Prevent a logged in user from returning to the registration page if (User.Identity.IsAuthenticated) { return(RedirectToAction("Index", "Home")); } // Prevent attempts to create duplicate user accounts var existingAccount = db.UserProfiles.Where(x => x.UserID == userId).FirstOrDefault(); if (existingAccount != null) { return(View("Error")); } // If either of the parameters are missing, display an error if (code == null || userId == null) { return(View("Error")); } // Make sure there's a user associated with this ID in the AspIdentity tables var user = await UserManager.FindByIdAsync(userId); if (user != null) { // Pass the model in with the userId and code for verification on completion CompleteRegistrationViewModel model = new CompleteRegistrationViewModel { UserID = userId, Code = code }; // Return the view with the model return(View(model)); } // If we got this far, something went wrong return(View("Error")); }
public async Task <ActionResult> CompleteRegistration(CompleteRegistrationViewModel model) { if (ModelState.IsValid) { // Get the current user through their UserID var user = await UserManager.FindByIdAsync(model.UserID); // If the user isn't null, we found a match, so update the account if (user != null) { // Update it with the values from the view model user.FirstName = model.FirstName; user.LastName = model.LastName; user.UserName = model.UserName; user.EmailConfirmed = true; // Assign the password to this user account var addPassword = await UserManager.AddPasswordAsync(model.UserID, model.Password); var result = await UserManager.UpdateAsync(user); if (result.Succeeded) { // Apply the changes UserManager.Update(user); // Create the entry for the UserProfile table UserProfile newUser = new UserProfile { UserID = model.UserID, FullName = model.FirstName + " " + model.LastName, UserName = model.UserName, Email = user.Email, IsCaller = false, CallsRemaining = 0, DonationsRaised = 0 }; // Set all Standard users to be callers -- Admins will not be counted as callers for now var userEntry = await UserManager.FindAsync(newUser.Email, model.Password); var roles = await UserManager.GetRolesAsync(user.Id); if (roles.Contains("Standard")) { newUser.IsCaller = true; } // Add them to the table and save the changes db.UserProfiles.Add(newUser); db.SaveChanges(); // Redirect to the login page (TO-DO: UPDATE THIS TO THE APPROPRIATE DASHBOARD BASED ON THE USER'S ROLE) return(RedirectToAction("Login", "Account")); } // Display any errors foreach (var error in result.Errors) { ModelState.AddModelError("", error); } // The password wasn't added successfully -- don't update the account and return the view return(View(model)); } // There was no match -- just return the view return(View(model)); } // Invalid model state -- return the view return(View(model)); }
public async Task <IActionResult> CompleteRegistration(CompleteRegistrationViewModel model) { model.RememberMe = false; try { if (ModelState.IsValid) { ApplicationUser user = await _userManager.FindByEmailAsync(model.Email); var defaultpassword = _configuration.GetSection("DefaultPassword").Value; //if (_userManager.FindByEmailAsync(model.Email).Result == null) if (user != null) { if (model.Password == defaultpassword) { ViewBag.error = "true"; ViewBag.message = "Old password cannot be the same as new password"; return(View(model)); } string resetToken = await _userManager.GeneratePasswordResetTokenAsync(user); var resetResult = await _userManager.ResetPasswordAsync(user, resetToken, model.Password); if (resetResult.Succeeded) { user.EmailConfirmed = true; await _userManager.UpdateAsync(user); await _context.SaveChangesAsync(); var signInResult = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure : false); if (signInResult.Succeeded) { ViewBag.error = "false"; ViewBag.loggedin = "success"; _logger.LogInformation("User {0} logged in at {1}", model.Email, DateTime.UtcNow.AddDays(1).ToString()); return(RedirectToAction("Index", "Home")); } } else { ViewBag.error = "true"; ViewBag.message = "An error occurred, password could not be reset"; ModelState.AddModelError(string.Empty, "An error occurred, password could not be reset"); return(View(model)); } } else { ViewBag.error = "true"; ViewBag.message = "Invalid Credentials"; return(View(model)); } } } catch (Exception ex) { ViewBag.loading = "not loading"; //Log the error (uncomment ex variable name and write a log. ModelState.AddModelError("", "Unable to Complete Request"); ViewBag.error = "true"; ViewBag.message = "A Network Error Occurred"; } return(View(model)); }