-1

I am working on an ASP.NET MVC 4 application. I have created few cookies and did not set any expiration time on it. When I am doing a RedirectToAction, all the cookies are getting deleted. I am not sure what I am missing here. Following is the code I wrote to create and access cookies:

Creating cookies:

HttpCookie authorizedCookie = new HttpCookie(AuthCookieName);
authorizedCookie.Value = authorized.ToString();
Response.SetCookie(authorizedCookie);

Accessing Cookies:

authorized = Request.Cookies[AuthCookieName] != null ? System.Convert.ToBoolean(Request.Cookies[AuthCookieName].Value) : false;

When I am trying to access the cookies, the cookie collection is always empty.

Update: I have also tried setting the domain, expiration time, httponly and yet nothing seems to work. When I look at fiddler, the cookies just seems to be deleted immediately after the redirect.

tereško
  • 58,060
  • 25
  • 98
  • 150
  • The code and information provided are not enough to answer this. What URL do you use to login, and what URL do you redirect to? Also, you cannot redirect and set cookies in one go, are you doing that? – CodeCaster Jul 11 '13 at 14:08
  • @CodeCaster The URL I am using is "us-srv-web01d/appname". As suggested in other posts I have tried removing the hyphens, but still the cookies aren't getting created. To mention to that this issue is only happening in IE. The cookies aren't created at all in IE. Other browsers work absolutely fine. – Badarinath Pennati Jul 11 '13 at 15:53
  • I just tested the application on a couple of other machines at work running the same version of IE that I am using and the application just works fine. As of now, this issue seems to be specific to my machine. All cookies are enabled on my machine. So, I am yet to determine what the actual cause(or setting) is. – Badarinath Pennati Jul 11 '13 at 16:30

2 Answers2

1

Response.SetCookie() only updates existing cookies. Use Response.Cookies.Add().

JuhaKangas
  • 875
  • 5
  • 17
  • that doesn't seem to do the trick either. I was able to partially resolve the issue by creating the cookies after redirect. It works like a charm on all the browsers. But IE fails to create the cookies. My url looks like "htttp://us-srv-web01d/appname". According to other posts I read, IE doesn't like some characters in the domain name. So I removed the hyphens in the domain name, but that doesn't work either. – Badarinath Pennati Jul 11 '13 at 15:49
  • In regards to your comments further up, maybe try seeing if there anything in here to help you: http://stackoverflow.com/questions/7384502/ie9-loses-cookies-after-redirect – JuhaKangas Jul 12 '13 at 09:29
0

I am able to create the cookies now. The issue seems to be with my IE settings. I reset the browser to its initial state and the cookies are working fine.

I am still not sure which setting was causing the issue. Because of this I am still not convinced to use the cookie approach. Currently my website is heavily dependent on cookies and any issues with the user's browser will render my website useless. I am planning to replace the cookie approach.

Thanks for all your responses.