I have farm solution Visual web part, with the following write and read cookies methods

void writeCookie(Guid thisVoteGuid, string pollID)
    HttpCookie pollCookies = new HttpCookie(COOKIE_NAME);
    pollCookies.Value = thisVoteGuid.ToString() + ";" + pollID;
    pollCookies.Expires = DateTime.Now.AddDays(30);

string readCookie()
    //Get Cookie value
    HttpCookie theCookie = Request.Cookies[COOKIE_NAME];
    if (theCookie != null)
       return theCookie.Value;
     return string.Empty;

The read function fails to get the cookie (of-course, since the write method didnt store the cookie at all!), I checked Temporary Internet Files to confirm that no cookie stored

How to read/write cookies in visual webPart-Farm solution?

1 Answer 1


The code you use looks ok... Does your browser allow you to save cookies? (Since cookies are stored client-side) If you need to be sure that every user uses this, you'll need a server-side solution like viewstate, session, database,... since not every user allows cookies!

  • 1
    After two-three tries it worked, I don't know why, but what Im sure about that I didnt change any of the IE options!, anyway I'll accept your post as an answer, Thank you. Commented Mar 25, 2012 at 6:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.