I’m not sure what is happening here but sometimes I get an error message “Server cannot modify cookies after HTTP headers have been sent” when trying to set cookies. From what I can tell it is mostly search bots of some sort. Do bots have cookies disabled or something? I can’t reproduce it when I disable cookies. My code below is ran in the controller. Does it look correct?
var cookie = new HttpCookie(Config.ApiCookie)
{
HttpOnly = true,
Secure = false,
Value = authenticationResponse[SessionKey].ToString()
};
if (HttpContext.Current.Response.Cookies[Config.ApiCookie] != null)
{
HttpContext.Current.Response.Cookies.Set(cookie);
}
else
{
HttpContext.Current.Response.Cookies.Add(cookie);
}
The problem was with the .Set which is buggy. I used the code below instead and fixed the issue.