May 12, 2005

ASP.Net Cookie Handling

Recently i came across with a problem with HtttpCookies. I try to save the client information in a cookie. But i couldnt, so i was just trying to check whether cookie is enable or not in the client browser. I simply used Request.Browser.Cookies property. But it didnt gave me the result that i wanted. Then i realized Request.Browser.Cookies is for checking the Browser capability of handling cookies. I coudn't find anything which tells me browser cookies are enable or disable . So what i did was just simply tried to create a cookie & tried to read the value from it. Below u will find the code for the two pages i used to solved the problem. If you guys have better solutions than this pls let me know.

//Page 1
//Page Load Event

if(!IsPostBack)
{
if(Request.QueryString["AcceptCookies"] == null)
{
Response.Cookies["TestCookie"].Value = "ok";
Response.Cookies["TestCookie"].Expires = DateTime.Now.AddMinutes(1);
Response.Redirect("My2.aspx?redirect=" + Server.UrlEncode(Request.Url.ToString()));
}
else
{
//Can Redirect to the page You want
Response.Write(Request.QueryString["AcceptCookies"].ToString());
}
}


//Page2
//Page Load Event

string redirect = Request.QueryString["redirect"];
string acceptCookies = "";
if(Request.Cookies["TestCookie"] == null)
{
//No Cookies
acceptCookies = "0";
}
else
{
//Yes Cookies
acceptCookies = "1";
//Delete the Test Cookies
Response.Cookies["TestCookie"].Expires = DateTime.Now.AddMinutes(1);
}
Response.Redirect(redirect + "?AcceptCookies=" + acceptCookies );

1 comment:

Madhawa said...

yo brother!
If money is not a problem u can try this.

http://www.cyscape.com/docs/browserhawk/sessioncookies_property_net_.htm

happy coding!!!