|
Posted on
2009-05-27 19:08
shanymen
阅读( 7262)
评论()
编辑
收藏
举报
该实例共有三个页面(登录,注册,管理),一个类(DB.cs)。 在DB中有MD5(加密密码方法),reDr(连接sql数据库并返回SqlDataReader),reDt(连接sql数据库并返回DataTable),sqlEx(操作数据库并返回成功数据行数),RandomNum(实现随机验证码方法)五个方法,连接数据库方法不再演示。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) Code
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System.Security.Cryptography;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//**//**//// <summary>
/// MD5加密
/// </summary>
/// <param name="strPwd">被加密的字符串</param>
/// <returns>加密后的字符串</returns>
public string MD5(string strPwd)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) {
MD5 md5 = new MD5CryptoServiceProvider();
//将字符编码为一个字节序列
byte[] data = System.Text.Encoding.Default.GetBytes(strPwd);
//计算data字节数组的哈希值
byte[] md5data = md5.ComputeHash(data);
md5.Clear();
string str = "";
for (int i = 0; i < md5data.Length - 1; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
//将字符转换成16进制并右对齐字符,字符数为2,不足位填0补足
str += md5data[i].ToString("x").PadLeft(2, '0');
}
return str;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//// <summary>
/// 实现随机验证码
/// </summary>
/// <param name="n">显示验证码的个数</param>
/// <returns>返回生成的随机数</returns>
public string RandomNum(int n)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) {
//包含数字,大写英文字母和小谢英文字母的字符串
string strchar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
//将strchar转化为数组
string[] VcArray = strchar.Split(',');
string VNum = "";
//记录上次随即数值,尽量避免产生几个一样的随机数
int temp = -1;
//采用简单的算法保证生成随机数的不同
Random rand = new Random();
for (int i = 1; i <= n; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if (temp != -1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
//unchecked:用于取消整型算术运算和转换的溢出检查
rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
}
//返回一个小于所制定最大值的非负随机数,61为VcArray数组长度
int t = rand.Next(61);
if ((temp != -1) && (temp == t))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return RandomNum(n);
}
temp = t;
VNum += VcArray[t];
}
return VNum;
}
在登录页面中有txtUserName(用户名TextBox控件),txtPwd(密码TextBox控件),txtValidateNum(验证码TextBox控件),btnLogin(登录Button控件),btnRegister(注册Button控件)。 cs代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) Code
using System.Data.SqlClient;
//登录按钮
protected void btnLogin_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) {
//DB为公共类,里面提供了数据库连接及MD5加密方法
DB db = new DB();
string userName = this.txtUserName.Text;
string passWord = db.MD5(this.txtPwd.Text.ToString());//对密码进行MD5加密处理
string num = this.txtValidateNum.Text.Trim();
//判断验证码是否相符
if (Session["ValidateNum"].ToString() == num.ToUpper())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
//获取用户信息
SqlDataReader dr = db.reDr("select * from tb_User where UserName='"+userName+"' and PassWord='"+passWord+"'");
dr.Read();
//如果dr中存在行,则将用户ID和权限存入Session中,并跳转到主页
if (dr.HasRows)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
Session["UserID"] = dr.GetValue(0);
Session["Role"] = dr.GetValue(4);
Response.Redirect("~/UserManagement.aspx");
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
Response.Write("<script>alert('登录失败!请返回查找原因');location='Login.aspx'</script>");
}
dr.Close;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
//提示错误并返回首页
Response.Write("<script>alert('验证码输入错误!');location='Login.aspx'</script>");
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//注册按钮
protected void btnRegister_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) {
//跳转到用户注册页面
Response.Redirect("~/Register.aspx");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
在注册页面中有txtUserName(用户名TextBox控件),txtPwd(密码TextBox控件),txtEmail(邮件TextBox控件),btnOk(注册新用户Button控件),btnCheck(用户名检查Button控件),btnCanel(返回登录Button控件)。 cs代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) Code
int reValue;
//检查用户名是否存在按钮
protected void btnCheck_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) {
//用CheckName方法查找用户名是否存在,已经存在返回-1,不存在返回2
reValue = CheckName();
if (reValue == -1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
Response.Write("<script>alert('用户名存在!');location='Login.aspx'</script>");
this.txtUserName.Focus();
}
else if (reValue == 2)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
Response.Write("<script>alert('恭喜你!该用户名尚未注册!');location='Login.aspx'</script>");
this.txtUserName.Focus();
}
}
//注册新用户按钮
protected void btnOk_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) {
reValue = CheckName();
if (reValue == -1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
Response.Write("<script>alert('用户名存在!');location='Login.aspx'</script>");
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
//将页面输入的内容放进字符串中
DB db = new DB();
string userName = this.txtUserName.Text;
string passWord = db.MD5(this.txtPwd.Text.ToString());
string Email = this.txtEmail.Text;
//插入数据的sql语句
string cmdstr = "insert into tb_User(UserName,Password,Email)values('" + userName + "','" + passWord + "','" + Email + "')";
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
//返回操作成功数据行数
reValue = db.sqlEx(cmdstr);
if (reValue == 1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
Response.Write("<script>alert('注册成功!');location='Login.aspx'</script>");
//清空文本框
Clear();
}
else if (reValue == 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
Response.Write("<script>alert('注册失败!');location='Login.aspx'</script>");
}
}
catch
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
Response.Write("<script>alert('注册失败!');location='Login.aspx'</script>");
}
}
}
//返回按钮
protected void btnCanel_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) {
//跳转到用户登录页面
Response.Redirect("~/Login.aspx");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//清空文本框
private void Clear()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) {
this.txtUserName.Text="";
this.txtPwd.Text="";
this.txtEmail.Text="";
}
//验证用户名是否存在
private int CheckName()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) {
DB db = new DB();
string str = "select count(*) from tb_User where UserName='" + this.txtUserName .Text+ "'";
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
DataTable dt = db.reDt(str);
//如果返回count计数不为0则用户名已经存在,否则用户名尚未注册
if (dt.Rows[0][0].ToString() != "0")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return -1;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return 2;
}
}
catch(Exception e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return 0;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
在管理页面中有DataList1(绑定数据控件),btnExit(退出Button控件)。在DataList1数据绑定后根据权限设置了权限按钮的Enabled 。关于DataList1控件中的模版设置及修改数据库中用户信息的操作见".NET基础之DataList控件"。 cs代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) Code
int reValue;
//检查用户名是否存在按钮
protected void btnCheck_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) {
//用CheckName方法查找用户名是否存在,已经存在返回-1,不存在返回2
reValue = CheckName();
if (reValue == -1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
Response.Write("<script>alert('用户名存在!');location='Login.aspx'</script>");
this.txtUserName.Focus();
}
else if (reValue == 2)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
Response.Write("<script>alert('恭喜你!该用户名尚未注册!');location='Login.aspx'</script>");
this.txtUserName.Focus();
}
}
//注册新用户按钮
protected void btnOk_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) {
reValue = CheckName();
if (reValue == -1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
Response.Write("<script>alert('用户名存在!');location='Login.aspx'</script>");
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
//将页面输入的内容放进字符串中
DB db = new DB();
string userName = this.txtUserName.Text;
string passWord = db.MD5(this.txtPwd.Text.ToString());
string Email = this.txtEmail.Text;
//插入数据的sql语句
string cmdstr = "insert into tb_User(UserName,Password,Email)values('" + userName + "','" + passWord + "','" + Email + "')";
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
//返回操作成功数据行数
reValue = db.sqlEx(cmdstr);
if (reValue == 1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
Response.Write("<script>alert('注册成功!');location='Login.aspx'</script>");
//清空文本框
Clear();
}
else if (reValue == 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
Response.Write("<script>alert('注册失败!');location='Login.aspx'</script>");
}
}
catch
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
Response.Write("<script>alert('注册失败!');location='Login.aspx'</script>");
}
}
}
//返回按钮
protected void btnCanel_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) {
//跳转到用户登录页面
Response.Redirect("~/Login.aspx");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//清空文本框
private void Clear()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) {
this.txtUserName.Text="";
this.txtPwd.Text="";
this.txtEmail.Text="";
}
//验证用户名是否存在
private int CheckName()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) {
DB db = new DB();
string str = "select count(*) from tb_User where UserName='" + this.txtUserName .Text+ "'";
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
DataTable dt = db.reDt(str);
//如果返回count计数不为0则用户名已经存在,否则用户名尚未注册
if (dt.Rows[0][0].ToString() != "0")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return -1;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return 2;
}
}
catch(Exception e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return 0;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
命运负责洗牌,但是玩牌的是我们自己!
|