//拿到账号
//var account = model.Set
().First(p=>p.UserName==user.UserName); string pwd = MD5Helper.MD5Encoding(user.Password, account.Salt);if (pwd != account.Password)
{
return ;
}
using System; using System.Security.Cryptography; using System.Text; namespace AspMVC.Common { public class MD5Helper { ////// MD5 加密字符串 /// /// 源字符串 ///加密后字符串 public static string MD5Encoding(string str) { // 创建MD5类的默认实例:MD5CryptoServiceProvider MD5 md5 = MD5.Create(); byte[] bs = Encoding.UTF8.GetBytes(str); byte[] hs = md5.ComputeHash(bs); StringBuilder stb = new StringBuilder(); foreach (byte b in hs) { // 以十六进制格式格式化 stb.Append(b.ToString("x2")); } return stb.ToString(); } ////// MD5盐值加密 /// /// 源字符串 /// 盐值 ///加密后字符串 public static string MD5Encoding(string rawPass, object salt) { if (salt == null) return rawPass; return MD5Encoding(rawPass + "{" + salt.ToString() + "}"); } ////// 获取盐(用户登录密码)+生成验证码文字 /// /// ///public static string CreateVerifyCode(int len) { char[] data = { 'a','c','d','e','f','h','k','m', 'n','r','s','t','w','x','y'}; StringBuilder sb = new StringBuilder(); Random rand = new Random(); for (int i = 0; i < len; i++) { int index = rand.Next(data.Length);//[0,data.length) char ch = data[index]; sb.Append(ch); } return sb.ToString(); } } }



