删除和删除我的ASP NET c-sharp应用程序的所有cookie

删除和删除我的ASP NET c-sharp应用程序的所有cookie

问题描述:

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

I need remove and delete all cookies of my ASP NET c-sharp application after sent on message email.

我已经尝试过解决方案没有成功,因为出现此错误。

I have tried this solution without success because I have this error.

Server cannot modify cookies after HTTP headers have been sent.

在此行:

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

消息电子邮件定期启动。

The message email started regularly.

谷歌搜索对我没有帮助。

The google search does not help me.

有人知道我该怎么解决吗?

Anybody know how can I resolve do this?

你能建议吗?

您能帮我吗?

我的下面的代码。

谢谢。

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


实际上,没有办法正确执行此操作。

Actually, there is no way to do this correctly.

请考虑以下代码:

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仅发送名称和值,而没有其他内容,也就是说,没有路径。

This will work, but only if all cookies are set on root path, aka /. If the cookie is set to a virtual directory, it won't work, because the cookie's path does NOT get sent along with the cookie. A cookie only sends the name and the value, and nothing else, that is to say, no path.

因此,如果要使用上述方法删除服务器上的Cookie,将无法删除所有具有路径 / Kamikatze /

So if you want to delete the cookies on the server with the above method, it will fail to delete all cookies that have, for example, path /Kamikatze/

的cookie,因此更正确的变体是:

So a more correct variant would be:

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

另请参见是否可以获取存储的cookie的路径?
MDN Set-Cookie