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

Developer technologies | ASP.NET | ASP.NET Core
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 82,221 Reputation points Volunteer Moderator
    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.


Your answer

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