Custom Implementation ASP.NET Identity -
i having little bit of problem implementing asp.net identity using repository pattern. when call usermanager.findasync(model.username, model.password)
return nothing
please see account controller :
' post: /account/login <httppost> <allowanonymous> <validateantiforgerytoken> public async function login(model loginviewmodel, returnurl string) task(of actionresult) if modelstate.isvalid ' validate password if usermanager nothing usermanager = new usermanager(of applicationuser, guid)(new we.security.userstore(model.domainname)) end if model.password = usermanager.passwordhasher.hashpassword(model.password) dim appuser = await usermanager.findasync(model.username, model.password) if appuser isnot nothing await signinasync(appuser, model.rememberme) return redirecttolocal(returnurl) else modelstate.addmodelerror("", "invalid username or password.") end if end if ' if got far, failed, redisplay form return view(model) end function private function authenticationmanager() iauthenticationmanager return httpcontext.getowincontext().authentication end function private async function signinasync(user applicationuser, ispersistent boolean) task authenticationmanager.signout(microsoft.aspnet.identity.defaultauthenticationtypes.externalcookie) dim identity = await usermanager.createidentityasync(user, microsoft.aspnet.identity.defaultauthenticationtypes.applicationcookie) authenticationmanager.signin(new authenticationproperties() {.ispersistent = ispersistent}, identity) end function private function redirecttolocal(returnurl string) actionresult if url.islocalurl(returnurl) return redirect(returnurl) else return redirecttoaction("index", "home") end if end function
please see store code:
public class userstore implements iuserstore(of applicationuser, guid) implements iuserpasswordstore(of applicationuser, guid) private repo genericrepository private _companyname string public sub new(companyname string) _companyname = companyname repo = new genericrepository(companyname) end sub private readonly property companyname string return _companyname end end property public function createasync(user applicationuser) task implements iuserstore(of applicationuser, guid).createasync throw new notimplementedexception end function public function deleteasync(user applicationuser) task implements iuserstore(of applicationuser, guid).deleteasync throw new notimplementedexception end function public function findbyidasync(userid guid) task(of applicationuser) implements iuserstore(of applicationuser, guid).findbyidasync dim user user = repo.getsingleordefault(of user)(userid) dim appuser applicationuser = nothing if user isnot nothing appuser = new applicationuser(user.id) {.username = user.username, .passwordhash = user.passwordhash} end if return task(of applicationuser).fromresult(appuser) end function public function findbynameasync(username string) task(of applicationuser) implements iuserstore(of applicationuser, guid).findbynameasync dim user user = repo.getsingleordefault(of user)(new core.specification.specification(of user)(function(x) x.username = username)) dim appuser applicationuser = nothing if user isnot nothing appuser = new applicationuser(user.id) {.username = user.username, .passwordhash = user.passwordhash} end if return task(of applicationuser).fromresult(appuser) end function public function updateasync(user applicationuser) task implements iuserstore(of applicationuser, guid).updateasync throw new notimplementedexception end function public function getpasswordhashasync(user applicationuser) task(of string) implements iuserpasswordstore(of applicationuser, guid).getpasswordhashasync dim paswordhash = repo.getdetail(of user)(user.id).passwordhash return task(of string).fromresult(paswordhash) end function public function haspasswordasync(user applicationuser) task(of boolean) implements iuserpasswordstore(of applicationuser, guid).haspasswordasync dim paswordhash = repo.getdetail(of user)(user.id).passwordhash dim haspassword boolean = not string.isnullorempty(paswordhash) return task(of boolean).fromresult(haspassword) end function public function setpasswordhashasync(user applicationuser, passwordhash string) task implements iuserpasswordstore(of applicationuser, guid).setpasswordhashasync user.passwordhash = passwordhash return task.fromresult(of object)(nothing) end function end class
i found issue, during login post method doing model.password = usermanager.passwordhasher.hashpassword(model.password) removed line , work fine.
Comments
Post a Comment