c# - ASP Identity 2.0: Regenerate Identity -



c# - ASP Identity 2.0: Regenerate Identity -

i having problem getting asp identity refresh identity stored in cookie on demand.

in startup.auth.cs file cookie set regenerate follows:

app.usecookieauthentication(new cookieauthenticationoptions { authenticationtype = defaultauthenticationtypes.applicationcookie, loginpath = new pathstring("/account/login"), provider = new cookieauthenticationprovider { onvalidateidentity = securitystampvalidator.onvalidateidentity<quizsparkusermanager, quizsparkuser, int>( validateinterval: timespan.fromminutes(30), regenerateidentitycallback: ((manager, user) => manager.createidentityasync(user, defaultauthenticationtypes.applicationcookie)), getuseridcallback: ((claimsidentity) => int.parse(claimsidentity.getuserid()))) } });

however cannot work out how refresh contents on user.identity in code, i.e. forcefulness refresh of identity cookie when need refresh.

i want able utilize regenerate identity callback programmatically, possible?

my problem similar 1 : how invalidate .aspnet.applicationcookie after adding user role using asp.net identity 2?

however want refresh rather invalidate cookie.

edit

after looking @ linked question attempted next (without total error handling):

iowincontext context = request.getowincontext(); quizsparksigninmanager manager = context.get<quizsparksigninmanager>(); claimsidentity newidentity = manager.createuseridentity(manager.usermanager.findbyid(user.identity.getuserid<int>())); authenticateresult authenticationcontext = await context.authentication.authenticateasync(defaultauthenticationtypes.applicationcookie); if (authenticationcontext != null) { context.authentication.authenticationresponsegrant = new authenticationresponsegrant( newidentity, authenticationcontext.properties); } bool first2 = user.isinrole("turtle");

edit2: user still not appear refresh. on page reload seem refresh, right in thinking because user.identity cookie part of request , cannot changed in code?

if trying add together new role logged-in user, need sign user out. create new identity new role , sign user in new identity. that's way update cookie.

best place check if user properties have changed in callback use: cookieauthenticationprovider.onvalidateidentity. this.

app.usecookieauthentication(new cookieauthenticationoptions { // other stuff provider = new cookieauthenticationprovider { // function executed every http request , executed in pipeline // , here have access cookie properties , other low-level stuff. // makes sense have invalidation here onvalidateidentity = async context => { // invalidate user cookie if user's security stamp have changed var invalidatebyseciritystamp = securitystampvalidator.onvalidateidentity<applicationusermanager, applicationuser>( validateinterval: timespan.fromminutes(30), regenerateidentity: (manager, user) => user.generateuseridentityasync(manager)); await invalidatebyseciritystamp.invoke(context); if (context.identity == null || !context.identity.isauthenticated) { return; } if(/*need update cookie*/) { // user manager. must registered owin var usermanager = context.owincontext.getusermanager<usermanager>(); var username = context.identity.name; // new user identity updated properties var updateduser = await usermanager.findbynameasync(username); // updated identity new info in user object var newidentity = updateduser.generateuseridentityasync(manager); // kill old cookie context.owincontext.authentication.signout(context.options.authenticationtype); // sign in 1 time again var authenticationproperties = new authenticationproperties() { ispersistent = context.properties.ispersistent }; context.owincontext.authentication.signin(authenticationproperties, newidentity); } } } });

disclaimer - never tested it, not tried compile it.

also can see other answer reference - pretty much same piece of code, different goal.

upd: regarding part of question - how observe role change: can think of way - have guid on user record. similar securitystamp, not used framework. phone call mysecuritystamp. on sign-in add together value of mysecuritystamp cookie claim. on every request compare value of mysecuritystamp in cookie value in database. if values different - time regenerate identity. , on every new role added/removed modify mysecuritystamp user in database. cover sessions in browsers.

c# asp.net cookies asp.net-identity-2

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -