Connecting Tech Pros Worldwide Help | Site Map

Cookies C#

  #1  
Old November 19th, 2005, 04:55 PM
Matt Jensen
Guest
 
Posts: n/a
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



  #2  
Old November 19th, 2005, 04:55 PM
Mark Rae
Guest
 
Posts: n/a

re: Cookies C#


"Matt Jensen" <newsgrouponly@microsoft.com> wrote in message
news:eLNp1TDlFHA.3756@TK2MSFTNGP15.phx.gbl...
[color=blue]
> 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?[/color]

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.


  #3  
Old November 19th, 2005, 04:55 PM
Matt Jensen
Guest
 
Posts: n/a

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" <mark@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:OzeZQYDlFHA.2608@TK2MSFTNGP14.phx.gbl...[color=blue]
> "Matt Jensen" <newsgrouponly@microsoft.com> wrote in message
> news:eLNp1TDlFHA.3756@TK2MSFTNGP15.phx.gbl...
>[color=green]
>> 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?[/color]
>
> 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.
>[/color]


  #4  
Old November 19th, 2005, 04:55 PM
Matt Jensen
Guest
 
Posts: n/a

re: Cookies C#


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

"Matt Jensen" <newsgrouponly@microsoft.com> wrote in message
news:eIFotlDlFHA.4028@TK2MSFTNGP10.phx.gbl...[color=blue]
> 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" <mark@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
> news:OzeZQYDlFHA.2608@TK2MSFTNGP14.phx.gbl...[color=green]
>> "Matt Jensen" <newsgrouponly@microsoft.com> wrote in message
>> news:eLNp1TDlFHA.3756@TK2MSFTNGP15.phx.gbl...
>>[color=darkred]
>>> 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?[/color]
>>
>> 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.
>>[/color]
>
>[/color]


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cookies Count rn5a@rediffmail.com answers 0 September 28th, 2007 02:45 AM
Can't remove cookies! Stephane answers 6 November 19th, 2005 01:28 AM
Response.Cookies bug Mark answers 6 July 22nd, 2005 02:50 AM
Writing cookies from include file Brian Burgess answers 20 July 19th, 2005 08:22 AM
Storing Cookies Brian Burgess answers 4 July 19th, 2005 08:17 AM