当前位置: 编程技术>.net/c#/asp.net
ASP.NET会员注册登录模块(MD5加密,防止SQL注入,判断是否注册)
来源: 互联网 发布时间:2014-08-30
本文导语: MD5加密,Parameters防止SQL注入: 代码示例: protected void btnLog_Click(object sender, EventArgs e) { //获取验证码 string code = txtCode.Text; //判断用户输入的验证码是否正确 if (Request.Cookies["CheckCode"].Value == code) { ...
MD5加密,Parameters防止SQL注入:
代码示例:
protected void btnLog_Click(object sender, EventArgs e)
{
//获取验证码
string code = txtCode.Text;
//判断用户输入的验证码是否正确
if (Request.Cookies["CheckCode"].Value == code)
{
//创建数据库连接
SqlConnection con = new SqlConnection("server=.;database=db_Register;uid=sa;pwd=102545;");
//打开数据库连接
con.Open();
//使用MD5加密将用户输入的密码加密
string pass = FormsAuthentication.HashPasswordForStoringInConfigFile(txtUserpass.Text, "MD5");
//创建SQL语句,该语句用来查询用户输入的用户名和密码是否正确
string sqlSel = "select count(*) from tb_userInfo where userName=@name and userPass=@pass";
//创建SqlCommand对象
SqlCommand com = new SqlCommand(sqlSel, con);
//使用Parameters的add方法添加参数类型,防止SQL注入,Parameters属性传参的方法将非法字符过滤掉.
com.Parameters.Add(new SqlParameter("name", SqlDbType.VarChar, 20));
//设置Parameters的参数值
com.Parameters["name"].Value = txtUserName.Text;
com.Parameters.Add(new SqlParameter("pass", SqlDbType.VarChar, 50));
com.Parameters["pass"].Value = pass;
//判断ExecuteScalar方法返回的参数是否大于0大于表示登录成功并给出提示
if (Convert.ToInt32(com.ExecuteScalar()) > 0)
{
RegisterStartupScript("", "alert(''登录成功!'')");
//清空文本框
txtCode.Text = txtUserName.Text = "";
}else
{
RegisterStartupScript("", "alert(''用户名或密码错误!'')");
}
}
else
{
RegisterStartupScript("", "alert(''验证码输入错误!'')");
}
}
{
//获取验证码
string code = txtCode.Text;
//判断用户输入的验证码是否正确
if (Request.Cookies["CheckCode"].Value == code)
{
//创建数据库连接
SqlConnection con = new SqlConnection("server=.;database=db_Register;uid=sa;pwd=102545;");
//打开数据库连接
con.Open();
//使用MD5加密将用户输入的密码加密
string pass = FormsAuthentication.HashPasswordForStoringInConfigFile(txtUserpass.Text, "MD5");
//创建SQL语句,该语句用来查询用户输入的用户名和密码是否正确
string sqlSel = "select count(*) from tb_userInfo where userName=@name and userPass=@pass";
//创建SqlCommand对象
SqlCommand com = new SqlCommand(sqlSel, con);
//使用Parameters的add方法添加参数类型,防止SQL注入,Parameters属性传参的方法将非法字符过滤掉.
com.Parameters.Add(new SqlParameter("name", SqlDbType.VarChar, 20));
//设置Parameters的参数值
com.Parameters["name"].Value = txtUserName.Text;
com.Parameters.Add(new SqlParameter("pass", SqlDbType.VarChar, 50));
com.Parameters["pass"].Value = pass;
//判断ExecuteScalar方法返回的参数是否大于0大于表示登录成功并给出提示
if (Convert.ToInt32(com.ExecuteScalar()) > 0)
{
RegisterStartupScript("", "alert(''登录成功!'')");
//清空文本框
txtCode.Text = txtUserName.Text = "";
}else
{
RegisterStartupScript("", "alert(''用户名或密码错误!'')");
}
}
else
{
RegisterStartupScript("", "alert(''验证码输入错误!'')");
}
}
设置密码强度:
代码示例:
function passHint()
{
var txt=document.getElementById(''txtPass'').value;
if(txt.length 0)
{
blIsName = true;
}
else
{
blIsName = false;
}
//返回布尔值变量
return blIsName;
}
protected bool isNameFormar()
{
//创建一个布尔型变量并初始化为false;
bool blNameFormar = false;
//设置正则表达式
Regex re = new Regex("^\w+$");
//使用Regex对象中的IsMatch方法判断用户名是否满足正则表达式
if (re.IsMatch(txtName.Text))
{
//设置布尔变量为true
blNameFormar = true;
//设置label控件的颜色
labUser.ForeColor = System.Drawing.Color.Black;
}
else
{
labUser.ForeColor = System.Drawing.Color.Red;
blNameFormar = false;
}
//返回布尔型变量
return blNameFormar;
}
protected void txtName_TextChanged(object sender, EventArgs e)
{
//判断用户名是否为空
if (txtName.Text == "")
{
//使用Label控件给出提示
labIsName.Text = "用户名不能为空";
//设置Label控件的颜色
labIsName.ForeColor = System.Drawing.Color.Red;
}
else
{
//调用自定义isNameFormar方法判断用户名是否满足格式要求
if (isNameFormar())
{
//调用isName自定义方法判断用户名是否已注册
if (isName())
{
labIsName.Text = "用户名已存在!";
labIsName.ForeColor = System.Drawing.Color.Red;
}
else
{
labIsName.Text = "可以注册!";
labIsName.ForeColor = System.Drawing.Color.Blue;
}
}
else
{
labIsName.Text = "";
} }
}
{
var txt=document.getElementById(''txtPass'').value;
if(txt.length 0)
{
blIsName = true;
}
else
{
blIsName = false;
}
//返回布尔值变量
return blIsName;
}
protected bool isNameFormar()
{
//创建一个布尔型变量并初始化为false;
bool blNameFormar = false;
//设置正则表达式
Regex re = new Regex("^\w+$");
//使用Regex对象中的IsMatch方法判断用户名是否满足正则表达式
if (re.IsMatch(txtName.Text))
{
//设置布尔变量为true
blNameFormar = true;
//设置label控件的颜色
labUser.ForeColor = System.Drawing.Color.Black;
}
else
{
labUser.ForeColor = System.Drawing.Color.Red;
blNameFormar = false;
}
//返回布尔型变量
return blNameFormar;
}
protected void txtName_TextChanged(object sender, EventArgs e)
{
//判断用户名是否为空
if (txtName.Text == "")
{
//使用Label控件给出提示
labIsName.Text = "用户名不能为空";
//设置Label控件的颜色
labIsName.ForeColor = System.Drawing.Color.Red;
}
else
{
//调用自定义isNameFormar方法判断用户名是否满足格式要求
if (isNameFormar())
{
//调用isName自定义方法判断用户名是否已注册
if (isName())
{
labIsName.Text = "用户名已存在!";
labIsName.ForeColor = System.Drawing.Color.Red;
}
else
{
labIsName.Text = "可以注册!";
labIsName.ForeColor = System.Drawing.Color.Blue;
}
}
else
{
labIsName.Text = "";
} }
}