Login page with cookie valid

Salvatore Rizzo 21 Reputation points
2023-01-16T20:53:16.25+00:00

I have a small aspnet core mvc .net 5 project with a simple authentication system.
At the login post action I create a cookie like this:

//Generate the user's cookie!
var authProperties = new AuthenticationProperties { IsPersistent = true, ExpiresUtc = DateTimeOffset.Now.AddHours(12) };
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity), authProperties);

Why, despite seeing the valid cookie in the browser, do I immediately get a redirect to the login page?

cookie

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,400 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 61,731 Reputation points
    2023-01-16T21:35:31.66+00:00

    The point of HttpContext.SignInAsync is to set a cookie and redirect to the url specified in properties. Usually a return url is passed to the login page, and the RedirectUri is set to this in the signin code. You appear to not be doing this, so it defaults to the current page.

    0 comments No comments

  2. Salvatore Rizzo 21 Reputation points
    2023-01-16T21:45:17.7233333+00:00

    Thanks for the reply but the problem is not the landing page after the post, but the redirect to the login that occurs after a few minutes of browsing the site.