Here is the code inside login method:
await HttpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(identity),
new AuthenticationProperties
{
IsPersistent = true,
AllowRefresh = true,
ExpiresUtc = DateTimeOffset.UtcNow.AddHours(10),
});
return RedirectToAction("Index", "UserDashBoard");
2
Answers
There is nothing wrong with this part of the code you provided. I’m guessing the problem might be in the configuration of your Program.cs file.
When authentication and authorization middleware are in the wrong order, there may be situations where cookies cannot be set. Make sure your middleware is in the following order:
In some cases, if it is not configured to use HTTPS, the cookie will not be sent to the browser, which will also lead to the situation you describe.
Please check if you have the above two phenomena in your application, here is an official document and a working example, you can refer to it.
If you still can’t figure out the problem, please provide a minimal reproducible example.
This is how I create session