public ActionResult Password(Users.PasswordChangeModel model)
        {
            var localAccount = WebSecurity.GetUserId(User.Identity.Name);
            ViewBag.ReturnUrl = Url.Action("Password");

            if (ModelState.IsValid)
            {
                // ChangePassword will throw an exception rather than return false in certain failure scenarios.
                bool changePasswordSucceeded;
                try
                {
                    changePasswordSucceeded = WebSecurity.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword);
                }
                catch (Exception)
                {
                    changePasswordSucceeded = false;
                }

                if (changePasswordSucceeded)
                {
                    return RedirectToAction("Password", new { Message = ManageMessageId.ChangePasswordSuccess });
                }
                else
                {
                    ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
                }
            }
            // If we got this far, something failed, redisplay form
            return View(model);
        }
Ejemplo n.º 2
0
        public static void MembershipRoles()
        {
            //don't o anythign if the admin role exists already
            if (!Roles.RoleExists("Admin"))
            {
                Users user = new Users();

                if (!Roles.RoleExists("Admin"))
                    Roles.CreateRole("Admin");
                if (!Roles.RoleExists("Mod"))
                    Roles.CreateRole(("Mod"));
                if (!Roles.RoleExists("User"))
                    Roles.CreateRole(("User"));

                if (!WebSecurity.UserExists("Admin"))
                    WebSecurity.CreateUserAndAccount("Admin", "Admin",
                                                     propertyValues:
                                                         new
                                                             {
                                                                 Username = "******",
                                                                 firstName = "Admin",
                                                                 lastName = "Admin",
                                                                 email = "*****@*****.**"
                                                             });
                if (!Roles.GetRolesForUser("Admin").Contains("Admin"))
                    Roles.AddUserToRole("Admin", "Admin");
            }
        }
 public ActionResult Create(Users usermodel)
 {
     if (ModelState.IsValid)
     {
         _userDb.Users.Add(usermodel);
         _userDb.SaveChanges();
         return RedirectToAction("Index");
     }
     ViewBag.Error = "Model Invalid";
     return View(usermodel);
 }
        public ActionResult Index(Users.LogonModel model, string returnUrl)
        {
            if (ModelState.IsValid && WebSecurity.Login(model.Username, model.Password, persistCookie: model.RememberMe))
            {
                if (Roles.IsUserInRole("Admin"))
                {
                    RedirectToAction("Index", "Admin");
                }
                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);

            #region here be dragons
            /*
              if (ModelState.IsValid)
            {

                var userList = _userDb.Users.Where(u => u.Username.Equals(model.Username));
                //Users user = _userDb.Users.Select() (model.Username); //TODO: need to figure out how to select a user row by ID based on username
                if (userList != null)
                {
                    userList.Cast<Users>();
                    foreach (Users user in userList)
                    {
                        string dbPwd = "";
                        //if (user.ValidatePassword(model.Password))
                        if(user.CheckHash(model.Password, user.password))
                        {
                            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, model.Username,
                                DateTime.Now, DateTime.Now.AddMinutes(60), false, Roles.GetRolesForUser().ToString(),
                                FormsAuthentication.FormsCookiePath);
                            string encTicket = FormsAuthentication.Encrypt(ticket);
                            Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));

                            //FormsAuthentication.SetAuthCookie(model.Username, false); //set non persistant cookie,
                            //return RedirectToAction("Index", "Home"); //return to home page
                            if (returnUrl != null)
                                return Redirect(returnUrl);
                            else
                                return View("../Home/Index");
                        }
                        else
                        {
                            ModelState.AddModelError("", "Invalid Username or Password"); //if not valid or not authenticated return error
                        }*/
            #endregion
        }
        public ActionResult Index(Users.RegistrationViewModel user)
        {
            if (user == null)
            {
                return View();
            }
            //TODO: Change recaptcha PRIVATE key if the domain changes register on Recaptcha.com
            if (ReCaptcha.Validate(privateKey: "6Ld_kt0SAAAAAAWadUrgyxHuqAlp2fjS5RXjdyn9"))
            {
                if (ModelState.IsValid)
                {
                    if (WebSecurity.UserExists(user.Username))
                    {
                        ModelState.AddModelError("Username", "User Name has already been chosen, please try another.");
                        return View(user);

                    }
                    WebSecurity.CreateUserAndAccount(
                        user.Username, user.password,
                        new
                        {
                            user.Username,
                            user.email
                        });

                    string username = user.Username;
                    Roles.AddUserToRole(username, "User");
                    WebSecurity.Login(username, user.password, persistCookie: false);
                    ViewBag.Title = "Success!";
                    ViewBag.Message = "You have succesfully been registered!";

                    return RedirectToAction("Index", "Movies");
                }
                ViewBag.Title = "FAILED!";
                return View(user);
            }
            //Else: recaptcha failed!
            return View(user);
        }
 public ActionResult Edit(Users usermodel)
 {
     if (ModelState.IsValid)
     {
         _userDb.Entry(usermodel).State = EntityState.Modified;
         _userDb.SaveChanges();
         return RedirectToAction("Index");
     }
     ViewBag.error = "Invlaid model";
     return View(usermodel);
 }