c# - How do I edit a FormCollection value, and then use TryUpdateModel with the edited collection? -
mvc5 storing passwords in plaintext. don't want use default hashing algorithm, i'm required use e.encrypt()
instead. i'm creating registration function, , need know how can edit values formcollection
before using tryupdatemodel
.
here's code:
[httppost] public actionresult register([bind(include = "user,pass,email")] formcollection form) { var user = new users(); string hash = e.encrypt(form["pass"]); // gets set. if (tryupdatemodel(user, form)) { context.entry(user).state = entitystate.added; context.savechanges(); return redirecttoaction("login", "account"); } return view(); }
i've searched high , low, , i've found irrelevant needs.
i've tried this:
form["password"] = e.encrypt(form["password"])
...and compiles, when debugging, value never gets set. e.encrypt()
work function, it's not that.
what doing wrong?
i figured out after trial , error:
[httppost] // remove formcollection , replace usermodel. public actionresult register([bind(include= "username,password,emailaddress")] usermodel user) { if (tryupdatemodel(user)) { // set password in tryupdate success. user.password = e.encrypt(user.password);; // set correctly context.entry(user).state = entitystate.added; context.savechanges(); return redirecttoaction("login", "account"); } return view(); }
however, issue popped up, datavalidationerror
. issue usermodel.cs
class:
[regularexpression(regexpassword, errormessage = errorpassword)]
i had regular expression didn't match hash, when tried update, unable validate. that's problem thread, removed now.