示例#1
0
        public ActionResult Login(string returnUrl)
        {
            string CookieName = ConfigUtilities.GetSysConfigAppSetting("CookieName");

            ViewBag.ReturnUrl = returnUrl;
            HttpCookie     userInfo = Request.Cookies[CookieName];
            LoginViewModel log      = new LoginViewModel()
            {
                RememberMe = false, Password = "", UserName = ""
            };

            if (userInfo != null && userInfo["username"].ToString() != "")
            {
                try
                {
                    log.RememberMe = true;
                    log.UserName   = userInfo["username"].ToString();
                    string password = Library.DecodePassword(userInfo["password"].ToString());
                    log.Password = password;
                }
                catch
                {
                }
            }
            return(View(log));
        }
示例#2
0
        public ActionResult Login(LoginViewModel model, string returnUrl)
        {
            string CookieName = ConfigUtilities.GetSysConfigAppSetting("CookieName");
            // If we got this far, something failed, redisplay form
            //ModelState.AddModelError("", "The user name or password provided is incorrect.");
            string userName = model.UserName;
            string password = model.Password;

            //Remember Cookies
            if (model.RememberMe == true)
            {
                HttpCookie userInfo = new HttpCookie(CookieName);
                userInfo.HttpOnly    = true;
                userInfo["username"] = userName;
                userInfo["password"] = Library.EncodePassword(password);
                userInfo.Expires     = DateTime.Now.AddDays(30);
                Response.Cookies.Add(userInfo);
                Request.Cookies[CookieName].Expires = DateTime.Now.AddDays(30);
            }
            else
            {
                HttpCookie userInfo = new HttpCookie(CookieName);
                userInfo["username"] = "";
                userInfo["password"] = "";
                Response.Cookies.Add(userInfo);
            }
            //Kiểm tra nếu tài khoản bị khóa thì không cho đăng nhập
            if (_context.AccountModel.Where(p => p.UserName == model.UserName && p.Actived != true).FirstOrDefault() != null)
            {
                string errorMessage = "Tài khoản đã bị khóa xin vui lòng liên hệ quản trị hệ thống để biết thêm chi tiết.";
                ModelState.AddModelError("", errorMessage);
                return(View(model));
            }
            //Kiểm tra đăng nhập
            if (CheckLogin(userName, password))
            {
                if (!string.IsNullOrEmpty(returnUrl))
                {
                    return(Redirect(GetRedirectUrl(returnUrl)));
                }
                else
                {
                    return(RedirectToAction("Index", "Home", null));
                }
            }
            else
            {
                string errorMessage = "Tài khoản hoặc mật khẩu của bạn chưa chính xác ! Xin vui lòng thử lại.";
                ModelState.AddModelError("", errorMessage);
                return(View(model));
            }
        }