【问题标题】:Remove and delete all cookies of my ASP NET c-sharp application删除和删除我的 ASP NET c-sharp 应用程序的所有 cookie
【发布时间】:2021-01-05 17:03:35
【问题描述】:

在通过消息电子邮件发送后,我需要删除和删除我的 ASP NET c-sharp 应用程序的所有 cookie。

我尝试了这个解决方案但没有成功,因为我有这个错误。

Server cannot modify cookies after HTTP headers have been sent.

在这一行:

HttpContext.Current.Response.Cookies.Add(expiredCookie);

消息电子邮件定期启动。

谷歌搜索对我没有帮助。

有人知道我该如何解决吗?

你能推荐一下吗?

你能帮帮我吗?

下面是我的代码。

提前谢谢你。

private void ExpireAllCookies()
{
    if (HttpContext.Current != null)
    {
        int cookieCount = HttpContext.Current.Request.Cookies.Count;
        for (var i = 0; i < cookieCount; i++)
        {
            var cookie = HttpContext.Current.Request.Cookies[i];
            if (cookie != null)
            {
                var cookieName = cookie.Name;
                var expiredCookie = new HttpCookie(cookieName) { Expires = DateTime.Now.AddDays(-1) };
                HttpContext.Current.Response.Cookies.Add(expiredCookie);
            }
        }

        HttpContext.Current.Request.Cookies.Clear();
    }
}



............

    {
        smtpClient.Send(mailMessagePlainText);
        ExpireAllCookies();
        Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('Ok.');window.location='http://...';", true);
    }
    catch (Exception ex)
    {
        throw (ex);
    }

【问题讨论】:

标签: c# asp.net cookies


【解决方案1】:

实际上,没有办法正确地做到这一点。

考虑以下代码:

foreach (string key in Request.Cookies.AllKeys)
{
   HttpCookie c = Request.Cookies[key];
   c.Expires = DateTime.Now.AddMonths(-1);
   Response.AppendCookie(c);
}

这将起作用,但前提是所有 cookie 都设置在根路径上,即 /。 如果 cookie 设置为虚拟目录,它将不起作用,因为 cookie 的路径不会与 cookie 一起发送。 cookie 只发送名称和值,没有其他内容,即没有路径。

所以如果你想用上面的方法删除服务器上的cookies,会无法删除所有的cookies,例如路径/Kamikatze/

所以更正确的变体是:

Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

    context.Response.ContentType = "text/plain"
    context.Response.Write("Die folgenden Cookies wurden gelöscht: ")

    If context.Session IsNot Nothing Then
        context.Session.Clear()
        context.Session.Abandon()
    End If

    For Each key As String In context.Request.Cookies.AllKeys
        context.Response.Write(key)
        context.Response.Write(System.Environment.NewLine)

        Dim c As HttpCookie = context.Request.Cookies(key)
        ' Here, the proc2-cookie is set on the VirtualPath ... '
        ' You need to handle all non-root cookies here, with if or switch or dictionary ' 
        If "proc2".Equals(key, StringComparison.InvariantCultureIgnoreCase) Then
            c.Path = System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath + "/"
        End If

        ' For Set-Cookie without domain attribute, 
        ' the cookie's domain value is "the origin server".
        ' treat an absent Domain attribute as if the Domain attribute 
        ' were present And contained the current host name
        ' c.Domain = context.Request.Url.Host
        c.Expires = System.DateTime.UtcNow.AddMonths(-1)
        context.Response.Cookies.Set(c)
    Next key

    ' clear cookies server side
    context.Request.Cookies.Clear()
End Sub

另见Is it possible to get a stored cookie's path?MDN Set-Cookie

【讨论】:

    【解决方案2】:

    你可以试试这样的

    if (Request.Cookies["id"] != null)
    {
        Response.Cookies["id"].Expires = DateTime.Now.AddDays(-1);   
    }
    

    或者类似的东西

    Session.Abandon();
    

    Abandon() 只会清除会话 cookie 而不会清除您手动设置的 cookie。如果您指定的 cookie 不存在,它将简单地返回 null。

    【讨论】:

    • 谢谢,根据您的建议,cookie 不会被删除
    猜你喜欢
    • 1970-01-01
    • 2020-12-27
    • 2011-01-18
    • 1970-01-01
    • 2017-05-09
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多