首页 > 解决方案 > 无法使用 C# 删除 cookie

问题描述

我的浏览器中有两个 cookie 在登录期间由 IDS4 放置在那里。我需要删除它们。未能通过退出来摆脱它们(无论出于何种原因,尽管有文档),我决定应用一个实用的解决方法并手动删除它们。看来那些是硬饼干。他们两个人...

在此处输入图像描述

我试图像这样摆脱它们 -以建议的所有可用模式为目标。

await HttpContext.SignOutAsync("Identity.Application");
await HttpContext.SignOutAsync("Identity.External");
await HttpContext.SignOutAsync("Identity.TwoFactorRememberMe");
await HttpContext.SignOutAsync("Identity.TwoFactorUserId");
await HttpContext.SignOutAsync("idsrv");
await HttpContext.SignOutAsync("idsrv.external");

我试图按照这里的建议通过明确的打击来杀死他们。不过,显然这不是饼干碎的方式。

Response.Cookies.Delete(".AspNetCore.Identity.Application");
Response.Cookies.Delete("idsrv.session");

没有什么能抹去他们。当然,当我重新启动浏览器时,它们确实会消失,但我需要它们在没有这种措施的情况下消失(另外,如果我要重新启动浏览器,我不需要注销用户,因为它们无论如何都会消失)。

我已经看到了调用的建议,HttpContext.Current但这与我的控制器中的调用相同HttpContext(根据this)。有人谈论,Session.Abandon但在我的上下文中我没有看到那个领域。这个具体问题似乎存在一些问题,但我不知道 IDS4 团队是否仍未解决这些问题。

编辑

public async Task<IActionResult> LogOut([FromQuery] string logoutId)
{
  LogoutRequest context = await InteractionService.GetLogoutContextAsync(logoutId);

  bool? isLoggedIn = User?.Identity.IsAuthenticated;
  isLoggedIn |= User.IsAuthenticated();

  await HttpContext.SignOutAsync();
  await HttpContext.SignOutAsync(IdentityConstants.ApplicationScheme);
  await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);

  //Response.Cookies.Delete("idsrv.session");

  var output = new
  {
    authenticated = isLoggedIn,
    clientId = context.ClientId,
    sessionId = context.SessionId,
    redirect = context.PostLogoutRedirectUri,
    sub = context.SubjectId
  };

  return Ok(output);
  // return SignOut();
}

标签: c#cookiesidentityserver4asp.net-core-3.1webapi

解决方案


执行注销时,不应从操作方法返回任何内容,因为 SignOutAsync 会生成自己的“响应”。我就是这样做的:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task Logout()
{
    await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
    await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme);

    //Important, this method should never return anything.
}

推荐阅读