public ActionResult Login(LoginContext userInfo)
        {
            if (Auth.Login(userInfo))
            {
                SaveUserCookie(userInfo);
                return RedirectToAction("Index", "Projects");
            }

            return RedirectToAction("Login");
        }
        public ActionResult ChangePass(string oldPass, string newPass, string confirmPass)
        {
            if (!Auth.IsLoggedIn())
            {
                return RedirectToAction("Login", "Home");
            }
            if (string.IsNullOrWhiteSpace(oldPass) ||
                string.IsNullOrWhiteSpace(newPass) ||
                string.IsNullOrWhiteSpace(confirmPass))
            {
                return View();
            }
            if (newPass != confirmPass)
            {
                return View();
            }

            // attempt to log in with current username and oldpass
            LoginContext tryLogin = new LoginContext();
            tryLogin.Username = Auth.GetCurrentUser().Username;
            tryLogin.Password = oldPass;

            if (!Auth.Login(tryLogin))
            {
                // FAILED!!! Need to set error code
                Debug.WriteLine("You Fail!!!"); //Harsh
                return View();
            }

            // update newPass in database
            using (var db = new DataClassesDataContext())
            {
                User user = (from u in db.Users
                              where u.TenantId == Auth.GetCurrentUser().TenantId
                                && u.UserId == Auth.GetCurrentUser().UserId
                              select u).FirstOrDefault();

                user.Password = Auth.GetPasswordHash(newPass);

                db.SubmitChanges();
            }

            return RedirectToAction("Index");
        }
Example #3
0
        public static bool Login(LoginContext loginInfo)
        {
            bool success = false;

            if (!string.IsNullOrEmpty(loginInfo.Username) && !string.IsNullOrEmpty(loginInfo.Password))
            {
                using (var db = new DataClassesDataContext())
                {
                    User matchedUser = (from u in db.Users
                                        where u.Username == loginInfo.Username && u.Password == GetPasswordHash(loginInfo.Password)
                                        select u).FirstOrDefault();

                    if (matchedUser != null)
                    {
                        Login(matchedUser);
                        success = true;
                    }
                }
            }

            return success;
        }
 private void SaveUserCookie(LoginContext userInfo)
 {
     HttpCookie userCookie = new HttpCookie("ProjectManagerUserSession");
     userCookie["username"] = userInfo.Username;
     userCookie["password"] = userInfo.Password;
     userCookie.Expires = DateTime.Now.AddMonths(1);
     Response.Cookies.Add(userCookie);
 }