473,406 Members | 2,707 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,406 software developers and data experts.

Cookies ... monster. Please help

Hi everyone,
Can someone tell me what's wrong with the way that i read a cookie as below:

private void Page_Load(object sender, System.EventArgs e)
{
Response.Cookies.Clear();
HttpCookie cookie = GetSessionCookie("MyCookie", "duh");
}

private HttpCookie GetSessionCookie(string cookieKey, string cookieValue)
{
HttpCookie cookie = Response.Cookies[cookieKey];
if (cookie == null)
{
cookie = this.CreateNewSessionCookie(cookieKey, cookieValue);
}
return cookie;
}

private HttpCookie CreateNewSessionCookie(string cookieKey, string
cookieValue)
{
HttpCookie cookie = new HttpCookie(cookieKey, cookieValue);
Request.Cookies.Add(cookie);
return cookie;
}

The problem is in GetSessionCookie(), after the line
HttpCookie cookie = Response.Cookies[cookieKey];
A new cookie with the specified key is added to the Request.Cookies
collection and the cookie object contains key "MyCookie" with empty value. As
a result, it never reaches the CreateNewSessionCookie() function call. What's
going on, please help me please, anyone?
My Web.Config file contains:
<authentication mode="Forms">
<forms name="MySettings" loginUrl="login.aspx" protection="All"
timeout="20" path="/"></forms>
</authentication>

Many thanks in advance.
KD
Nov 19 '05 #1
3 2068
For now I would say rather that you mixed up Response and Request. Try to
add the cookie to Response rather than Request...

Also you could use a property to expose the value stored by the cookie
wihtout having your page(s) knowing from where it comes...

Patrice

--

"Calvin KD" <Ca******@discussions.microsoft.com> a écrit dans le message de
news:E8**********************************@microsof t.com...
Hi everyone,
Can someone tell me what's wrong with the way that i read a cookie as below:
private void Page_Load(object sender, System.EventArgs e)
{
Response.Cookies.Clear();
HttpCookie cookie = GetSessionCookie("MyCookie", "duh");
}

private HttpCookie GetSessionCookie(string cookieKey, string cookieValue)
{
HttpCookie cookie = Response.Cookies[cookieKey];
if (cookie == null)
{
cookie = this.CreateNewSessionCookie(cookieKey, cookieValue);
}
return cookie;
}

private HttpCookie CreateNewSessionCookie(string cookieKey, string
cookieValue)
{
HttpCookie cookie = new HttpCookie(cookieKey, cookieValue);
Request.Cookies.Add(cookie);
return cookie;
}

The problem is in GetSessionCookie(), after the line
HttpCookie cookie = Response.Cookies[cookieKey];
A new cookie with the specified key is added to the Request.Cookies
collection and the cookie object contains key "MyCookie" with empty value. As a result, it never reaches the CreateNewSessionCookie() function call. What's going on, please help me please, anyone?
My Web.Config file contains:
<authentication mode="Forms">
<forms name="MySettings" loginUrl="login.aspx" protection="All"
timeout="20" path="/"></forms>
</authentication>

Many thanks in advance.
KD

Nov 19 '05 #2
Without the setting .Expires, the cookie is used only during the
current user session. T0 persist the cookie, set the .Expires property.
const string NAME_COOKIE = "TGWebMail";
Response.Cookies[NAME_COOKIE].Expires = DateTime.MaxValue;

Sample C# Code:
const stringNAME_COOKIE = "YourCookieName";
const stringNAME_USERID = "inpUserIDField";
const stringNAME_SERVER = "inpServerField";

private void Page_Load(object sender, System.EventArgs e)
{
// Retrieve settings from last visit
try
{
if (Request.Cookies != null)
{
inpUserID.Value =
Server.HtmlEncode(Request.Cookies[NAME_COOKIE][NAME_USERID]).ToString();
inpServer.Value =
Server.HtmlEncode(Request.Cookies[NAME_COOKIE][NAME_SERVER]).ToString();
}
}
catch {}
}

