无法使用C#删除Cookie [英] Can't remove cookies using C#

查看:84
本文介绍了无法使用C#删除Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的浏览器中有两个Cookie,在登录时IDS4放在那里。我要把它们取下来。由于无法通过注销来删除它们(无论出于什么原因,despite the docs),我决定应用一种实际的变通办法,并手动删除它们。这似乎是一块硬邦邦的饼干。其中两个...

我试图像这样摆脱它们--瞄准所有可用的架构as suggested

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");

我试图通过显式命中proposed here来杀死他们。不过,很明显,饼干并不是这么碎的。

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

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

我看到了调用HttpContext.Current的建议,但这与在我的控制器中简单地调用HttpContext相同(根据this)。有talk about Session.Abandon,但在我的上下文中看不到该字段。这个特定的问题似乎有some issues,但我不知道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();
}

推荐答案

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

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

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

这篇关于无法使用C#删除Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