public ActionResult AccountDetails(UserAccount account) { User user = new User(); using (PYPContext db = new PYPContext()) { user = db.Users.Single(u => u.UserName == User.Identity.Name); user.UserName = account.UserName; user.Name = account.Name; user.Email = account.Email; if (db.Entry(user).State == EntityState.Modified) { db.SaveChanges(); } } return RedirectToAction("Index", "Home"); }
public ActionResult ChangePassword(ChangePassword passwords) { if (ModelState.IsValid) { using (PYPContext db = new PYPContext()) { User user = db.Users.SingleOrDefault(u => u.UserName == User.Identity.Name); if (user != null && Crypto.VerifyHashedPassword(user.Password, passwords.CurrentPassword + user.Salt)) { user.Password = Crypto.HashPassword(passwords.NewPassword + user.Salt); db.SaveChanges(); return RedirectToAction("Index", "Home"); } ModelState.AddModelError("", "Your current password did not match our records!"); } } return View("_ChangePassword", passwords); }
public ActionResult Register(UserRegistration model) { if (ModelState.IsValid) { using (PYPContext db = new PYPContext()) { User user = new User(); user.Name = model.Name; user.Email = model.Email; user.UserName = model.UserName; user.Salt = Crypto.GenerateSalt(); user.Password = Crypto.HashPassword(model.Password + user.Salt); // Save the new user to the database db.Users.Add(user); db.SaveChanges(); // Login the new user FormsAuthentication.SetAuthCookie(user.UserName, false); FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(Response.Cookies.Get(FormsAuthentication.FormsCookieName).Value); GenericPrincipal userPrincipal = new GenericPrincipal(new FormsIdentity(ticket), null); System.Web.HttpContext.Current.User = userPrincipal; Thread.CurrentPrincipal = userPrincipal; } // Redirect to Home return RedirectToAction("Index", "Home"); } // If we got this far, something failed, redisplay form return View(model); }