c# - ASP.NET MVC Identity 2.2.1 how to figure out why persistent authorization doesn't work -


i configured authentication in mvc template authorization. want system never log out users once logged in. (persistent authorization) after each application restart users need log in again. (my app not optimized can application restarted frequent opening lot of tabs main page) how can approach issue?

code: config.cs

public partial class startup {     public void configuration(iappbuilder app)     {         configureauth(app);     } } 

app_start/config.auth.cs

 public void configureauth(iappbuilder app)     {         app.createperowincontext(applicationdbcontext.create);         app.createperowincontext<applicationusermanager>(applicationusermanager.create);         app.createperowincontext<applicationsigninmanager>(applicationsigninmanager.create);          app.usecookieauthentication(new cookieauthenticationoptions         {             authenticationtype = defaultauthenticationtypes.applicationcookie,             loginpath = new pathstring("/account/login"),             provider = new cookieauthenticationprovider             {                  onvalidateidentity = securitystampvalidator.onvalidateidentity<applicationusermanager, applicationuser>(                     validateinterval: timespan.fromminutes(30),                     regenerateidentity: (manager, user) => user.generateuseridentityasync(manager))             }         });      } 

app_start/identityconfig.cs

 public class applicationusermanager : usermanager<applicationuser> {     public applicationusermanager(iuserstore<applicationuser> store)         : base(store)     {     }      public static applicationusermanager create(identityfactoryoptions<applicationusermanager> options, iowincontext context)     {         var manager = new applicationusermanager(new userstore<applicationuser>(context.get<applicationdbcontext>()));         manager.uservalidator = new uservalidator<applicationuser>(manager)         {             allowonlyalphanumericusernames = false,             requireuniqueemail = false         };         manager.passwordvalidator = new passwordvalidator         {             requiredlength = 6,         };         return manager;     } } public class applicationsigninmanager : signinmanager<applicationuser, string> {     public applicationsigninmanager(applicationusermanager usermanager, iauthenticationmanager authenticationmanager)         : base(usermanager, authenticationmanager)     {     }      public override task<claimsidentity> createuseridentityasync(applicationuser user)     {         return user.generateuseridentityasync((applicationusermanager)usermanager);     }      public static applicationsigninmanager create(identityfactoryoptions<applicationsigninmanager> options, iowincontext context)     {         return new applicationsigninmanager(context.getusermanager<applicationusermanager>(), context.authentication);     } } 

models/identitymodels.cs

public class applicationuser : identityuser {     public int authorid { get; set; }     public async task<claimsidentity> generateuseridentityasync(usermanager<applicationuser> manager)     {         var useridentity = await manager.createidentityasync(this, defaultauthenticationtypes.applicationcookie);         return useridentity;     } }  public class applicationdbcontext : identitydbcontext<applicationuser> {     public applicationdbcontext()         : base("name=mydbcontext")     {     }     public static applicationdbcontext create()     {         return new applicationdbcontext();     } } 

and finally, login method of accountcontroller.cs

[httppost] [allowanonymous] public async task<actionresult> login(loginviewmodel model, string returnurl) {     if (modelstate.isvalid)     {         var user = await usermanager.findasync(model.username, model.password);         if (user != null)         {             await signinmanager.passwordsigninasync(user.username, model.password, model.rememberme, shouldlockout: false);             return redirecttolocal(returnurl);         }     } } 


Comments