Pregunta

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);
    Response.Cookies.Add(pollCookies);
}


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?

¿Fue útil?

Solución

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!

Licenciado bajo: CC-BY-SA con atribución
scroll top