ASP.NET Server cannot modify cookies after HTTP headers have been sent

1

We have a ASP.NET MVC site with some legacy webforms pages. Recently some hits on these pages ends with the following error in log:

System.Web.HttpException (0x80004005): Server cannot modify cookies after HTTP headers have been sent.
at System.Web.HttpCookieCollection.Add(HttpCookie cookie)
at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Apparently the stack trace does not contain any user code.

We have failed to reproduce the error. The error happens on GETs and POSTs and different user agents.

Response.Buffer and Response.BufferOutput is set to true.

asp.net
cookies
webforms
asked on Stack Overflow Dec 1, 2014 by v0id • edited Dec 1, 2014 by v0id

1 Answer

0

The error you are getting is quite self explanatory. Your code tries to add/modify cookies after the response is set.

You need to walk-through your code and checkthe sections that write cookies.

Even if your code looks ok at the first glance, make sure that you don't have async content loading up before your cookies are written.

Your post suggest that this errors occur randomly so most likely it's a issue related to async. It's a race situation: sometimes the async completes before cookie creation but other times it doesn't, hence the random error.


User contributions licensed under CC BY-SA 3.0