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; //assigning a string pass to stud.pass
- var create = dbstud.students.Add(stud);
- dbstud.SaveChanges(); //here give an error validation failed one or more entities.
- return RedirectToAction("Login");
- }
student.cs
- {
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
- public student()
- {
- this.blogs = new HashSet
(); - }
- 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
blogs { get; set; }
dbstud.SaveChanges(); //here give an error validation failed one or more entities.

rahul patil159 reputationPosted Jun 9, 2020, 5:06 AM EDT
rahul patil159 reputationPosted Jun 9, 2020, 5:19 AM EDT
Rajanikant Hawaldar
Rajanikant HawaldarRank #110 · 20.2K reputationPosted Jun 9, 2020, 4:47 AM EDT
Viswanatha SwamyRank #43 · 33.4K reputationPosted Jun 9, 2020, 4:50 AM EDT
rahul patil159 reputationPosted Jun 9, 2020, 4:58 AM EDT
Viswanatha Swamy
create
student