TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
rahul patil
NA
160
8.5k
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
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<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.
Reply
Answers (
5
)
How to open pdf and xls, doc file into browser .
How to change to static method to without static ?