The login code seems to work because PasswordSignInAsync return Succeed, but when I'm getting the user information on the next request by using User.GetUserName(), it always return me null. And also User.IsSignedIn() is also returning false.
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
{
ViewData["ReturnUrl"] = returnUrl;
if (ModelState.IsValid)
{
var result = await _signInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, lockoutOnFailure: false);
if (result.Succeeded)
{
// this code executed and the redirection works fine
Logger.LogInformation(1, "User logged in.");
return RedirectToLocal(returnUrl);
}
if (result.RequiresTwoFactor)
{
Logger.LogWarning(2, "RequiresTwoFactor");
}
if (result.IsLockedOut)
{
Logger.LogWarning(3, "User account locked out.");
}
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
return View(model);
}
return View(model);
}
In the next request, I could not get any information back.
Logger.LogWarning(User.Identity.Name ?? "User.Identity.Name is null"); // null
Logger.LogWarning(User.GetUserName() ?? "User.GetUserName() is null"); // null
Logger.LogWarning(User.IsSignedIn() ? "User is signed in" : "User is not signed in"); // not signed in
My Startup.cs
app.UseIdentity();
services.AddIdentity<CustomAccount, CustomRole>(options =>
{
options.Cookies.ApplicationCookie.ExpireTimeSpan = TimeSpan.FromHours(24);
})
.AddEntityFrameworkStores<ApplicationDbContext, long>()
.AddDefaultTokenProviders();
Note: I'm also using app.UseJwtBearerAuthentication in the same application, could it be an issue?