Go Back   Velocity Reviews > Newsgroups > ASP Net
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

ASP Net - Cookies C#

 
Thread Tools Search this Thread
Old 07-29-2005, 12:01 PM   #1
Default Cookies C#


Howdy
I'm using these two functions I got off the web for cookies, but with a
problem:

public bool SetCookie(string cookiename, string cookievalue ,int
iDaysToExpire)
{
try{
HttpCookie objCookie = new HttpCookie(cookiename);
Response.Cookies.Clear();
Response.Cookies.Add(objCookie);
objCookie.Values.Add(cookiename,cookievalue);
DateTime dtExpiry = DateTime.Now.AddDays(iDaysToExpire);
Response.Cookies[cookiename].Expires =dtExpiry;
}
catch( Exception e){
return false;
}
return true;
}

public string GetCookie(string cookiename){
string cookyval="";
try{
cookyval= Request.Cookies[cookiename].Value;
}
catch(Exception e){
cookyval="";
}
return cookyval;
}


Howdy, if I set a cookie via say SetCookie("username", "test" ,30)

and then try to get the value using GetCookie("username")
the value returned is username=test
when I actually just want test.

How do I get this, or what am I doing wrong?
Thanks
Matt





Matt Jensen
  Reply With Quote
Old 07-29-2005, 12:07 PM   #2
Mark Rae
 
Posts: n/a
Default Re: Cookies C#

"Matt Jensen" <> wrote in message
news:...

> Howdy, if I set a cookie via say SetCookie("username", "test" ,30)
>
> and then try to get the value using GetCookie("username")
> the value returned is username=test
> when I actually just want test.
>
> How do I get this, or what am I doing wrong?


That's just how a cookie is structured - just separate the name part from
the value part and keep the value part. You could use Split() to convert the
cookie into a string array, or just use Substring to return everything after
the equals sign.


  Reply With Quote
Old 07-29-2005, 12:33 PM   #3
Matt Jensen
 
Posts: n/a
Default Re: Cookies C#

Thanks a lot Mark

I considered that option, however I thought there must be a simpler way
because it seems like a bit of a kludge, I thought that surely you can get
the value part of the name/value pair somehow?

You could do it in classic ASP, just getting the 'subkey' value of the
cookie eg. Request.Cookies("username")("username")

I'll see what I can find out
Matt

"Mark Rae" <> wrote in message
news:...
> "Matt Jensen" <> wrote in message
> news:...
>
>> Howdy, if I set a cookie via say SetCookie("username", "test" ,30)
>>
>> and then try to get the value using GetCookie("username")
>> the value returned is username=test
>> when I actually just want test.
>>
>> How do I get this, or what am I doing wrong?

>
> That's just how a cookie is structured - just separate the name part from
> the value part and keep the value part. You could use Split() to convert
> the cookie into a string array, or just use Substring to return everything
> after the equals sign.
>



  Reply With Quote
Old 07-29-2005, 12:36 PM   #4
Matt Jensen
 
Posts: n/a
Default Re: Cookies C#

Request.Cookies["username"]["username"]
worked!

"Matt Jensen" <> wrote in message
news:...
> Thanks a lot Mark
>
> I considered that option, however I thought there must be a simpler way
> because it seems like a bit of a kludge, I thought that surely you can get
> the value part of the name/value pair somehow?
>
> You could do it in classic ASP, just getting the 'subkey' value of the
> cookie eg. Request.Cookies("username")("username")
>
> I'll see what I can find out
> Matt
>
> "Mark Rae" <> wrote in message
> news:...
>> "Matt Jensen" <> wrote in message
>> news:...
>>
>>> Howdy, if I set a cookie via say SetCookie("username", "test" ,30)
>>>
>>> and then try to get the value using GetCookie("username")
>>> the value returned is username=test
>>> when I actually just want test.
>>>
>>> How do I get this, or what am I doing wrong?

>>
>> That's just how a cookie is structured - just separate the name part from
>> the value part and keep the value part. You could use Split() to convert
>> the cookie into a string array, or just use Substring to return
>> everything after the equals sign.
>>

>
>



  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump