protected void Seed(PortfolioContext context)
        {
            // Cascade null
            context.Database.ExecuteSqlCommand(@"IF(OBJECT_ID('Project_Media','F') IS NOT NULL)
                BEGIN
                    ALTER TABLE Project
                    DROP CONSTRAINT Project_Media
                END");

            context.Database.ExecuteSqlCommand(@"ALTER TABLE Project
                ADD CONSTRAINT Project_Media
                FOREIGN KEY (MediaID)
                REFERENCES Media (ID)
                ON UPDATE CASCADE ON DELETE SET NULL");

            context.Database.ExecuteSqlCommand(@"IF(OBJECT_ID('User_Media','F') IS NOT NULL)
                BEGIN
                    ALTER TABLE AccountUser
                    DROP CONSTRAINT User_Media
                END");

            context.Database.ExecuteSqlCommand(@"ALTER TABLE AccountUser
                ADD CONSTRAINT User_Media
                FOREIGN KEY (MediaID)
                REFERENCES Media (ID)
                ON UPDATE CASCADE ON DELETE SET NULL");

            // Create myself as a user
            var user = new AccountUser
            {
                FirstName = "Hannah",
                LastName = "Hamlin",
                Email = "*****@*****.**",
                Password = SecurePasswordHasher.Hash("password"),
                MediaID = null,
                CreatedAt = TimeStamp.Now(),
                UpdatedAt = TimeStamp.Now()
            };

            context.AccountUsers.Add(user);

            context.SaveChanges();
        }
Пример #2
0
        public ActionResult Index(AccountUser accountUser, string ReturnUrl)
        {
            if (!ModelState.IsValid)
            {
                return View(accountUser);
            }

            bool isValid = Membership.ValidateUser(accountUser.Email, accountUser.Password);
            if (!isValid)
            {
                ModelState.AddModelError("", "Invalid username or password.");
                return View(accountUser);
            }

            ToMembershipUser user = Membership.GetUser(accountUser.Email, true) as ToMembershipUser;
            FormsAuthentication.SetAuthCookie(user.Email, false);

            if (!String.IsNullOrEmpty(ReturnUrl) && ReturnUrl != "/")
            {
                return Redirect(ReturnUrl);
            }
            var a = HttpContext.User.Identity.IsAuthenticated;
            return RedirectToAction("Index", "Dashboard");
        }
        public ActionResult Edit([Bind(Include = "ID,FirstName,LastName,Email,CurrentPassword,NewPassword,ConfirmPassword,MediaID,Media")] EditAccountViewModel accountViewModel)
        {
            if (!ModelState.IsValid)
            {
                accountViewModel.Media = db.Media.Find(accountViewModel.MediaID);
                return View(accountViewModel);
            }

            string currentPass = accountViewModel.CurrentPassword;
            if(currentPass != null)
            {
                var user = membershipUser.Password;
                bool isVerified = SecurePasswordHasher.Verify(currentPass, membershipUser.Password);
                if (!isVerified)
                {
                    ModelState.AddModelError("CurrentPassword", "Current password incorrect. Cannot update password.");
                    return View(accountViewModel);
                }
            }

            var accountUser = new AccountUser();
            MapViewModel(accountUser, accountViewModel);
            db.Entry(accountUser).State = EntityState.Modified;
            db.Entry(accountUser).Property("CreatedAt").IsModified = false;
            db.Entry(accountUser).Property("MediaID").IsModified = false;
            if (accountViewModel.NewPassword == null)
                db.Entry(accountUser).Property("Password").IsModified = false;
            db.SaveChanges();
            AccountUserMembershipProvider.UpdateCookie(accountUser.Email);
            return RedirectToRoute("Dashboard_default");
        }
 private void MapViewModel(AccountUser accountUser, EditAccountViewModel accountViewModel)
 {
     accountUser.ID = accountViewModel.ID;
     accountUser.FirstName = accountViewModel.FirstName;
     accountUser.LastName = accountViewModel.LastName;
     accountUser.Email = accountViewModel.Email;
     if(accountViewModel.NewPassword != null)
         accountUser.Password = SecurePasswordHasher.Hash(accountViewModel.NewPassword);
     accountUser.MediaID = accountViewModel.MediaID;
     accountUser.Media = accountViewModel.Media;
     accountUser.UpdatedAt = TimeStamp.Now();
 }