asp.net - C# - Identity Framework and integer UserId - await SignInAsync issue -


i working on mvc5 demo app , attempting make of identity framework, in conjunction dapper within this.

i have implemented n-tier hybrid architecture many layers including dal handled dapper (with repository pattern). using standard microsoft.aspnet.identity both owin , core assemblies correctly referenced authentication - read on rolling own not advisable!

i digress, problem having following method within identity implementation...

passwordsigninasync() 

for example, within accountcontoller.cs have following:

 // post: /account/login     [httppost]     [allowanonymous]     [validateantiforgerytoken]     public async task<actionresult> login(loginviewmodel model, string returnurl)     {          // doesn't count login failures towards account lockout         // enable password failures trigger account lockout, change shouldlockout: true         var result = await signinmanager.passwordsigninasync(model.username, model.password, model.rememberme, shouldlockout: false);          int userid = int32.parse(user.identity.getuserid());          switch (result)         {             case signinstatus.success:                 viewbag.userid = userid;                 return redirecttoaction("index",  new routevaluedictionary(new { controller = "user", action = "index", id = userid }));                 //return redirecttolocal(returnurl);             case signinstatus.lockedout:                 return view("lockout");             case signinstatus.requiresverification:                 return redirecttoaction("sendcode", new { returnurl = returnurl, rememberme = model.rememberme });             case signinstatus.failure:                 return view(model);             default:                 modelstate.addmodelerror("", "invalid login attempt.");                 return view(model);         }     } 

basically, method logs users in , if signinstatus.success redirects usercontroller.cs index method. index method accepts integer id - aka userid variable of authenticated user.

my index(int id) routes /users/{id} or example /users/5

however, happens after submitting form is:

server error in '/' application.

value cannot null. parameter name: string description: unhandled exception occurred during execution of current web request. please review stack trace more information error , originated in code.

exception details: system.argumentnullexception: value cannot null. parameter name: string

source error:

line 77: var result = await signinmanager.passwordsigninasync(model.username, model.password, model.rememberme, shouldlockout: false); line 78: line 79:
int userid = int32.parse(user.identity.getuserid()); line 80:
line 81: switch (result)

source file: c:[projectname omitted]\controllers\accountcontroller.cs line: 79

stack trace:

[argumentnullexception: value cannot null. parameter name: string] system.number.stringtonumber(string str, numberstyles options, numberbuffer& number, numberformatinfo info, boolean parsedecimal) +12099581
system.number.parseint32(string s, numberstyles style, numberformatinfo info) +120
system.int32.parse(string s) +24 [projectname omitted security].controllers. d__4.movenext() in [projectname omitted]\controllers\accountcontroller.cs:79 system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) +99
system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) +58 system.runtime.compilerservices.taskawaiter.getresult() +26 system.threading.tasks.taskhelpersextensions.throwiffaulted(task task) +61 system.web.mvc.async.taskasyncactiondescriptor.endexecute(iasyncresult asyncresult) +114
system.web.mvc.async.<>c__displayclass37.b__36(iasyncresult asyncresult) +66
system.web.mvc.async.wrappedasyncresult1.callenddelegate(iasyncresult asyncresult) +47
system.web.mvc.async.wrappedasyncresultbase
1.end() +136
system.web.mvc.async.asyncresultwrapper.end(iasyncresult asyncresult, object tag) +102
system.web.mvc.async.asynccontrolleractioninvoker.endinvokeactionmethod(iasyncresult asyncresult) +49
system.web.mvc.async.asyncinvocationwithfilters.b__3d() +117 system.web.mvc.async.<>c__displayclass46.b__3f() +323 system.web.mvc.async.<>c__displayclass33.b__32(iasyncresult asyncresult) +44
system.web.mvc.async.wrappedasyncresult1.callenddelegate(iasyncresult asyncresult) +47
system.web.mvc.async.wrappedasyncresultbase
1.end() +136
system.web.mvc.async.asyncresultwrapper.end(iasyncresult asyncresult, object tag) +102
system.web.mvc.async.asynccontrolleractioninvoker.endinvokeactionmethodwithfilters(iasyncresult asyncresult) +50
system.web.mvc.async.<>c__displayclass2b.b__1c() +72 system.web.mvc.async.<>c__displayclass21.b__1e(iasyncresult asyncresult) +185
system.web.mvc.async.wrappedasyncresult1.callenddelegate(iasyncresult asyncresult) +42
system.web.mvc.async.wrappedasyncresultbase
1.end() +133
system.web.mvc.async.asyncresultwrapper.end(iasyncresult asyncresult, object tag) +56
system.web.mvc.async.asynccontrolleractioninvoker.endinvokeaction(iasyncresult asyncresult) +40
system.web.mvc.controller.b__1d(iasyncresult asyncresult, executecorestate innerstate) +34
system.web.mvc.async.wrappedasyncvoid1.callenddelegate(iasyncresult asyncresult) +70
system.web.mvc.async.wrappedasyncresultbase
1.end() +133
system.web.mvc.async.asyncresultwrapper.end(iasyncresult asyncresult, object tag) +56
system.web.mvc.async.asyncresultwrapper.end(iasyncresult asyncresult, object tag) +37
system.web.mvc.controller.endexecutecore(iasyncresult asyncresult) +44 system.web.mvc.controller.b__15(iasyncresult asyncresult, controller controller) +39
system.web.mvc.async.wrappedasyncvoid1.callenddelegate(iasyncresult asyncresult) +62
system.web.mvc.async.wrappedasyncresultbase
1.end() +133
system.web.mvc.async.asyncresultwrapper.end(iasyncresult asyncresult, object tag) +56
system.web.mvc.async.asyncresultwrapper.end(iasyncresult asyncresult, object tag) +37 system.web.mvc.controller.endexecute(iasyncresult asyncresult) +39
system.web.mvc.controller.system.web.mvc.async.iasynccontroller.endexecute(iasyncresult asyncresult) +39
system.web.mvc.mvchandler.b__5(iasyncresult asyncresult, processrequeststate innerstate) +39
system.web.mvc.async.wrappedasyncvoid1.callenddelegate(iasyncresult asyncresult) +70
system.web.mvc.async.wrappedasyncresultbase
1.end() +133
system.web.mvc.async.asyncresultwrapper.end(iasyncresult asyncresult, object tag) +56
system.web.mvc.async.asyncresultwrapper.end(iasyncresult asyncresult, object tag) +37
system.web.mvc.mvchandler.endprocessrequest(iasyncresult asyncresult) +40 system.web.mvc.mvchandler.system.web.ihttpasynchandler.endprocessrequest(iasyncresult result) +38
system.web.callhandlerexecutionstep.system.web.httpapplication.iexecutionstep.execute() +9723757 system.web.httpapplication.executestep(iexecutionstep step, boolean& completedsynchronously) +155

