I have the login function and normal use on my Blazor web application. However, sometimes Cookie no longer logs me back in. This happens in an unpredictable way: I code a few lines of web interface code and my Cookies stop working. I copy the new commands, delete the local project, copy a new command from my git, and paste the commands I copied into the same location on the cloned project. And just like that, my Cookie was working normally again. However, recently the above method finally stopped working, even though I cloned from the master branch and not the branch I was working on Cookie still refused to work. What should I do?
public async Task<bool> LoginAsync(string username, string password, string unit)
{
try
{
var user = await FindByInfo(username, password, unit);
if (user != null)
{
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, user.Name),
new Claim(ClaimTypes.Role, user.Role),
new Claim("unit", user.Unit)
};
var claimsIdentity = new ClaimsIdentity(
claims, CookieAuthenticationDefaults.AuthenticationScheme);
var authProperties = new AuthenticationProperties
{
ExpiresUtc = DateTime.UtcNow.AddHours(1),
};
await _httpContextAccessor.HttpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(claimsIdentity),
authProperties);
return true;
}
return false;
}
catch(Exception ex)
{
return false;
}
}