简体   繁体   中英

How to persist the login information after closing Browser using asp.net Identity?

When using the ASP.NET Identity, I want to persist the login information as long as possible when the user logs in to my website, so the user doesn't need to login again when they reopened their Browser (just like github.com and stackoverflow.com). When I login to github, it persists my information for many days, so I don't need to login again every day. Are there any methods can that can implement this functionality using ASP.NET Identity?

Just pass the appropriate value to the isPersistent argument of the SignIn methods:

SignInManager.PasswordSignInAsync("email@id.com", "password", isPersistent: true, shouldLockout: false);

or

SignInManager.SignInAsync(applicationUser, isPersistent: true, rememberBrowser: false);

The isPersistent argument is used to control if the user's authentication cookie should be persisted.

The rememberBrowser argument is used in case of Two Factor Authentication: a remembered browser can login directly with the password alone.

You need to set IsPersistent property on AuthenticationProperties to persist login after browser close. The registered method user.GenerateUserIdentityAsync generates the new ClaimsIdentity to be refreshed in the cookie.

private async Task<SignInStatus> SignIn(User user, bool isPersistent)
{
    await SignInAsync(user, isPersistent);
    return SignInStatus.Success;
}

public async Task SignInAsync(User user, bool isPersistent)
{
    var userIdentity = await user.GenerateUserIdentityAsync(UserManager);
    AuthenticationManager.SignIn(
       new AuthenticationProperties
        {
           IsPersistent = isPersistent
        },
        userIdentity
    );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM