protected void ButtonSignIn_Click(object sender, EventArgs e)
{
string username = TextBoxUserName.Text;
string password = TextBoxPassword.Text;
var db = new TestEntities();
usercredential record = db.usercredentials.Where(x => string.Compare(x.UserName, username, true) == 0).FirstOrDefault();
if (record == default(usercredential))
{
throw new ApplicationException("invalid user name and password");
}
string salt = record.Salt;
byte[] passwordAndSaltBytes = System.Text.Encoding.UTF8.GetBytes(password + salt);
byte[] hashBytes = new System.Security.Cryptography.SHA256Managed().ComputeHash(passwordAndSaltBytes);
string hashString = Convert.ToBase64String(hashBytes);
if (hashString == record.PasswordHash)
{
// user login successfully
}
else
{
throw new ApplicationException("invalid user name and password");
}
}
总结:单单使用哈希函数来为密码加密是不够的,需要为密码加盐来提高安全性,盐的长度不能过短,并且盐的产生应该是随机的。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持ASPKU!
注:相关教程知识阅读请移步到c#教程频道。










