问题可能是由于在配置身份验证服务时未正确设置Cookie属性而导致。以下是正确设置Cookie属性的示例代码:
services.ConfigureApplicationCookie(options =>
{
options.Cookie.HttpOnly = true;
options.ExpireTimeSpan = TimeSpan.FromMinutes(60);
options.SlidingExpiration = true;
});
如果仍然遇到问题,则可以尝试使用以下代码实现SignOutAsync:
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
await HttpContext.SignOutAsync("Identity.Application");
这将同时注销Cookie和Identity认证令牌。
另外,检查是否在登录时将RememberMe设置为true。如果是,则需要调用HttpContext.SignOutAsync()。否则,将不会注销用户。
if (signInResult.Succeeded)
{
// Make sure the user's credentials exist on the cache.
HttpContext.Session.SetString("Username", model.Username);
HttpContext.Session.SetString("Password", model.Password);
if (model.RememberMe)
{
// When the user checks out the remember me option. The cookie will be valid for 7 days.
var authProperties = new AuthenticationProperties
{
IsPersistent = true,
ExpiresUtc = DateTimeOffset.UtcNow.AddDays(7)
};
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, userPrincipal, authProperties);
}
else
{
// When the user does not check the remember me option. The cookie will be valid for the current session.
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, userPrincipal);
}
return LocalRedirect(returnUrl);
}
希望这能帮助到你解决Asp.net core 5 Identity SignOutAsync不起作用的问题。