I created the ADO.NET model of my database. Created a new controller with CRUD (entity structure and using the ADO.NET entity model that I created).
My database has a simple user table. The Password line in the table will store user passwords encrypted using SimpleCrypto (PBKDF2).
In my ADO.NET Users.cs model, I added the following check:
[Required]
[DataType(DataType.Password)]
[StringLength(20, MinimumLength = 6)]
[Display(Name = "Password")]
public string Password { get; set; }
This works with jQuery in a validated browser. But in my controller, I encrypt the password, and then the string "Password" will contain more than 20 characters in length.
var crypto = new SimpleCrypto.PBKDF2();
var encryptedPass = crypto.Compute(user.Password);
user.Password = encryptedPass;
user.PasswordSalt = crypto.Salt;
_db.Users.Add(user);
_db.SaveChanges();
And that gives me and "Failed to validate for one or more objects." - mistake.
"var newUser", , ?
EDIT: , . , , , 6-20 +100 lengt - .
: .
[HttpPost]
public ActionResult Create(Users user)
{
if (!ModelState.IsValid)
{
return View();
}
if (_db.Users.FirstOrDefault(u => u.Email == user.Email) != null)
{
ModelState.AddModelError("", "User already exists in database!");
return View();
}
var crypto = new SimpleCrypto.PBKDF2();
var encryptedPass = crypto.Compute(user.Password);
user.Password = encryptedPass;
user.PasswordSalt = crypto.Salt;
_db.Users.Add(user);
_db.SaveChanges();
return RedirectToAction("Index", "User");
}