How to save login state when close browser?

mc 4,456 Reputation points
2023-02-04T00:18:52.4966667+00:00

when signed HttpContext.SignInAsync(claimsPrinciple);

when I close the browser the cookie is not valid and I need login again.

builder.Services.AddAuthentication("Cookie").AddCookie("Cookie", options =>
{
    options.Cookie.Name = "j.Cookie";
    options.LoginPath = "/Login";
    options.ExpireTimeSpan = TimeSpan.FromDays(7);
});
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,532 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,466 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 28,131 Reputation points
    2023-02-04T00:58:16.5966667+00:00

    You are using the wrong SignInAsync() overload. Use the SignInAsync() method overload that lets you set the authentication properties. Please see the reference documentation and select the options that best fit your needs.

    Use cookie authentication without ASP.NET Core Identity

    var authProperties = new AuthenticationProperties
    {
        ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(10),
        IsPersistent = true
    };
    
    await HttpContext.SignInAsync( 
        new ClaimsPrincipal(claimsIdentity), 
        authProperties);
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.