smhy8187

 

控制验证登陆次数

http://blog.csdn.net/javabuilder9/archive/2007/05/26/1626751.aspx

控制验证登陆次数
收藏

新一篇: 时间参数

为了防止暴力破解 ,通常使用验证码,现在加入一种新的防暴力破解方法,限制验证登陆错误次数,并封锁IP
代码如下
    /**/
    public class Login : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.RequiredFieldValidator rfvUsername;
        
protected System.Web.UI.WebControls.RequiredFieldValidator rfvPassword;
        
protected System.Web.UI.WebControls.Label lblErrorMessage;
        
protected System.Web.UI.WebControls.TextBox txtUserName;
        
protected System.Web.UI.WebControls.TextBox txtPassWord;
        
protected System.Web.UI.WebControls.TextBox txtYanzheng;
        
protected System.Web.UI.WebControls.RequiredFieldValidator Requiredfieldvalidator1;
        
protected System.Web.UI.WebControls.ImageButton btnLogin;
        
protected System.Web.UI.WebControls.Image Image1;
        
        
string strIp="";
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
// 在此处放置用户代码以初始化页面
            if(!Page.IsPostBack)
            
{
                Session[
"CheckCode"= "";
            }

        }


        
#region Web 窗体设计器生成的代码
        
override protected void OnInit(EventArgs e)
        
{
            
//
            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            
//
            InitializeComponent();
            
base.OnInit(e);
        }

        
        
/// <summary>
        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
        
/// 此方法的内容。
        
/// </summary>

        private void InitializeComponent()
        
{    
            
this.btnLogin.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButton2_Click);
            
this.Load += new System.EventHandler(this.Page_Load);

        }

        
#endregion


        
        

        
private void ImageButton2_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        
{
            
            WebJTJ.Components.IPLock ip
=new WebJTJ.Components.IPLock();
            
bool IPLocked=ip.IPLockCheck(GetIP());
            
if(IPLocked)
            
{
                lblErrorMessage.Text
="您的ip地址已经被锁定,请联系管理员!"    ;
            }

            
else
            
{
                
if(Convert.ToInt16(Session["LoginTimes"])<1)
                
{
                    
if(txtYanzheng.Text.Trim()==Session["CheckCode"].ToString())
                    
{
                        
string UserID ="";
                        
                        WebJTJ.Components.Staff staff
=new WebJTJ.Components.Staff();
                        
string CheckString = staff.Login(txtUserName.Text, txtPassWord.Text);
                
                        
if(CheckString!=null)
                        
{
                            
string LoginChecked;
                            
if(CheckString.IndexOf("-")>=0)
                            
{
                                UserID
=CheckString.Substring(0,CheckString.IndexOf("-"));
                                LoginChecked 
= CheckString.Substring(CheckString.IndexOf("-")+1);

                                
if(LoginChecked=="True")
                                
{
                                    Session[
"UserName"]=txtUserName.Text;
                                    Session[
"UserID"]=UserID;
                                    Session[
"WebManager"]=LoginChecked;
                                    Response.Redirect(
"Admin_Main.aspx");
                                }

                            }

                        }

                        
else
                        
{
                            Session[
"LoginTimes"]=Convert.ToInt16(Session["LoginTimes"])+1;
                            lblErrorMessage.Text
="您输入的用户名或密码错误,请重新输入!";    
                        }

                    }

                    
else
                    
{
                        lblErrorMessage.Text
="您输入的验证码错误!请重新输入!";
                    }

                }

                
else
                
{
                    
if(Request.ServerVariables["HTTP_VIA"]!=null)
                    
{       
                        strIp
=Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();       
                    }

                    
else
                    
{       
                        strIp
=Request.ServerVariables["REMOTE_ADDR"].ToString();       
                    }

                    
if(strIp.Length>0)
                    
{
                                            
                        WebJTJ.Components.Staff IPLock
=new WebJTJ.Components.Staff();
                        IPLock.IPLock(strIp);
                    
                    }

                    lblErrorMessage.Text
="对不起,您的密码输入次数已经收到限制,您的IP地址已经被记录封锁,请联系管理员给您解除锁定!";
                }

        
            }

            
        

        }


        
public string GetIP()
        
{
            
if(Request.ServerVariables["HTTP_VIA"]!=null)
             
{       
               strIp
=Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();       
             }

            
else
             
{       
               strIp
=Request.ServerVariables["REMOTE_ADDR"].ToString();       
             }

            
return strIp ;

        }

    }

}

posted on 2008-06-27 09:27  new2008  阅读(300)  评论(0编辑  收藏  举报

导航