Working with cookies

P

Pete Davis

I've never done anything with cookies. What I'm trying to do is very
straight-forward, but for some reason, it just doesn't seem to want to work.
I have a helper class with some static methods. Two are for setting and
getting the name of the user to/from a cookie:

public static string GetSiteUser(HttpRequest request)
{
HttpCookie userCookie = request.Cookies["SiteUser"];
if (null == userCookie || null == userCookie.Value)
{
return "";
}
return userCookie.Value;
}

public static void SetSiteUser(HttpResponse response, string user)
{
HttpCookie userCookie = response.Cookies["SiteUser"];
userCookie.Expires = DateTime.Now.AddYears(1);
userCookie.Value = user;
}

I call SetSiteUser() but it doesn't seemto be writing the cookie (I checked
the actual cookie file)

The only thing I can think of is that right after I call SetSiteUser, I'm
calling:

Response.Redirect(Request.RawUrl);

Which, as long as I'm here, is there a better way to force a page to
refresh?

Essentially what I've got is something like a blog comment page. You type in
a title, your name (with the option of "Remember me", and an area for
comments. When you click submit, it should refresh the page to show all the
comments (including the ones just added).

Thanks for any help.

Pete
 
G

Guest

Hi Pete,

as far as I knwo, it has nothing todo with the redirect.
Your method "SetSiteUser" is missing some major implementation:

....
response.Cookies.Add (userCookie);
....

If you do not add the cookie to the HttpResponse object, it will never be
written to the header-output of your site. And remember, if you want to
delete a Cookie, it's not enough to remove the cookie from the collection.
You must set the expiration date to a Date in the past and add it again to
the HttpResponse object.

Hope that helps.
cu patrick
 
P

Pete Davis

Are you sure you have to "add" the cookie?

The reason I ask is that this MS web page describing how to use cookies in
ASP.NET specifically says you don't have to:

http://msdn.microsoft.com/library/d...s/dv_vstechart/html/vbtchaspnetcookies101.asp

Look at the bottom of the section called "Writing Cookies".

It says, and the looking at it in the debugger seems to verify, that
referencing a non-existent cookie, implicitly adds it to the collection. And
I just checked HttpCookieCollection in Reflector which further verifies that
the cookie is added automatically. The relevant methods are:

public HttpCookie get_Item(string name)
{
return this.Get(name);
}

public HttpCookie Get(string name)
{
HttpCookie cookie1 = (HttpCookie) base.BaseGet(name);
if ((cookie1 == null) && (this._response != null))
{
cookie1 = new HttpCookie(name);
this.AddCookie(cookie1, true);
this._response.OnCookieAdd(cookie1);
}
return cookie1;
}

In addition, I tried manually "adding" the cookie and that didn't work.

Pete



Patrick said:
Hi Pete,

as far as I knwo, it has nothing todo with the redirect.
Your method "SetSiteUser" is missing some major implementation:

...
response.Cookies.Add (userCookie);
...

If you do not add the cookie to the HttpResponse object, it will never be
written to the header-output of your site. And remember, if you want to
delete a Cookie, it's not enough to remove the cookie from the collection.
You must set the expiration date to a Date in the past and add it again to
the HttpResponse object.

Hope that helps.
cu patrick

Pete Davis said:
I've never done anything with cookies. What I'm trying to do is very
straight-forward, but for some reason, it just doesn't seem to want to work.
I have a helper class with some static methods. Two are for setting and
getting the name of the user to/from a cookie:

public static string GetSiteUser(HttpRequest request)
{
HttpCookie userCookie = request.Cookies["SiteUser"];
if (null == userCookie || null == userCookie.Value)
{
return "";
}
return userCookie.Value;
}

public static void SetSiteUser(HttpResponse response, string user)
{
HttpCookie userCookie = response.Cookies["SiteUser"];
userCookie.Expires = DateTime.Now.AddYears(1);
userCookie.Value = user;
}

I call SetSiteUser() but it doesn't seemto be writing the cookie (I checked
the actual cookie file)

