rahul patil

rahul patil

  • NA
  • 160
  • 7.6k

when store the password then face the error?

Jun 9 2020 8:33 AM
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
  1. public static string Encrypt(string clearText)    
  2. {    
  3.     try    
  4.     {    
  5.         byte[] hashBytes = ComputeHash(clearText);    
  6.         byte[] saltBytes = GetRandomSalt();    
  7.         byte[] saltHash = ComputeHash(saltBytes.ToString());    
  8.   
  9.         byte[] hashWithSaltBytes = new byte[hashBytes.Length + saltBytes.Length];    
  10.         for (int i = 0; i < hashBytes.Length; i++)    
  11.             hashWithSaltBytes[i] = hashBytes[i];    
  12.         for (int i = 0; i < saltBytes.Length; i++)    
  13.             hashWithSaltBytes[hashBytes.Length + i] = saltBytes[i];    
  14.   
  15.         string hashValue = Convert.ToBase64String(hashWithSaltBytes);    
  16.   
  17.         return hashValue;    
  18.     }    
  19.     catch (Exception)    
  20.     {    
  21.   
  22.         throw;    
  23.     }    
  24. }    
  25. public static byte[] GetRandomSalt()    
  26. {    
  27.     int minSaltSize = 16;    
  28.     int maxSaltSize = 32;    
  29.   
  30.     Random random = new Random();    
  31.     int saltSize = random.Next(minSaltSize, maxSaltSize);    
  32.     byte[] saltBytes = new byte[saltSize];    
  33.     RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();    
  34.     rng.GetNonZeroBytes(saltBytes);    
  35.     return saltBytes;    
  36. }    
  37.   
  38. public static byte[] ComputeHash(string plainText)    
  39. {    
  40.     byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);    
  41.     HashAlgorithm hash = new SHA256Managed();    
  42.     return hash.ComputeHash(plainTextBytes);    
  43. }    
  44.   
  45. public ActionResult create()    
  46. {    
  47.     return View();    
  48. }    
  49.   
  50. [HttpPost]    
  51. public ActionResult create(student stud)    
  52. {    
  53.     string pass = Encrypt(stud.password);    
  54.     stud.password = pass; //assigning a string pass to stud.pass    
  55.   
  56.     var create = dbstud.students.Add(stud);    
  57.     dbstud.SaveChanges();     //here give an error validation failed one or more entities.    
  58.     return RedirectToAction("Login");    
  59.   
  60. }   
student.cs
  1. {    
  2.    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage""CA2214:DoNotCallOverridableMethodsInConstructors")]    
  3.    public student()    
  4.    {    
  5.        this.blogs = new HashSet<blog>();    
  6.    }    
  7.     
  8.    public int studid { getset; }    
  9.    public string firstname { getset; }    
  10.    public string lastname { getset; }    
  11.    public string username { getset; }    
  12.    public string password { getset; }    
  13.    public string email { getset; }    
  14.     
  15.    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage""CA2227:CollectionPropertiesShouldBeReadOnly")]    
  16.    public virtual ICollection<blog> blogs { getset; }  
dbstud.SaveChanges(); //here give an error validation failed one or more entities.
 
 

Answers (5)