Cookie doesn't work

Z

zmj

I have written the following code in 2 web pages using C#

code for page 1:
//...
HttpCookie mycookie = new HttpCookie("Member");
mycookie.Values.Add("MemberID","0001");
Response.Cookies.Add(mycookie);
Response.Redirect("page2.aspx");
//...

code for page2:
//...
HttpCookie mycookie = Request.Cookies["Member"];
String MemberID = mycookie.Values["MemberID"];
//...

But when the browser goes to page2,it displays some error, and the value of
mycookie.Values["MemberID"] is nothing.
Please help me to solve this problem.
Thanks.
 
H

Hans Kesting

zmj said:
I have written the following code in 2 web pages using C#

code for page 1:
//...
HttpCookie mycookie = new HttpCookie("Member");
mycookie.Values.Add("MemberID","0001");
Response.Cookies.Add(mycookie);
Response.Redirect("page2.aspx");
//...

Redirect and cookies don't go together, I found.
I think that the redirect cancels all http-headers (which is
where the cookies are transferred) before setting the status to
"302 - object temporarily moved".
(another reason could be that the browser ignores everything
when it gets a status 302 - net result is the same: no cookie)

Something you could do:
don't use Response.Redirect, but send an html-page containing
<meta http-equiv=refresh content="0;URL=page2.aspx">
in the head-section (you can forget the body).
This will send a complete response to the client (including cookies)
and will redirect anyway to where you want to go.

Hans Kesting
 
J

John Saunders

Hans Kesting said:
Redirect and cookies don't go together, I found.

This turns out not to be the case. "Redirect and cookies" is exactly how
Forms Authentication works.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top