i not want index action within usercontroller accept nullable params (i.e. int? id) although making int nullable fix (by loading blank index page) - whole point profile belongs user , cannot generic...

    // usercontroller.cs  /user/index/5     // get: user's profile page calling dal profilerepository.cs!     public actionresult index(int id)     {         profile profile = _profilerepository.profileget(id);         return view(profile);     } 

strange thing is, if reload login page , resubmit login form manages assign integer value userid variable , route profile!

i don't know if await signinasync method surely should stop thread executing until method call complete?

or perhaps it's parsing of string int here: int userid = int32.parse(user.identity.getuserid());

but if signinasync has finished there should user.identity call getuserid() on right?

so strange therefore ctrl + f5 after error dump appearing cause correct profile page load - meaning integer value correctly assigned.

bah! i'm @ point considering scrapping identity framework - tough enough me decouple horrid ef , use own db schema!

if guys need see anymore of source please let me know

all best,

:-)

or should o.o !

you have issue in code. first identity's default user id value guid can't parse int. keep using string data type or change default data type int. second using user.identity.getuserid() after calling signinmanager.passwordsigninasync() method. don't work. @ last 1 new request must send server check signed in user info. why code works after refresh. third index action has security issue, access users data directly typing proper url in browser. therefore write this:

public async task<actionresult> login(loginviewmodel model, string returnurl) {     var result = await signinmanager.passwordsigninasync(model.username,          model.password, model.rememberme, shouldlockout: false);      switch (result)     {         case signinstatus.success:             viewbag.userid = userid;             return redirecttoaction("index","user");         case signinstatus.lockedout:             return view("lockout");         case signinstatus.requiresverification:             return redirecttoaction("sendcode", new { returnurl = returnurl, rememberme = model.rememberme });         case signinstatus.failure:             return view(model);         default:             modelstate.addmodelerror("", "invalid login attempt.");             return view(model);     } }  public actionresult index() {     string userid=user.identity.getuserid();     // use userid in way } 

but if want user id before calling signinmanager.passwordsigninasync() without redirecting consider using user manager user id:

var result = await signinmanager.passwordsigninasync(model.username,          model.password, model.rememberme, shouldlockout: false);  var userid =usermanager.findbyname(model.username).id; // instead of  // int userid = int32.parse(user.identity.getuserid()); 

Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -