473,385 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

Nov 19 '05 #1
3 4954
"Matt Jensen" <ne***********@microsoft.com> wrote in message
news:eL**************@TK2MSFTNGP15.phx.gbl...
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.
Nov 19 '05 #2
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" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:Oz**************@TK2MSFTNGP14.phx.gbl...
"Matt Jensen" <ne***********@microsoft.com> wrote in message
news:eL**************@TK2MSFTNGP15.phx.gbl...
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.

Nov 19 '05 #3
Request.Cookies["username"]["username"]
worked!

"Matt Jensen" <ne***********@microsoft.com> wrote in message
news:eI**************@TK2MSFTNGP10.phx.gbl...
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" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:Oz**************@TK2MSFTNGP14.phx.gbl...
"Matt Jensen" <ne***********@microsoft.com> wrote in message
news:eL**************@TK2MSFTNGP15.phx.gbl...
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.


Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Brian Burgess | last post by:
Hi all, Anyone know of any special issues with storing cookies with ASP? I'm trying this with two browsers: One is IE 6.0 with cookies set to 'prompt'. This has been working properly as any...
20
by: Brian Burgess | last post by:
Hi all, Anyone know if this is possible? If so, on which page would the cookie be? .. On the page calling a function defined in the include file? thanks in advance.. -BB
9
by: | last post by:
Is it possible for a user to enable permanent cookies but disable session cookies.....this seems like a contradition yet this is what I appear to be reading in online articles?
1
by: John Taylor-Johnston | last post by:
I'm a University academic looking for a proper definition of JavaScript Cookies. http://www.CollegeSherbrooke.qc.ca/languesmodernes/604-HAE_Grammar_Practice/ I'm trying to decipher what...
6
by: Mark | last post by:
Hi... I've come across some weird bug with Response.Cookies. Or maybe it will be called "by design" but for the life of me I can't figure out what purpose it would serve. If you're setting a...
8
by: CDARS | last post by:
Hi all, I have a confusing question on ASP.NET cookies usage: 1> Response.Cookies("test").value = Now 2> Response.Write(Request.Cookies("test").value) 3> 4> Response.write("<hr>") 5>...
6
by: Stephane | last post by:
Hi, I have a login page where if the user wants his access codes to be saved are set into a cookie. In the logout page, I want to delete those cookies. I tried this and this is not working at...
1
by: Alex Nitulescu | last post by:
I have the following very simple colde (while learning about cookies and session state): Private Sub cmdAddCookie_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles...
0
by: Claudio | last post by:
I have a demo app where cookies does not work. The first page create a cookies. The second read the contet. If I browse the pages via IE6.0 the pages does not work. If I browse the pages via...
0
by: rn5a | last post by:
This is how I am creating & then reading cookies: <script runat="server"> Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs) 'create cookies Response.Cookies("UserName").Value = "Ron"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.