By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,190 Members | 1,469 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,190 IT Pros & Developers. It's quick & easy.

Working with cookies

P: n/a
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
Nov 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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" wrote:
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

Nov 19 '05 #2

P: n/a
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/de...cookies101.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" <Pa*****@discussions.microsoft.com> wrote in message
news:8B**********************************@microsof t.com...
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" wrote:
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

Nov 19 '05 #3

P: n/a
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" <pd******@NOSPAM.hotmail.com> wrote in message
news:cJ********************@giganews.com...
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/de...cookies101.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" <Pa*****@discussions.microsoft.com> wrote in message
news:8B**********************************@microsof t.com...
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" wrote:
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


Nov 19 '05 #4

P: n/a
Bill,

Yes, I checked. I'm running locally (localhost) and my IE browser is set
to allow all cookies anyway.

You wrote:
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, Jr." <th****@nameht.org> wrote in message
news:ub**************@TK2MSFTNGP09.phx.gbl... 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" <pd******@NOSPAM.hotmail.com> wrote in message
news:cJ********************@giganews.com...
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/de...cookies101.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" <Pa*****@discussions.microsoft.com> wrote in message
news:8B**********************************@microsof t.com...
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" wrote:

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



Nov 19 '05 #5

P: n/a
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" wrote:
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/de...cookies101.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" <Pa*****@discussions.microsoft.com> wrote in message
news:8B**********************************@microsof t.com...
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" wrote:
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


Nov 19 '05 #6

P: n/a
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" <pd******@NOSPAM.hotmail.com> wrote in message
news:Pf********************@giganews.com...
Bill,

Yes, I checked. I'm running locally (localhost) and my IE browser is set to allow all cookies anyway.

You wrote:
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, Jr." <th****@nameht.org> wrote in message
news:ub**************@TK2MSFTNGP09.phx.gbl...
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" <pd******@NOSPAM.hotmail.com> wrote in message
news:cJ********************@giganews.com...
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/de...cookies101.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" <Pa*****@discussions.microsoft.com> wrote in message
news:8B**********************************@microsof t.com...
> 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" wrote:
>
> > 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
> >
> >
> >



Nov 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.