Different Session Time out .net core Session

2019-08-20 06:25发布

问题:

I am building an API using .net core 2 I want to change the session time out for different users. It will be like the is a default session time out(24 Hour) for every one (which I can do from service configuration). And if some one says 'keep me logged in' then the session time out will be a month or larger.

But .net core does not have Session.Current Session like the previous .net versions where I can change the session Time Out value.

-Thanks for your help

回答1:

For default cookie expiration you could setup in Startup.cs:

services.ConfigureApplicationCookie(options =>
{
    options.ExpireTimeSpan = TimeSpan.FromHours(24);
});

And for those who wants to stay logged in you could change cookie expiration date upon login:

var result = await this.signInManager.CheckPasswordSignInAsync(account, password, false);
if (result.Succeeded)
{
    await this.signInManager.SignInAsync(account, new AuthenticationProperties
    {
        ExpiresUtc = DateTime.UtcNow.AddDays(30),
        IsPersistent = true
    });
}