public ActionResult AccountDetails()
        {
            UserAccount account = new UserAccount();

            using (PYPContext db = new PYPContext())
            {
                User currentUser = db.Users.SingleOrDefault(u => u.UserName == User.Identity.Name);

                account.Name = currentUser.Name;
                account.Email = currentUser.Email;
                account.UserName = currentUser.UserName;
            }

            return PartialView("_AccountDetails", account);
        }
        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 JsonResult IsUniqueUserName(string username)
 {
     using (PYPContext db = new PYPContext())
     {
         return db.Users.Any(u => u.UserName == username) ? Json(ErrorCodeToString(MembershipCreateStatus.DuplicateUserName), JsonRequestBehavior.AllowGet) : Json(true, JsonRequestBehavior.AllowGet);
     }
 }
        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);
        }
        public ActionResult Login(UserLogin model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                using (PYPContext db = new PYPContext())
                {
                    // Lookup user by unique username
                    User user = db.Users.SingleOrDefault(u => u.UserName == model.UserName);

                    if (user != null && Crypto.VerifyHashedPassword(user.Password, model.Password + user.Salt))
                    {
                        // Credentials Passed Login the User
                        FormsAuthentication.SetAuthCookie(user.UserName, model.RememberMe);
                        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;

                        HttpContext.Session["userId"] = user.UserId;

                        return RedirectToLocal(returnUrl);
                    }
                }

            }

            // If we got this far, something failed, redisplay form
            ModelState.AddModelError("", "The user name or password provided is incorrect.");
            return View(model);
        }