ASP.NET Identity 2 Remember Me - User Is Being Logged Out

后端 未结 5 805
南旧
南旧 2021-02-08 12:40

I am using Identity 2.1 in my MVC5 app. I am setting the isPersistent property of the PasswordSignInAsync to true to enable \'Remember Me\':

var result = await S         


        
相关标签:
5条回答
  • 2021-02-08 13:18

    Form this post, the isPersistent parameter sets whether the authentication session is persisted across multiple requests.

    0 讨论(0)
  • 2021-02-08 13:24

    I should write more. This strange code:

    OnValidateIdentity = SecurityStampValidator
      .OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
      validateInterval: TimeSpan.FromMinutes(0),
      regenerateIdentity: (manager, user)
      => user.GenerateUserIdentityAsync(manager))
    

    was causing my app to lost cookie after 1 day. I really don`t know why, but after excluding this code and adding a mashine key to my web.config "remember me" future is finally working right.

    My current code is:

    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
       AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
       LoginPath = new PathString("/Account/Login"),
       ExpireTimeSpan = TimeSpan.FromDays(5)
    });
    
    0 讨论(0)
  • 2021-02-08 13:25

    I had this issue. It was caused by my custom UserStore not implementing IUserSecurityStampStore.

    public Task<string> GetSecurityStampAsync(IdentityUser user)
    {
        return Task.FromResult<string>(user.SecurityStamp);
    }
    

    Without a security stamp the SecurityStampValidator has nothing to validate and so logs out the user.

    0 讨论(0)
  • 2021-02-08 13:27

    I think you should read this article . There are two different intervals: ValidateInterval and ExpireTimeSpan. And in your case i think you should change the expireTimeSpan, not the ValidateInterval.

    0 讨论(0)
  • 2021-02-08 13:30

    There is an explanation for TimeSpan parameter in similar question. Simply use the infinite cookies, like this:

    OnValidateIdentity = SecurityStampValidator
      .OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
      validateInterval: TimeSpan.FromMinutes(0),
      regenerateIdentity: (manager, user)
      => user.GenerateUserIdentityAsync(manager))
    

    This is also needed for it to work correctly:

    Call

    await UserManager.UpdateSecurityStampAsync(userId);
    

    before

    AuthenticationManager.SignOut(); 
    
    0 讨论(0)
提交回复
热议问题