/// <summary>
        /// 缓存用户Cookie数据
        /// </summary>
        /// <param name="userInfo">用户信息</param>
        /// <returns></returns>
        private void CacheUserData(UserInfo userInfo)
        {
            _Session[LOGINERROR] = null;
            //客户端浏览器参数
            int w = _Request["w"].ObjToInt();
            int h = _Request["h"].ObjToInt();

            if (w > 0)
            {
                userInfo.ClientBrowserWidth = w;
            }
            if (h > 0)
            {
                userInfo.ClientBrowserHeight = h;
            }
            //获取客户端IP
            userInfo.ClientIP = WebHelper.GetClientIP(_Request);
            //缓存用户扩展信息
            UserInfo.CacheUserExtendInfo(userInfo.UserName, userInfo.ExtendUserObject);
            //用户票据保存
            FormsPrincipal.Login(userInfo.UserName, userInfo, UserInfo.ACCOUNT_EXPIRATION_TIME, GetHttpContext(_Request));
            //登录成功写cookie,保存客户端用户名
            var userNameCookie = new HttpCookie("UserName", userInfo.UserName);

            userNameCookie.Expires = DateTime.Now.AddDays(365);
            _Response.Cookies.Add(userNameCookie);
        }
        /// <summary>
        /// 缓存用户Cookie数据
        /// </summary>
        /// <param name="userInfo">用户信息</param>
        /// <returns></returns>
        private void CacheUserData(UserInfo userInfo)
        {
            //客户端浏览器参数
            int w = _Request.QueryEx("w").ObjToInt();
            int h = _Request.QueryEx("h").ObjToInt();

            if (w > 0)
            {
                userInfo.ClientBrowserWidth = w;
            }
            if (h > 0)
            {
                userInfo.ClientBrowserHeight = h;
            }
            //获取客户端IP
            userInfo.ClientIP = WebHelper.GetClientIP(_Request);
            //缓存用户扩展信息
            UserInfo.CacheUserExtendInfo(userInfo.UserName, userInfo.ExtendUserObject);
            //用户票据保存
            int acount_expire_time = UserInfo.ACCOUNT_EXPIRATION_TIME;

            acount_expire_time = WebConfigHelper.GetAppSettingValue("ACCOUNT_EXPIRATION_TIME").ObjToInt();
            if (acount_expire_time <= 0)
            {
                acount_expire_time = UserInfo.ACCOUNT_EXPIRATION_TIME;
            }
            FormsPrincipal.Login(userInfo.UserName, userInfo, acount_expire_time, GetHttpContext(_Request));
        }
        /// <summary>
        /// 应用程序认证请求
        /// </summary>
        /// <param name="sender">发送对象</param>
        /// <param name="e">事件参数</param>
        public void Application_AuthenticateRequest(object sender, EventArgs e)
        {
            HttpApplication app      = (HttpApplication)sender;
            string          username = string.Empty;

            if (app.Context.User != null && app.Context.User.Identity != null)
            {
                username = app.Context.User.Identity.Name;
            }
            int w = 0;
            int h = 0;

            if (app.Context.Request["nfm"].ObjToInt() == 1)
            {
                username = app.Context.Request["un"].ObjToStr(); //请求中自带的用户名
                w        = app.Context.Request["w"].ObjToInt();
                h        = app.Context.Request["h"].ObjToInt();
            }
            if (!string.IsNullOrEmpty(username))
            {
                UserInfo tempUserInfo = UserInfo.GetCurretnUser(app.Context);
                if (tempUserInfo == null || tempUserInfo.UserId == Guid.Empty || tempUserInfo.UserName.ToLower() != username.ToLower())
                {
                    Guid     userId   = UserOperate.GetUserIdByUserName(username);
                    UserInfo userInfo = UserOperate.GetUserInfo(userId);
                    if (w > 0 && h > 0)
                    {
                        userInfo.ClientBrowserWidth  = w;
                        userInfo.ClientBrowserHeight = h;
                    }
                    //缓存用户扩展信息
                    UserInfo.CacheUserExtendInfo(userInfo.UserName, userInfo.ExtendUserObject);
                    //保存票据
                    FormsPrincipal.Login(userInfo.UserName, userInfo, UserInfo.ACCOUNT_EXPIRATION_TIME, app.Context);
                }
                FormsPrincipal.TrySetUserInfo(app.Context);
            }
            else
            {
                FormsPrincipal.TrySetUserInfo(app.Context);
            }
        }
Example #4
0
        public ActionResult MiniLogin(LoginVM loginModel)
        {
            SysUserVM sysUser = _accountBizProcess.Login(loginModel.SysUserName, loginModel.PasswordHash);

            if (sysUser != null)
            {
                UserInfo userInfo = sysUser.ToUserInfo();
                FormsPrincipal <UserInfo> .Login(sysUser.UserName, userInfo, 30);

                //登录成功写cookie
                var userNameCookie = new HttpCookie("username", sysUser.UserName);
                userNameCookie.Expires = DateTime.Now.AddDays(365);
                var rememberMeCookie = new HttpCookie("rememberme", loginModel.RememberMe.ToString().ToLower());
                userNameCookie.Expires = DateTime.Now.AddDays(365);

                Response.Cookies.Add(userNameCookie);

                return(DWZHelper.ReturnSuccAndClose("欢迎您回来!"));
            }
            else
            {
                return(DWZHelper.ReturnError("账号或密码输入错误"));
            }
        }
Example #5
0
        public ActionResult Login(LoginVM loginModel)
        {
            ViewBag.IsShowValidateCode = false;

            if (ModelState.IsValid)
            {
                bool validatecode = true;
                if (Session["LoginError"] != null && Convert.ToInt32(Session["LoginError"]) >= 3)
                {
                    if (TempData.ContainsKey(SecurityController.VALIDATECODE))
                    {
                        string code = TempData[SecurityController.VALIDATECODE].ToString();
                        validatecode = loginModel.ValidateCode == code;
                    }
                    else
                    {
                        validatecode = false;
                    }
                }

                if (validatecode)
                {
                    SysUserVM sysUser = _accountBizProcess.Login(loginModel.SysUserName, loginModel.PasswordHash);
                    if (sysUser != null)
                    {
                        UserInfo userInfo = sysUser.ToUserInfo();
                        FormsPrincipal <UserInfo> .Login(sysUser.UserName, userInfo, 30);

                        Session["LoginError"] = null;

                        //登录成功写cookie
                        var userNameCookie = new HttpCookie("username", sysUser.UserName);
                        userNameCookie.Expires = DateTime.Now.AddDays(365);
                        var rememberMeCookie = new HttpCookie("rememberme", loginModel.RememberMe.ToString().ToLower());
                        userNameCookie.Expires = DateTime.Now.AddDays(365);

                        Response.Cookies.Add(userNameCookie);

                        return(RedirectToAction("Index", "Home"));
                    }
                    else
                    {
                        ModelState.AddModelError("", "用户名或者密码输入错误!");
                    }
                }
                else
                {
                    ModelState.AddModelError("ValidateCode", "验证码输入错误!");
                }
            }

            Session["LoginError"] = Session["LoginError"] == null ? 0 : Convert.ToInt32(Session["LoginError"]) + 1;
            if (Convert.ToInt32(Session["LoginError"]) >= 3)
            {
                ViewBag.IsShowValidateCode = true;
            }

            //绑定错误信息
            ViewData.BindErrorMessage(ModelState);

            return(View(loginModel));
        }