public ActionResult MigrateUser() { string userName = Session["MigrateUser"].ToString(); if (String.IsNullOrEmpty(userName)) { return(View("Error")); } var model = new ReRegisterViewModel() { UserName = userName }; return(View(model)); }
public async Task <ActionResult> MigrateUser(ReRegisterViewModel model) { if (ModelState.IsValid) { var userManager = new UserManager(); bool flag = userManager.ConfirmUserInfo(model.UserName, model.Email, model.Password); if (flag) { com.GreenThumb.BusinessObjects.User oldUser = null; try { oldUser = userManager.GetUserByUserName(model.UserName); } catch (Exception) { return(View("Error")); } if (null != oldUser) { var user = new ApplicationUser { UserName = model.UserName, Email = model.Email, FirstName = oldUser.FirstName, LastName = oldUser.LastName }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { UserManager.AddClaim(user.Id, new Claim(ClaimTypes.GivenName, user.FirstName)); UserManager.AddClaim(user.Id, new Claim(ClaimTypes.Surname, user.LastName)); await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); Session.Remove("MigrateUser"); return(RedirectToAction("Index", "Home")); } AddErrors(result); if (result.Errors.Any(c => c.Contains("Passwords")) && 1 == result.Errors.Count()) // If the only error is invalid password, redirect to password change... { Session.Add("MigrationNewPassword", oldUser); Session.Add("PasswordError", new { OldValue = model.Password, ErrorMessage = result.Errors.ElementAt(0) }); Session.Remove("MigrateUser"); return(RedirectToAction("MigrationNewPassword", "Account")); } } else { ModelState.AddModelError("", "Information could not be retrieved."); } } else { ModelState.AddModelError("", "Information was not correct."); } } ViewBag.UserName = model.UserName; // If we got this far, something failed, redisplay form return(View(model)); }