Exemplo n.º 1
0
        //
        // POST: /Account/LogOff
        //[ValidateAntiForgeryToken]
        public ActionResult LogOut()
        {
            if (CurrentUser != null)
            {
                _logger.Info("注销退出:user:" + CurrentUser.UserName);
                //_userLogService.Log(new UserLogContract() { IpAddress = CerCommon.GetIp(), Message = "注销退出", UserId = CurrentUser.Id, FromClient = "主系统" });
            }
            //WebSecurity.Logout();

            HttpCookie cookie = new HttpCookie("USER_COOKIE");

            //读取保存的Cookie信息
            HttpCookie cookies = Request.Cookies["USER_COOKIE"];
            var        model   = new LoginModel();

            if (cookies != null && !string.IsNullOrEmpty(cookies.Value))
            {
                //如果Cookie不为空,则将Cookie里面的用户名和密码读取出来赋值给前台的文本框。
                model.UserName = Md5Util.Decrypt(cookies["UserName"]);
                model.Password = Md5Util.Decrypt(cookies["UserPassword"]);
                if (!string.IsNullOrEmpty(cookies["AutoLogin"]))
                {
                    model.AutoLogin = bool.Parse(Md5Util.Decrypt(cookies["AutoLogin"]));
                }
                //这里依然把记住密码的选项给选中。
                model.RememberMe = true;
            }
            if (model.RememberMe)
            {
                //所有的验证信息检测之后,如果用户选择的记住密码,则将用户名和密码写入Cookie里面保存起来。
                cookie.Values.Add("UserName", Md5Util.Encrypt(model.UserName.Trim()));
                cookie.Values.Add("UserPassword", Md5Util.Encrypt(model.Password.Trim()));
                cookie.Values.Add("AutoLogin", Md5Util.Encrypt(false.ToString()));
                //这里是设置Cookie的过期时间,这里设置7天的时间,过了时间之后状态保持自动清空。
                cookie.Expires = DateTime.Now.AddDays(7);
                Response.Cookies.Add(cookie);
            }

            ActionResult logOff = Logout();

            _formsAuthentication.SignOut();
            _contextService.SetCookie("role", "");
            _contextService.NickName = null;
            _contextService.DepId    = string.Empty;

            return(logOff);
        }
Exemplo n.º 2
0
        //
        // GET: /Account/Login
        //[AllowAnonymous]
        public ActionResult Login(string returnUrl)
        {
            _logger.Info("登录页面:访问IP:" + CerCommon.GetIp());

            if (CurrentUser != null && !(CurrentUser is EmptyUserContract))
            {
                _formsAuthentication.SetAuthCookie(CurrentUser.UserName, false);
                _contextService.SetCookie("role", CurrentUser.RoleId.ToString());
                _contextService.NickName  = CurrentUser.NickName;
                _contextService.DepId     = CurrentUser.DepId.ToString();
                _contextService.UserPhoto = ConfigurationManager.AppSettings["USER_AVATAR"] + CurrentUser.UserInfoPhoto;
                _logger.Info(CurrentUser.Id + "登录成功" + "文档管理系统");

                return(Redirect("/home/index"));
            }
            var model = new LoginModel();
            //读取保存的Cookie信息
            HttpCookie cookies = Request.Cookies["USER_COOKIE"];

            if (cookies != null && !string.IsNullOrEmpty(cookies.Value))
            {
                //如果Cookie不为空,则将Cookie里面的用户名和密码读取出来赋值给前台的文本框。
                model.UserName = Md5Util.Decrypt(cookies["UserName"]);
                model.Password = Md5Util.Decrypt(cookies["UserPassword"]);

                //这里依然把记住密码的选项给选中。
                model.RememberMe  = true;
                ViewBag.ReturnUrl = returnUrl;
                if (model.AutoLogin)
                {
                    return(Login(model, returnUrl));
                }

                return(View(model));
            }

            //if (!string.IsNullOrEmpty(returnUrl) && returnUrl.EndsWith("/account/logoff"))
            //{
            //    returnUrl = returnUrl.Replace("/account/logoff", "/home/index");
            //}
            ViewBag.ReturnUrl = returnUrl;
            return(View(model));
        }