Exemplo n.º 1
0
        /// <summary>
        /// 用户登录
        /// 添加人:周 鹏
        /// 添加时间:2014-01-03
        /// </summary>
        /// <param name="loginName">登录名</param>
        /// <param name="passWord">密码</param>
        /// <param name="remberPwd">保存登录账号密码</param>
        /// <param name="decodePassWord">是否对密码进行加密</param>
        /// <param name="createPersistentCookie">是否永久保存</param>
        /// <returns></returns>
        public UserLoginStatus UserLogin(string loginName, string passWord, string remberPwd, bool decodePassWord = true, bool createPersistentCookie = false)
        {
            var user            = new CrmUserBll().GetUserEntity(loginName, "Account");
            var loginErrorCount = new ComLoginLogBll().GetLoginErrorCount(loginName);

            if (loginErrorCount >= 5)
            {
                return(UserLoginStatus.TemporaryLocked);
            }
            if (user != null && !string.IsNullOrEmpty(user.Id))
            {
                passWord = DESHelper.ToDESEncrypt(passWord, AppConst.EncryptKey);  //将密码进行加密
                if (!user.Password.Equals(passWord))
                {
                    return(UserLoginStatus.PasswordError);
                }

                if (!string.IsNullOrEmpty(remberPwd) && remberPwd.Equals("checked"))
                {
                    //验证通过,设置Cookie信息
                    CookieUtil.SetCookie(AppConst.LoginUserNameCookieName,
                                         createPersistentCookie ? HttpUtility.UrlEncode(user.Account) : "",
                                         DateTime.MaxValue);
                    //验证通过,设置Cookie信息
                    CookieUtil.SetCookie(AppConst.LoginUserCookiePwd,
                                         createPersistentCookie ? HttpUtility.UrlEncode(user.Password) : "",
                                         DateTime.MaxValue);
                }
                else
                {
                    CookieUtil.Remove(AppConst.LoginUserNameCookieName);
                    CookieUtil.Remove(AppConst.LoginUserCookiePwd);
                }

                var ticket    = new FormsAuthenticationTicket(1, user.Id, DateTime.Now, DateTime.Now.AddDays(1), false, "");
                var strTicket = FormsAuthentication.Encrypt(ticket);
                var cookie    = new HttpCookie(FormsAuthentication.FormsCookieName, strTicket);
                HttpContext.Current.Response.Cookies.Add(cookie);
                return(UserLoginStatus.Sucess);
            }
            return(UserLoginStatus.NotUser);
        }