The only thing I can think of is that right after I call SetSiteUser, I'm
calling:

Response.Redirect(Request.RawUrl);

Which, as long as I'm here, is there a better way to force a page to
refresh?

Essentially what I've got is something like a blog comment page. You type in
a title, your name (with the option of "Remember me", and an area for
comments. When you click submit, it should refresh the page to show all the
comments (including the ones just added).

Thanks for any help.

Pete
 
W

William F. Robertson, Jr.

You are correct peat, you do not have to "add" the cookie, it is added
automatically for you when you request it in the response.

As far as not seeing the cookie, are you in a zone that permits storing
cookies? Have you tried accessing the same cookie client side to see if it
was even attached to the response object?

bill


Pete Davis said:
Are you sure you have to "add" the cookie?

The reason I ask is that this MS web page describing how to use cookies in
ASP.NET specifically says you don't have to:

http://msdn.microsoft.com/library/d...s/dv_vstechart/html/vbtchaspnetcookies101.asp

Look at the bottom of the section called "Writing Cookies".

It says, and the looking at it in the debugger seems to verify, that
referencing a non-existent cookie, implicitly adds it to the collection. And
I just checked HttpCookieCollection in Reflector which further verifies that
the cookie is added automatically. The relevant methods are:

public HttpCookie get_Item(string name)
{
return this.Get(name);
}

public HttpCookie Get(string name)
{
HttpCookie cookie1 = (HttpCookie) base.BaseGet(name);
if ((cookie1 == null) && (this._response != null))
{
cookie1 = new HttpCookie(name);
this.AddCookie(cookie1, true);
this._response.OnCookieAdd(cookie1);
}
return cookie1;
}

In addition, I tried manually "adding" the cookie and that didn't work.

Pete



Patrick said:
Hi Pete,

as far as I knwo, it has nothing todo with the redirect.
Your method "SetSiteUser" is missing some major implementation:

...
response.Cookies.Add (userCookie);
...

If you do not add the cookie to the HttpResponse object, it will never be
written to the header-output of your site. And remember, if you want to
delete a Cookie, it's not enough to remove the cookie from the collection.
You must set the expiration date to a Date in the past and add it again to
the HttpResponse object.

Hope that helps.
cu patrick

Pete Davis said:
I've never done anything with cookies. What I'm trying to do is very
straight-forward, but for some reason, it just doesn't seem to want to work.
I have a helper class with some static methods. Two are for setting and
getting the name of the user to/from a cookie:

public static string GetSiteUser(HttpRequest request)
{
HttpCookie userCookie = request.Cookies["SiteUser"];
if (null == userCookie || null == userCookie.Value)
{
return "";
}
return userCookie.Value;
}

public static void SetSiteUser(HttpResponse response, string user)
{
HttpCookie userCookie = response.Cookies["SiteUser"];
userCookie.Expires = DateTime.Now.AddYears(1);
userCookie.Value = user;
}

I call SetSiteUser() but it doesn't seemto be writing the cookie (I checked
the actual cookie file)

The only thing I can think of is that right after I call SetSiteUser, I'm
calling:

Response.Redirect(Request.RawUrl);

Which, as long as I'm here, is there a better way to force a page to
refresh?

