在登陆页面中,取到页面的用户名和密码,然后通过加载实体读取Entites,
当密码不加密时可以通过下面的方法取到Entites,加密后就无法取到.
密码是byte[]类型的
/// <summary> /// 加载实体集合 /// </summary> /// <param name="whereLambda"></param> /// <returns></returns> public virtual IQueryable<T> LoadEntites(Func<T, bool> whereLambda) { return objectContext.CreateObjectSet<T>().Where<T>(whereLambda).AsQueryable(); }
/// <summary> /// 验证登陆用户 /// </summary> /// <param name="userName"></param> /// <param name="password"></param> /// <returns></returns> public SYS_UserInfo CheckUserLogin(string userName, string password) { byte[] encpassword = PageValidate.EncryptPassword(password); return this.DbSession.SYS_UserInfoRepository.LoadEntites(u => u.UserName.Equals(userName) && u.Password.Equals(password)).FirstOrDefault(); }
加密方法如下:
//密码加密
public static byte[] EncryptPassword(string password)
{
byte[] bytes = new UnicodeEncoding().GetBytes(password);
SHA1 sha = new SHA1CryptoServiceProvider();
return sha.ComputeHash(bytes);
}
byte[]是不能直接进行比较的,我采用了下面的方法进行比较:
/// <summary> /// 比较两个字节数组是否相等 /// </summary> /// <param name="b1">byte数组1</param> /// <param name="b2">byte数组2</param> /// <returns>是否相等</returns> private bool PasswordEquals(byte[] b1, byte[] b2) { if (b1 == null || b2 == null) return false; if (b1.Length != b2.Length) return false; for (int i = 0; i < b1.Length; i++) if (b1[i] != b2[i]) return false; return true; }
登陆验证的方法改为:
/// <summary> /// 验证登陆用户 /// </summary> /// <param name="userName"></param> /// <param name="password"></param> /// <returns></returns> public SYS_UserInfo CheckUserLogin(string userName, string password) { byte[] encpassword = PageValidate.EncryptPassword(password); return this.DbSession.SYS_UserInfoRepository.LoadEntites(u => u.UserName.Equals(userName) && PasswordEquals(u.Password,encpassword)).FirstOrDefault(); }
使用BitConverter.ToString(byte[])方法将byte[]转换为字符串在比较啊