473,394 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Cookie doesn't work

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.
Nov 18 '05 #1
3 992
Your code works perfectly for me. What is the error you are talking about?

zmj wrote:
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.

Nov 18 '05 #2

"zmj" <zh*************@sina.com> wrote in message news:Oj**************@TK2MSFTNGP09.phx.gbl...
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
Nov 18 '05 #3
"Hans Kesting" <ne***********@spamgourmet.com> wrote in message
news:OL**************@tk2msftngp13.phx.gbl...

"zmj" <zh*************@sina.com> wrote in message

news:Oj**************@TK2MSFTNGP09.phx.gbl...
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.


This turns out not to be the case. "Redirect and cookies" is exactly how
Forms Authentication works.
--
John Saunders
johnwsaundersiii at hotmail
Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Joe | last post by:
I want to be able to detect if I can write cookies out or not. Currently I'm doing: my $cgi = new CGI; my $cookie = $cgi->cookie(-name => $POLL_CODE_TEST, -value => "cookietest",...
3
by: Wysiwyg | last post by:
After a server created cookie is processed on the client I want it removed, cleared, or expired in the javascript block but have been unable to do this. If I set a cookie value in the server code...
1
by: BJörn Lindqvist | last post by:
Hello, I have some very serious trouble getting cookes to work. After a lot of work (urllib2 is severly underdocumented, arcane and overengineerd btw) I'm finally able to accept cookes from a...
29
by: Jerim79 | last post by:
I did try to find the answer to this before posting, so this isn't a knee jerk reaction. What I am trying to accomplish is to have a script that opens a cookie, reads a value, and then use a...
7
by: =?Utf-8?B?SlA=?= | last post by:
I need to design a WS that will after authenicating the user, create a cookie on the users PC that made the request. All the code I keep finding is how to get a WS to read a cookie, I need it to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.