Essentially what I've got is something like a blog comment page. You type in
a title, your name (with the option of "Remember me", and an area for
comments. When you click submit, it should refresh the page to show
all
 
P

Pete Davis

Bill,

Yes, I checked. I'm running locally (localhost) and my IE browser is set
to allow all cookies anyway.
Have you tried accessing the same cookie client side to see if it
was even attached to the response object?

What do you mean? I'm not sure I understand what you're asking.

Pete

William F. Robertson said:
You are correct peat, you do not have to "add" the cookie, it is added
automatically for you when you request it in the response.

As far as not seeing the cookie, are you in a zone that permits storing
cookies? Have you tried accessing the same cookie client side to see if it
was even attached to the response object?

bill


Pete Davis said:
Are you sure you have to "add" the cookie?

The reason I ask is that this MS web page describing how to use cookies in
ASP.NET specifically says you don't have to:
http://msdn.microsoft.com/library/d...s/dv_vstechart/html/vbtchaspnetcookies101.asp
Look at the bottom of the section called "Writing Cookies".

It says, and the looking at it in the debugger seems to verify, that
referencing a non-existent cookie, implicitly adds it to the collection. And
I just checked HttpCookieCollection in Reflector which further verifies that
the cookie is added automatically. The relevant methods are:

public HttpCookie get_Item(string name)
{
return this.Get(name);
}

public HttpCookie Get(string name)
{
HttpCookie cookie1 = (HttpCookie) base.BaseGet(name);
if ((cookie1 == null) && (this._response != null))
{
cookie1 = new HttpCookie(name);
this.AddCookie(cookie1, true);
this._response.OnCookieAdd(cookie1);
}
return cookie1;
}

In addition, I tried manually "adding" the cookie and that didn't work.

Pete
again
to
the HttpResponse object.

Hope that helps.
cu patrick

:

I've never done anything with cookies. What I'm trying to do is very
straight-forward, but for some reason, it just doesn't seem to want
to
work.
I have a helper class with some static methods. Two are for setting and
getting the name of the user to/from a cookie:

public static string GetSiteUser(HttpRequest request)
{
HttpCookie userCookie = request.Cookies["SiteUser"];
if (null == userCookie || null == userCookie.Value)
{
return "";
}
return userCookie.Value;
}

public static void SetSiteUser(HttpResponse response, string user)
{
HttpCookie userCookie = response.Cookies["SiteUser"];
userCookie.Expires = DateTime.Now.AddYears(1);
userCookie.Value = user;
}

I call SetSiteUser() but it doesn't seemto be writing the cookie (I checked
the actual cookie file)

The only thing I can think of is that right after I call
SetSiteUser,
I'm
calling:

Response.Redirect(Request.RawUrl);

Which, as long as I'm here, is there a better way to force a page to
refresh?

Essentially what I've got is something like a blog comment page. You type in
a title, your name (with the option of "Remember me", and an area for
comments. When you click submit, it should refresh the page to show
all
the
comments (including the ones just added).

Thanks for any help.

Pete
 
G

Guest

Yeah you're right, but when you take a closer look on the code, you will
notice that the cookie will only be added, when it's NOT NULL. So, when you
already added an cookie named "xxx" it will work; but when there is no cookie
named like the cookie you want to add, nothing happens. If it works for you,
it just means, that your browser already had a cookie named like the cookie
you want to edit.

Patrick

Pete Davis said:
Are you sure you have to "add" the cookie?

The reason I ask is that this MS web page describing how to use cookies in
ASP.NET specifically says you don't have to:

http://msdn.microsoft.com/library/d...s/dv_vstechart/html/vbtchaspnetcookies101.asp

Look at the bottom of the section called "Writing Cookies".

It says, and the looking at it in the debugger seems to verify, that
referencing a non-existent cookie, implicitly adds it to the collection. And
I just checked HttpCookieCollection in Reflector which further verifies that
the cookie is added automatically. The relevant methods are:

public HttpCookie get_Item(string name)
{
return this.Get(name);
}

public HttpCookie Get(string name)
{
HttpCookie cookie1 = (HttpCookie) base.BaseGet(name);
if ((cookie1 == null) && (this._response != null))
{
cookie1 = new HttpCookie(name);
this.AddCookie(cookie1, true);
this._response.OnCookieAdd(cookie1);
}
return cookie1;
}

In addition, I tried manually "adding" the cookie and that didn't work.

Pete



Patrick said:
Hi Pete,

as far as I knwo, it has nothing todo with the redirect.
Your method "SetSiteUser" is missing some major implementation:

...
response.Cookies.Add (userCookie);
...

If you do not add the cookie to the HttpResponse object, it will never be
written to the header-output of your site. And remember, if you want to
delete a Cookie, it's not enough to remove the cookie from the collection.
You must set the expiration date to a Date in the past and add it again to
the HttpResponse object.

Hope that helps.
cu patrick

Pete Davis said:
I've never done anything with cookies. What I'm trying to do is very
straight-forward, but for some reason, it just doesn't seem to want to work.
I have a helper class with some static methods. Two are for setting and
getting the name of the user to/from a cookie:

public static string GetSiteUser(HttpRequest request)
{
HttpCookie userCookie = request.Cookies["SiteUser"];
if (null == userCookie || null == userCookie.Value)
{
return "";
}
return userCookie.Value;
}

public static void SetSiteUser(HttpResponse response, string user)
{
HttpCookie userCookie = response.Cookies["SiteUser"];
userCookie.Expires = DateTime.Now.AddYears(1);
userCookie.Value = user;
}

I call SetSiteUser() but it doesn't seemto be writing the cookie (I checked
the actual cookie file)

