I have written this utility class to save and retrieve HttpCookies.
It seems not to be working, i.e. Cookie is not being retrieved...
public class AspNetUtil
{
private Page _page = null;
public AspNetUtil(Page page)
{
_page = page;
}
public bool SaveInCookie(string cookieName, string valueKey, string valueToBeStored, int expiryTimeInMinutes)
{
bool success = false;
try
{
HttpCookie cookie = null;
if(_page.Request.Cookies[cookieName] == null)
{
cookie = new HttpCookie(cookieName);
}
else
{
cookie = _page.Request.Cookies[cookieName];
}
cookie.Values.Add(valueKey, valueToBeStored);
cookie.Expires = DateTime.Now.AddMinutes(expiryTimeInMinutes);
_page.Response.Cookies.Add(cookie);
}
catch(Exception ex)
{
success = false;
throw ex;
}
return success;
}
public string GetCookieValue(string cookieName, string valueKey)
{
string cookieValue = string.Empty;
try
{
cookieValue = (string)_page.Response.Cookies[cookieName].Values[valueKey];
}
catch (Exception ex)
{
cookieValue = string.Empty;
throw ex;
}
return cookieValue;
}
}
Can anyone tell me what can be the problem?