尝试在数据库中存储哈希数据时获取“ EntityValidationError”

时间:2019-04-16 13:52:19

标签: sql asp.net-mvc entity-framework encryption hash

我正在尝试使用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; }
}

希望有人可以帮助我解决这个问题!

谢谢!

2 个答案:

答案 0 :(得分:0)

尝试获取确切的错误消息

 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);
            }
        }
    }

答案 1 :(得分:0)

首先感谢您的帮助。我想通了,我对我的第一个密码进行了哈希处理,但没有确认密码。因此,这两个密码不相等,我的应用程序禁止将此密码写入数据库。