我正在尝试使用Entity Framework在ASP.net中创建一个注册表单。注册工作正常而不试图散列数据,但当我尝试散列数据时,我收到以下错误:“EntityValidationErrors”。
加密类
public class Encrypt
{
public static string GetMD5Hash(string input)
{
using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider()) {
byte[] b = System.Text.Encoding.UTF8.GetBytes(input);
b = md5.ComputeHash(b);
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (byte x in b)
{
sb.Append(x.ToString("x2"));
}
return sb.ToString();
}
}
}
和
public class CustomPasswordHasher : IPasswordHasher
{
public string HashPassword(string password)
{
return Encrypt.GetMD5Hash(password);
}
public PasswordVerificationResult VerifyHashedPassword(string hashedPassword, string providedPassword)
{
if (hashedPassword == HashPassword(providedPassword))
{
return PasswordVerificationResult.Success;
}
else
{
return PasswordVerificationResult.Failed;
}
}
}
我正在尝试加密数据的方法
[HttpPost]
public ActionResult Register(CustomUser user)
{
CustomPasswordHasher cph = new CustomPasswordHasher();
var hashedpw = cph.HashPassword(user.Password);
user.Password = hashedpw;
using (DBModelEntities dbModel = new DBModelEntities())
{
if (dbModel.CustomUsers.Any(x => x.Email == user.Email))
{
ViewBag.DuplicateMessage = "Email already in use";
return View("Register", user);
}
dbModel.CustomUsers.Add(user);
dbModel.SaveChanges();
}
ModelState.Clear();
ViewBag.SuccesMessage = "Succes";
return View("Register", new customUser());
}
和我的CustomUserModel类
public partial class CustomUser
{
public int UserID { get; set; }
[Required(ErrorMessage = "Verplicht")]
public string Email { get; set; }
[Required(ErrorMessage = "Verplicht")]
[DataType(DataType.Password)]
public string Password { get; set; }
[Required(ErrorMessage = "Verplicht")]
[DataType(DataType.Password)]
[DisplayName("Confirm password")]
[Compare("Password")]
public string ConfirmPassword { get; set; }
public string LoginErrorMessage { get; set; }
}
希望有人可以帮我解决这个问题!
提前致谢!
尝试获取确切的错误消息
catch (DbEntityValidationException dbEx)
{
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
Trace.TraceInformation("Property: {0} Error: {1}",
validationError.PropertyName,
validationError.ErrorMessage);
}
}
}
首先感谢您的帮助。我想通了,我把我的第一个密码丢了,但不是我的确认密码。由于这两个密码不相等,我的应用程序阻止将其写入数据库。