Exemplo n.º 1
0
        public ActionResult ChangePassword(ChangePasswordModel model)
        {
            if (ModelState.IsValid)
            {
                bool changePasswordSucceeded = true;
                try
                {
                    var currentUser = customerDb.Set <Customer>().Where(x => x.UserName == model.UserName).SingleOrDefault();
                    if (currentUser != null && currentUser.Password == model.OldPassword)
                    {
                        currentUser.Password = model.NewPassword;
                        customerDb.Dispose();
                        customerDb = new ProductStoreContext();
                        customerDb.Customers.Attach(currentUser);
                        using (var tr = customerDb.Database.BeginTransaction())
                        {
                            try
                            {
                                customerDb.SaveChanges();
                            }
                            catch
                            {
                                tr.Rollback();
                            }
                        }
                    }
                    else
                    {
                        changePasswordSucceeded = false;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    changePasswordSucceeded = false;
                }

                if (changePasswordSucceeded)
                {
                    return(RedirectToAction("ChangePasswordSuccess"));
                }
                else
                {
                    ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
                }
            }

            return(View(model));
        }
Exemplo n.º 2
0
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                var users = customerDb.Set <Customer>().Where(x => x.UserName == model.UserName || x.Email == model.Email).ToList();

                if (users != null && !users.Any())
                {
                    MigrateShoppingCart(model.UserName);
                    Customer newUser = new Customer()
                    {
                        UserName = model.UserName, Email = model.Email, Password = model.Password
                    };
                    using (var tr = customerDb.Database.BeginTransaction())
                    {
                        try
                        {
                            customerDb.Dispose();
                            customerDb = new ProductStoreContext();
                            customerDb.Customers.Add(newUser);
                            customerDb.SaveChanges();
                        }
                        catch
                        {
                            tr.Rollback();
                        }
                    }
                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    ModelState.AddModelError("", "User name already exists. Please enter a different user name.");
                }
            }
            return(View(model));
        }