Cookies ... monster. Please help

G

Guest

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
 
P

Patrice

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
 
W

www.VoiceInformation.com

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>
 
G

Guest

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
 

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

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top