【发布时间】: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);
}
【问题讨论】:
-
对于你在这里得到的错误信息是一个很好的解释stackoverflow.com/questions/5507580/…