The only thing I can think of is that right after I call SetSiteUser, I'm
calling:

Response.Redirect(Request.RawUrl);

Which, as long as I'm here, is there a better way to force a page to
refresh?

Essentially what I've got is something like a blog comment page. You type in
a title, your name (with the option of "Remember me", and an area for
comments. When you click submit, it should refresh the page to show all the
comments (including the ones just added).

Thanks for any help.

Pete
 
W

William F. Robertson, Jr.

You can access cookies client side using document.cookies.

Codebehind:
Response.Cookies["myCookie"] = "myvalue";

ClientSide
document.cookies will return something like "myCookie=myvalue", along with
the asp.net session id cookie. I did look more closely at your code, and I
could not confirm it, but I do not believe cookies are sent when you do a
Response.Redirect. Redirect simply sends instructions to the client to GET
another page.

Instead of using Redirect, you could write out startup script to accomplish
this and have the cookies sent to the user.

Page.RegisterStartUpScript( "redirectScript", @"
<script language=""javascript"">
location.href = ""new address.aspx"";
</script> " );

bill


Pete Davis said:
Bill,

Yes, I checked. I'm running locally (localhost) and my IE browser is set
to allow all cookies anyway.
Have you tried accessing the same cookie client side to see if it
was even attached to the response object?

What do you mean? I'm not sure I understand what you're asking.

Pete

William F. Robertson said:
You are correct peat, you do not have to "add" the cookie, it is added
automatically for you when you request it in the response.

As far as not seeing the cookie, are you in a zone that permits storing
cookies? Have you tried accessing the same cookie client side to see if it
was even attached to the response object?

bill
cookies
http://msdn.microsoft.com/library/d...s/dv_vstechart/html/vbtchaspnetcookies101.asp
collection.
And verifies
that never
be again
want
to
work.
I have a helper class with some static methods. Two are for
setting
and
getting the name of the user to/from a cookie:

public static string GetSiteUser(HttpRequest request)
{
HttpCookie userCookie = request.Cookies["SiteUser"];
if (null == userCookie || null == userCookie.Value)
{
return "";
}
return userCookie.Value;
}

public static void SetSiteUser(HttpResponse response, string user)
{
HttpCookie userCookie = response.Cookies["SiteUser"];
userCookie.Expires = DateTime.Now.AddYears(1);
userCookie.Value = user;
}

I call SetSiteUser() but it doesn't seemto be writing the cookie (I
checked
the actual cookie file)

The only thing I can think of is that right after I call SetSiteUser,
I'm
calling:

Response.Redirect(Request.RawUrl);

Which, as long as I'm here, is there a better way to force a page to
refresh?

Essentially what I've got is something like a blog comment page. You
type in
a title, your name (with the option of "Remember me", and an area for
comments. When you click submit, it should refresh the page to
show
all
the
comments (including the ones just added).

Thanks for any help.

Pete
 

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

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top