ASP.NET使用MD5加密用户验证
2008-10-25 12:51 $等待$ 阅读(460) 评论(0) 收藏 举报1 using System.Data.SqlClient;
2 using System.Configuration;
3 using System.Web.Configuration;
4 using System.Security.Cryptography;
5 using System.Text;
代码部分:
protected void Button1_Click(object sender, EventArgs e)
{
//建立数据库连接
string ConnString = WebConfigurationManager.ConnectionStrings["ABCDWebConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(ConnString);
//创建数据库命令
string sql;
sql = "Select COUNT(*) From Users Where UName=@Username and PassW=@Password";
SqlCommand Comm = new SqlCommand(sql, conn);
//重建用户名验证
SqlParameter Username = new SqlParameter("@Username", SqlDbType.NChar, 50);
Username.Value = TextBox1.Text;
Comm.Parameters.Add(Username);
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
Byte[] hashedDataBytes;
UTF8Encoding encoder = new UTF8Encoding();
hashedDataBytes = md5Hasher.ComputeHash(encoder.GetBytes(TextBox2.Text));
string b = "";
for (int i = 0; i <= 15; ++i)
{
b += hashedDataBytes[i];
}
SqlParameter Password = new SqlParameter("@Password", SqlDbType.NChar, 50);
Password.Value = b;
Comm.Parameters.Add(Password);
conn.Open();
int j = Convert.ToInt16(Comm.ExecuteScalar());
conn.Close();
if (j == 1)
{
Label1.Text = "正确";
}
else
{
Label1.Text = "错误";
}
}
浙公网安备 33010602011771号