c# - Not getting user with correct credentials with asp.net identity and owin -
i doing custom authentication in mvc using asp.net identity , owin
framework unfortunately not getting user correct credentials below method:
var user = usermanager.find(email, hashednewpassword);
i have stored email , password below in database table:
id emailadress password 1 abc@yahoo.com 123456
right doing later on hash password , store in table.this testing microsoft owin framework.
but not getting user credentials:
emaild id:abc@yahoo.com password:123456.
this code:
controller:
[authorize] public class authenticationcontroller : controller { public authenticationcontroller() : this(new usermanager<usermodel>(new userstore(new myentities()))) { } public authenticationcontroller(usermanager<usermodel> usermanager) { usermanager = usermanager; } public usermanager<usermodel> usermanager { get; private set; } [httppost] [allowanonymous] [validateantiforgerytoken] public async task<actionresult> login(string email, string password, string returnurl) { if (modelstate.isvalid) { var user = usermanager.find(email, password); //getting null here } } } usermodel: public class usermodel : iuser { public string id { get; private set; } public string username { { return email; } set { email = value; } } public string firstname { get; set; } public string lastname { get; set; } public string email { get; set; } public usermodel(string email, string firstname, string lastname) { email = email; firstname = firstname; lastname = lastname; } public usermodel(users dbuser) { if (dbuser == null) return; id = dbuser.id.tostring(); email = dbuser.emailaddress; firstname = dbuser.firstname; lastname = dbuser.lastname; } } public class userstore : iuserstore<usermodel>, iuserpasswordstore<usermodel> { public task<usermodel> findbynameasync(string username) { return task.factory.startnew(() => { httpcontext.current = _httpcontext ?? httpcontext.current; var dbuser = getdbuser(username); if (dbuser == null) return null; return new usermodel(dbuser); }); } private users getdbuser(string username) { using (var db = new myentities()) { return db.users.firstordefault(u => u.emailaddress.equals(username, stringcomparison.ordinalignorecase)); } } public task<string> getpasswordhashasync(usermodel user) { return task.factory.startnew(() => { httpcontext.current = _httpcontext ?? httpcontext.current; var userobj = getdbuser(user); int len = userobj.password.length % 4; //solved error below code :invalid length base-64 char array if (len > 0) userobj.password = userobj.password.padright(userobj.password.length + (4 - len), '='); return userobj.password; //sometimes progam gets stuck here on debugging }); } private users getdbuser(usermodel user) { if (user.id == null) return null; return getdbuser(convert.toint32(user.id)); } private users getdbuser(int userid) { using (var db = new myentities()) { return db.users.firstordefault(u => u.id == userid); } } }
can please me recognize problem???
if understand correctly, you're storing password plain text in database? if that's case, that's problem. can't choose hash later; identity needs hashed in order match password. how authentication works:
- user submits username , password
- identity hashes submitted password
- identity looks user matching submitted username , compares password on record hashed submitted password. if matches, user signed in.
so, when identity compares hashed version of 123456
plain-text 123456
, never match. in order match, password saved in db must hashed well.
Comments
Post a Comment