public ActionResult ActivateAccount2(Guid? id) { if (id.HasValue) { using (var db = new LibraryDbContext()) { MultipleModel.AuthModelVM vm = new MultipleModel.AuthModelVM(); var userActivation = TempData["UserActivation"] as MultipleModel.AuthModelVM; if(userActivation != null) { vm = userActivation; } vm.UserModel = db.Users.SingleOrDefault(u => u.Id == id); return View(vm); } } return RedirectToAction("Login"); }
public ActionResult ActivateAccount2(MultipleModel.AuthModelVM request) { if(ModelState.IsValid) { using (var db = new LibraryDbContext()) { MultipleModel.AuthModelVM vm = new MultipleModel.AuthModelVM(); vm.UserModel = db.Users.SingleOrDefault(u => u.Id == request.UserModel.Id); var crypto = new SimpleCrypto.PBKDF2(); var encrypPass = crypto.Compute(request.ActivationModel1.Password); vm.UserModel.PasswordSalt = crypto.Salt; vm.UserModel.Password = encrypPass; vm.UserModel.SecretQuestion = request.ActivationModel1.SecretQuestion; vm.UserModel.SecretAnswer = request.ActivationModel1.SecretAnswer; vm.UserModel.Status = true; vm.UserModel.Deleted = false; vm.UserModel.UpdatedAt = DateTime.Now; vm.UserModel.Student.Birthday = request.ActivationModel1.Birthday; vm.UserAddressModel = db.UserAddresses.Create(); vm.UserAddressModel.UserId = vm.UserModel.Id; vm.UserAddressModel.ZipCode = request.ActivationModel1.ZipCode; vm.UserAddressModel.Address1 = request.ActivationModel1.Address1; vm.UserAddressModel.Address2 = request.ActivationModel1.Address2; vm.UserAddressModel.City = request.ActivationModel1.City; vm.UserAddressModel.Country = request.ActivationModel1.Country; vm.UserAddressModel.CreatedAt = DateTime.Now; db.UserAddresses.Add(vm.UserAddressModel); db.Entry(vm.UserModel).State = EntityState.Modified; db.SaveChanges(); var ctx = Request.GetOwinContext(); var authManager = ctx.Authentication; authManager.SignOut("ApplicationCookie"); var loginVM = new MultipleModel.LoginModelVM(); loginVM.Error = false; var errorList = new List<string>(); string message = "You have successfully activated your account. Please log in"; errorList.Add(message); loginVM.Message = errorList; TempData["LoginTD"] = loginVM; return RedirectToAction("Login"); } } request.Error = true; request.Message = CustomValidationMessage.GetErrorList(ViewData.ModelState); TempData["UserActivation"] = request; return RedirectToAction("ActivateAccount2", new { id = request.UserModel.Id }); }