c# - ASP.NET MVC LinQ - Use our own method in "Any" LinQ expression? -
i'm coding login page in asp.net mvc multiple articles found on web have problem find matching user in database. here post method :
[httppost] [validateantiforgerytoken] public actionresult login([bind(include = "username, password")] user model, string returnurl) { if (modelstate.isvalid) { string username = model.username.tolower(); string plainpassword = model.password; bool uservalid = db.user.any(u => sha512.verifyhash(plainpassword, u.salt, u.password) && u.username_canonical == username); if (uservalid) { formsauthentication.setauthcookie(username, false); if (url.islocalurl(returnurl) && returnurl.length > 1 && returnurl.startswith("/") && !returnurl.startswith("//") && !returnurl.startswith("/\\")) { return redirect(returnurl); } else { return redirecttoaction("index", "home"); } } else { modelstate.addmodelerror("", "the username or password provided incorrect."); } } // if got far, failed, redisplay form return view(model); }
i got "notsupportedexception" in line :
bool uservalid = db.user.any(u => sha512.verifyhash(plainpassword, u.salt, u.password) && u.username_canonical == username);
linq entities not recognize method 'boolean verifyhash(system.string, system.string, system.string)' method, , method cannot translated store expression.
here in mind : verifyhash returns true if provided password (uncrypted) matches crypted 1 stored in database (by getting salt corresponding username, in database , re-encrypting provided password salt, compare newly encrypted hash stored one). usernames uniques.
i understand doesn't accept verifyhash method inside "any" one. don't know how else because need corresponding salt , encrypted password (u.salt , u.password) in addition matching username verify password.
i tried use "find" method in same way doesn't work (vs error, memory need user id pass parameter). i'm new linq entities.
any idea ? forgive eventual english mistakes.
thanks,
hellcat
you getting exception, because entity framework cannot translate method verifyhash
sql syntax. verifyhash
implemented in c# , not have corresponding method in sql.
why don't first search user username , verify password. can this:
var userfromdb = db.user.firstordefault(u => u.username_canonical == username); if(userfromdb !=null) { var uservalid = sha512.verifyhash(plainpassword, userfromdb.salt, userfromdb.password) if (uservalid) { // logic } }
Comments
Post a Comment