asp.net密码加密登录
密码加密登录
asp中使用MD5加密登录的实现及功能代码
## 第一步
新建一个aspx页面,然后进入后台代码
protected void Page_Load(object sender, EventArgs e)
{
}
private static string PasswordEncryption(string pwd)
{
//创建SHA1加密算法对象
SHA1 sha1 = SHA1.Create();
//将原始密码转换为字节数组
byte[] originalPwd = Encoding.UTF8.GetBytes(pwd);
//执行加密
byte[] encryPwd = sha1.ComputeHash(originalPwd);
//将加密后的字节数组转换为大写字符串
return string.Join("", encryPwd.Select(b => string.Format("{0:x2}",
b)).ToArray()).ToUpper();
}
## 第二步
进入登录按钮编辑
protected void btnLogin_ServerClick(object sender, EventArgs e)
{
using (TRDBEntities db = new TRDBEntities())
{
string pwd = PasswordEncryption(this.txtLoginPwd.Value);
var user = db.Users.Where(s => s.LoginId ==
this.txtLoginID.Value && s.LoginPwd == pwd).ToList();
if (user.Count > 0)
{
string sn = Session["sn"] as string;
if (txtValidateCode.Value == sn)
{
this.Label1.Visible = true;
}
}
}
}
这样一个简单的MD5加密就实现了