ASP.NET登入驗證

蘭博丶專屬發表於2018-05-24
   protected void btnLogin_Click(object sender, EventArgs e)
        {
            string username = txtUserName.Value.Trim();
            string userpassword = txtUserPassword.Value.Trim();

            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(userpassword))
            {
                Common.MessageBox.Show(this, "賬號和密碼不能為空!");
            }
            else
            {
                string ip = Request.UserHostAddress;
                BLL.ProxyInfo bllMember = new ProxyInfo();
                string encrytpswd = DimoNetwork.Common.DEncrypt.TextEncrypt.MD5EncryptPassword(userpassword);
                int reuslt;
                Model.UserInfo userInfo= bllMember.ProxyLogin(username, encrytpswd, ip, out reuslt);
                if (mProxyInfo != null)
                {
                    //判斷使用者是否被凍結
                    if (userInfo.freezeState)
                    {
                        Common.MessageBox.Show(this, "您的賬號已被凍結,請聯絡你的上級代理!");
                    }
                    else
                    {
                        Common.Cache.DimoCache.Default.Save<Common.Cache.SessionCache>("MemberSession", userInfo);
                        Common.MessageBox.ShowAndRedirects(this.Page, "", "main.aspx");
                    }
                }
                else
                {
                    AddLoginErrorCount();
                    if (GetLoginErrorCount() >= 5)
                    {
                        Common.MessageBox.Show(this, "賬號或密碼錯誤次數過多,賬號被鎖定20分鐘,請20分鐘後重試!");
                        txtUserName.Value = "";
                        txtUserPassword.Value = "";
                        btnLogin.Visible = false;
                    }
                    else
                    {
                        Common.MessageBox.Show(this, "賬號或密碼錯誤!");
                    }
                }
            }
        }
        /// <summary>
        /// 獲得登陸錯誤次數
        /// </summary>
        /// <returns></returns>
        protected int GetLoginErrorCount()
        {
            int logincount = 0;
            object logincountcache = Common.Cache.Default.Get<Common.Cache.SessionCache>(lockcachekey);
            if (logincountcache != null)
            {
                logincount = Convert.ToInt32(logincountcache);
            }
            return logincount;
        }
  /// <summary>
        /// 修改使用者登入錯誤次數
        /// </summary>
        protected void AddLoginErrorCount()
        {
            object logincountcache = Common.Cache.DimoCache.Default.Get<Common.Cache.SessionCache>(lockcachekey);
            if (logincountcache != null)
            {
               int logincount = Convert.ToInt32(logincountcache);
               Common.Cache.Default.Save<Common.Cache.SessionCache>(lockcachekey,logincount + 1);
            }
        }

 

相關文章