private void Submit_ServerClick(object sender, System.EventArgs e)
{
// Save user settings
try
{
Response.Cookies[NAME_COOKIE][NAME_USERID] = inpUserID.Value;
Response.Cookies[NAME_COOKIE][NAME_SERVER] = inpServer.Value;
Response.Cookies[NAME_COOKIE].Expires = DateTime.MaxValue;
}
catch {}
}
www.VoiceInformation.com . . <a href="http://www.VoiceInformation.com">
Virtual Office, Automated Virtual Phone Assistant, Web Message </a><br>

www.TekGuard.com . . . . . . . . . . <a href="http://www.TekGuard.com">
Free Mail Server, AntiSpam, PlugIn, WebMail, Free Source Code </a>

Nov 19 '05 #3
Thank you all for your replies. It was greatly appreciated. Yes, I was pretty
dumb getting mixed between Request & Response.
Another interesting finding : The new value of a cookie, after you update
it, does not "stick" until after the page has made its round trip to the
server! Is this by design??? Why can't we have a method like
"Cookies.Refresh()" or "Cookies.Update()" to register the new updated value
so that it can be used straight away?????
Cheers,
KD

"Calvin KD" wrote:
Hi everyone,
Can someone tell me what's wrong with the way that i read a cookie as below:

private void Page_Load(object sender, System.EventArgs e)
{
Response.Cookies.Clear();
HttpCookie cookie = GetSessionCookie("MyCookie", "duh");
}

private HttpCookie GetSessionCookie(string cookieKey, string cookieValue)
{
HttpCookie cookie = Response.Cookies[cookieKey];
if (cookie == null)
{
cookie = this.CreateNewSessionCookie(cookieKey, cookieValue);
}
return cookie;
}

private HttpCookie CreateNewSessionCookie(string cookieKey, string
cookieValue)
{
HttpCookie cookie = new HttpCookie(cookieKey, cookieValue);
Request.Cookies.Add(cookie);
return cookie;
}

The problem is in GetSessionCookie(), after the line
HttpCookie cookie = Response.Cookies[cookieKey];
A new cookie with the specified key is added to the Request.Cookies
collection and the cookie object contains key "MyCookie" with empty value. As
a result, it never reaches the CreateNewSessionCookie() function call. What's
going on, please help me please, anyone?
My Web.Config file contains:
<authentication mode="Forms">
<forms name="MySettings" loginUrl="login.aspx" protection="All"
timeout="20" path="/"></forms>
</authentication>

Many thanks in advance.
KD

Nov 19 '05 #4

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

Similar topics

4
by: Brian Burgess | last post by:
Hi all, Anyone know of any special issues with storing cookies with ASP? I'm trying this with two browsers: One is IE 6.0 with cookies set to 'prompt'. This has been working properly as any...
7
by: Brian Burgess | last post by:
Hi all, Anyone see anything wrong with the following: *************************************************************** <%@ LANGUAGE="VBSCRIPT" %> <% Option Explicit Dim nAcctNbr Dim...
1
by: hochun | last post by:
I am trying to read non-persistent cookies that is not reading in the program coding is there any software thich can read it (just like Cookie Monster, but it can't read non-persistent cookies ) ...
5
by: Martin Heuckeroth | last post by:
Hi We are working on a webservice application and are having some problems with the cookies and/or sessions. We have them working on our intranet but then its not working on the internet. We...
2
by: Gonzosez | last post by:
How can I set my cookies to expire when the the user leaves the site?
0
by: Calvin KD | last post by:
Hi everyone, I need help urgently. I have a C#.Net app which uses cookies for state management (since we've gone away from Session for fear of webfarm and we haven't found the need for SQL Server...
5
by: jheines | last post by:
I am trying to explain how cookies and sessions work in a class I teach, but I have hit a wall when it comes to the interaction between cookies and the state of the privacy settings in Internet...
3
by: Inny | last post by:
I want To offer a login Option, two Checkboxs, 1 labeled 'normal' the other labeled 'Forever'. I want to assign the check boxes to switch between the cookie script below (normal) and An altered...
7
by: cmrchs | last post by:
Hi, In Windows Vista: where does asp.net write its cookies? I use HttpCookie objCookie = new HttpCookie("nameCookie"); in Win2000 (and later) they used to be in C:\Documents and...
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: 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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.