吉首大学运网开发工作室 吉首大学新校区2#221 电话:0743-2143867 20 //判断用户是否已经正确输入验证码,错误则抛出异常 throw new Exception(\验证码有错!\
} else { if(CheckLogin(UserName.Text.Trim(),Passwd.Text.Trim())==true) { Session.RemoveAll();
//Session[\ //登陆成功,跳转到管理首页
FormsAuthentication.RedirectFromLoginPage(this.UserName.Text, true);
Response.Redirect(\false);
} else { //登陆错误抛出异常
throw new Exception(\用户名或密码有错\
}
}
}
}
catch(Exception ex) { //显示错误信息 ErrorCode.Text=\
}
}
private void ImageButton2_Click(object sender, System.Web.UI.ImageClickEventArgs e) { /*清空 */
this.ErrorCode.Text=\ this.UserName.Text=\ this.TxtCode.Text=\ this.Passwd.Text=\
//this.UserName.Text=FormsAuthentication.HashPasswordForStoringInConfigFile(Passwd.Text,
\ }
/* 检查登陆*/
private bool CheckLogin(string StrUser,string StrPwd) {
SqlCommand cmd=null;
20
SqlDataAdapter ad=null; DataSet ds=null; try {
if(CheckCanLogin()==true) {
string
吉首大学运网开发工作室 吉首大学新校区2#221 电话:0743-2143867 21 strcon=System.Configuration.ConfigurationSettings.AppSettings[\
//从Web.config文件中加载数据库连接字符串
string strSQL=\//组装登陆查询Sql语句 string
strPass=FormsAuthentication.HashPasswordForStoringInConfigFile(Passwd.Text.Trim(), \
} else {
//从记录集中取出用户名和密码
string user=(string)ds.Tables[\string pwd=(string)ds.Tables[\
if(StrUser!=user.Trim()) {
21
//如果记录集为空,刚表示没有这个用户抛出异常 throw new Exception(\用户名或密码错误\
//由于数据库里存储用户密码是用MD5算法加密的,所在以这儿把用户输入的表单中的数据进行connection =new SqlConnection(strcon); //实例化数据库连接对象 connection.Open(); //打开数据库连接对象
cmd=new SqlCommand(strSQL,connection); //实例化SqlCommand对象 用于执行数据库查询命令 ad=new SqlDataAdapter(cmd);
//实例化数据库连接桥(.NET里的采用的是从数据库中取得数据和记录集建立管道) //这个适配器是数据记集和数据库的连接管道 ds=new DataSet(); //实例化记录集对象 ad.Fill(ds,\
//把从数据库中的数据填充到记录集的缓冲区 if(ds.Tables[0].Rows.Count==0) {
一次MD5加密
} else { if(pwd.Trim()!=strPass) { throw new Exception(\密码错误\throw new Exception(\用户名错误\
吉首大学运网开发工作室 吉首大学新校区2#221 电话:0743-2143867 22 } else { /* 登陆成功后获取其权限*/ int Permission=(int)ds.Tables[\ /* 在session 里保存权限*/ Session[\ /* 写日志*/
WriteLog(StrUser,\正常登陆\ /* 登陆成功返回真*/ return true;
}
} }
} else { /*记录日志*/
WriteLog(StrUser,\输入密码错误达到三次\ /* 输入登陆所需信息连三次错误不能再进行登陆!弹出消息框给用户*/ ShowMessage(\您已经输入出错达到3次!不能再登陆\ /* 抛出异常*/
throw new Exception(\你已经三次输入错误\
}
}
catch(Exception ex) { ErrorCode.Text=\/*ex.Message*/+\ return false;
} finally {
/* 释放对象!把资源返回给系统*/ ds.Clear();
ds.Dispose();
22
吉首大学运网开发工作室 吉首大学新校区2#221 电话:0743-2143867 ad.Dispose(); cmd.Dispose(); if(connection!=null) { connection.Close();
}
}
}
/* 检查输入错误是否已经达到三次*/ private bool CheckCanLogin() { Object code=Session[\ int iNumber=0; if(code==null) { iNumber=1;
Session[\ return true;
} else { iNumber=(int)code; if(iNumber>2) { return false; } else { iNumber++;
Session[\ return true; }
}
}
private void ShowMessage(string strMsg) { Response.Write(\}
private void WriteLog(string user,string LogInfo) {
SqlConnection con=null;
23 23
SqlCommand cmd=null; try { 吉首大学运网开发工作室 吉首大学新校区2#221 电话:0743-2143867 24 string strcon=System.Configuration.ConfigurationSettings.AppSettings[\ string ipAddr=Request.ServerVariables[\ DateTime Time=System.DateTime.Now; /* 组装日志插入SQL语句*/
string strSql=\ strSql=strSql+\ strSql=strSql+\
strSql=strSql+\ strSql=strSql+\ strSql=strSql+\
con=new SqlConnection(strcon); //实例化数据库连接对象 con.Open();
//打开数据库连接对象
cmd=new SqlCommand(strSql,con); int EffectNum=cmd.ExecuteNonQuery(); if(EffectNum==0) { throw new Exception();
}
}
catch(Exception ex) { ErrorCode.Text=\/*ex.Message*/+\
} finally { //释放cmd对象 cmd.Dispose(); if(con!=null) {
con.Close();
}
}
}
24
con.Dispose();