c# - ApplicationUserContext is null in Entity Framework -


i came accross out of world today. basically, i'm using default applicationusercontext included in entityframework, , somehow, when add role user, subject needs logout roles updated. in fact, normal because roles stored in cookie in infos loaded every 30 minutes, or everytime user signs in.

so in case, trying add specific role user, using role manager , forcing "resignin", aka logout login.

_usermanager.addtorole(userid, "the role of world"); applicationuser theuser = _usermanager.findbyid(user.identity.getuserid());  if (returnurl != null)  {     accountcontroller ac = new accountcontroller();     await ac.relogin(theuser);     return redirect(returnurl);  } 

now see, created new instance of accountcontroller since in other controller , called method "relogin(user)"

    public async task relogin(applicationuser _user)     {         await signinasync(_user, false);     }     private async task signinasync(applicationuser user, bool ispersistent)     {         authenticationmanager.signout(defaultauthenticationtypes.externalcookie);         authenticationmanager.signin(new authenticationproperties() { ispersistent = ispersistent }, await user.generateuseridentityasync(usermanager));     } 

now when run code, error "object reference not set instance of object. on code :

    private iauthenticationmanager authenticationmanager     {                 {             return httpcontext.getowincontext().authentication;         }     } 

which mean httpcontext null... tried getting httpcontext in controller , sending in parameter this. not null in controller went in accountcontroller, usermanager became null... happening?

public async task relogin(applicationusermanager _usermanager) 

when use async/await, work off loaded thread. reason why httpcontext null, because you're no longer on request thread anymore , current httpcontext instance not reachable (thread static).

to confirm, remove async/await , try.

as quick work around, can pass reference authenticationmanager parameter argument.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -