I am storing the password encrypted format in table and assign there value in modelclass property of password but give an error?
validation failed one or more property.
see my watch window
HomeController.cs
- public static string Encrypt(string clearText)
- {
- try
- {
- byte[] hashBytes = ComputeHash(clearText);
- byte[] saltBytes = GetRandomSalt();
- byte[] saltHash = ComputeHash(saltBytes.ToString());
-
- byte[] hashWithSaltBytes = new byte[hashBytes.Length + saltBytes.Length];
- for (int i = 0; i < hashBytes.Length; i++)
- hashWithSaltBytes[i] = hashBytes[i];
- for (int i = 0; i < saltBytes.Length; i++)
- hashWithSaltBytes[hashBytes.Length + i] = saltBytes[i];
-
- string hashValue = Convert.ToBase64String(hashWithSaltBytes);
-
- return hashValue;
- }
- catch (Exception)
- {
-
- throw;
- }
- }
- public static byte[] GetRandomSalt()
- {
- int minSaltSize = 16;
- int maxSaltSize = 32;
-
- Random random = new Random();
- int saltSize = random.Next(minSaltSize, maxSaltSize);
- byte[] saltBytes = new byte[saltSize];
- RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
- rng.GetNonZeroBytes(saltBytes);
- return saltBytes;
- }
-
- public static byte[] ComputeHash(string plainText)
- {
- byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
- HashAlgorithm hash = new SHA256Managed();
- return hash.ComputeHash(plainTextBytes);
- }
-
- public ActionResult create()
- {
- return View();
- }
-
- [HttpPost]
- public ActionResult create(student stud)
- {
- string pass = Encrypt(stud.password);
- stud.password = pass;
-
- var create = dbstud.students.Add(stud);
- dbstud.SaveChanges();
- return RedirectToAction("Login");
-
- }
student.cs
- {
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
- public student()
- {
- this.blogs = new HashSet<blog>();
- }
-
- public int studid { get; set; }
- public string firstname { get; set; }
- public string lastname { get; set; }
- public string username { get; set; }
- public string password { get; set; }
- public string email { get; set; }
-
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
- public virtual ICollection<blog> blogs { get; set; }
dbstud.SaveChanges(); //here give an error validation failed one or more entities.