0

This is how I am setting up the cookies data and passing along web request. When I check the response from the website using fiddler I see cookies have no data and expiry data is set to Jan 1, 1900. Any help would be much appreciated. Please let me know if something is not clear from following code or you need more info to answer my question. Thanks.

System.Net.Cookie userType = 
    new System.Net.Cookie("CUserType","subscriber", "/", "www.DOMAIN_NAME.com");
userType.Expires = DateTime.Now.AddYears(1);
System.Net.Cookie dUserType = 
    new System.Net.Cookie("dCUserType", "subscriber", "/", "www.DOMAIN_NAME.com");
dUserType.Expires = DateTime.Now.AddYears(1);

System.Net.CookieContainer cookieContainer = new System.Net.CookieContainer();
cookieContainer.Add(userType);    
cookieContainer.Add(dUserType);

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(URL);

webRequest.Proxy = new WebProxy("127.0.0.1", 8888);   
webRequest.Referer = "http://DOMAIN_NAME/search/index.aspx?lid=3";  
webRequest.ContentType = "application/x-www-form-urlencoded";   
webRequest.Date = DateTime.Now;    
webRequest.CookieContainer = cookieContainer;

string result;

using (var stream = webRequest.GetResponse().GetResponseStream())
using (var reader = new StreamReader(stream, Encoding.UTF8))    
{
    result = reader.ReadToEnd();    
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Mandeep Janjua
  • 15,583
  • 4
  • 29
  • 24
  • 1
    I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Dec 25 '12 at 20:01

1 Answers1

0

When you set cookies, you also need to set the domain because the cookies are different for www.domain.com and just domain.com.

To make them the same, you need to set the domain on your cookie. First on the web.config

<authentication mode="Forms">
 <forms domain="yoururl.com" ... />
</authentication> 

and second when you set manually your cookies (with out the www.):

Response.Cookies[cookieName].Domain = "yoururl.com";

or as you set it up (with out the subdomain www.):

System.Net.Cookie dUserType = 
    new System.Net.Cookie("dCUserType", "subscriber", "/", "DOMAIN_NAME.com");

Similar questions and answers :
Multiple applications using same login database logging each other out
asp.net forms authentication logged out when logged into another instance
Lost session/cookie when login as another user

Community
  • 1
  • 1
Aristos
  • 66,005
  • 16
  • 114
  • 150
  • Thanks. I am not sure if you missed this piece of code but I am setting up the domain for cookie - System.Net.Cookie("dCUserType", "subscriber", "/", "www.DOMAIN_NAME.com") – Mandeep Janjua Dec 25 '12 at 20:22
  • @MandeepJanjua Set it up with out the `www.` !!! and both on web.config, because if you call it from www.domain.com and from domain.com on separate calls then the cookie can't be read. – Aristos Dec 25 '12 at 20:30