【问题标题】:How to delete cookie using IOwinContext in OwinMiddleware and .Net MVC 5?如何在 OwinMiddleware 和 .Net MVC 5 中使用 IOwinContext 删除 cookie?
【发布时间】:2019-04-10 18:30:10
【问题描述】:

是否有可能根据 OwinMiddleware 中的 cookie 名称删除 cookie?

我知道 IOwinContext 有自己的基于 Set-Cookie Header 的 cookie 抽象,而 HttpContext 有自己的 HttpCookieCollection。在我的中间件中,我无法访问 HttpContext,因此无法设置:Response.Cookies["userId"].Expires = DateTime.Now.AddDays(-1);

使用 IOwinContext 我尝试执行以下操作: Context.Response.Cookies.Delete("userId"); Context.Response.Cookies.Append("userId", ""); 但它不起作用。

有任何可行的示例如何实现我的目标? 非常感谢您的帮助和提示。

【问题讨论】:

    标签: c# asp.net-mvc cookies owin middleware


    【解决方案1】:

    Oki 那是我的错,因为我试图删除具有不同域的 cookie。

    这段代码解决了问题:

    Context.Response.Cookies.Delete("userId", new CookieOptions
                        {
                            Domain = "DomainName"
                        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-24
      • 2012-08-20
      • 2018-08-01
      • 2013-12-11
      • 1970-01-01
      相关资源
      最近更新 更多