473,811 Members | 2,717 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do you read persistent cookies?

Ok. I know this sounds like a stupid question, but I'm really beside myself
here. I've worked with cookies for years, so I know how they work, but I am
new to C#, so apparently I'm doing something wrong with C#.

I'm saving a cookie (user preferences) that I want to load again the next
time a user visits my site. I know it's saving correctly, I can see it in
the temporary internet files. But my first page (Default.aspx) is trying to
read it from Request.Cookies and it's not there. There must be a trick to
reading in this cookie file. Ok. What is the trick? How do I pull the
saved cookies back in and/or how do I read them?
Nov 19 '05 #1
3 4945
Brian,

I found the following on http://www.csharphelp.com/archives/archive179.html:

HttpCookie cookie = Request.Cookies[strCookieName];
String strCookieValue = cookie.Value.To String();

HTH,
John

"Brian Kitt" wrote:
Ok. I know this sounds like a stupid question, but I'm really beside myself
here. I've worked with cookies for years, so I know how they work, but I am
new to C#, so apparently I'm doing something wrong with C#.

I'm saving a cookie (user preferences) that I want to load again the next
time a user visits my site. I know it's saving correctly, I can see it in
the temporary internet files. But my first page (Default.aspx) is trying to
read it from Request.Cookies and it's not there. There must be a trick to
reading in this cookie file. Ok. What is the trick? How do I pull the
saved cookies back in and/or how do I read them?

Nov 19 '05 #2
Hi, Brian, have you checked whether you cookie has expired?

"Brian Kitt" wrote:
Ok. I know this sounds like a stupid question, but I'm really beside myself
here. I've worked with cookies for years, so I know how they work, but I am
new to C#, so apparently I'm doing something wrong with C#.

I'm saving a cookie (user preferences) that I want to load again the next
time a user visits my site. I know it's saving correctly, I can see it in
the temporary internet files. But my first page (Default.aspx) is trying to
read it from Request.Cookies and it's not there. There must be a trick to
reading in this cookie file. Ok. What is the trick? How do I pull the
saved cookies back in and/or how do I read them?

Nov 19 '05 #3
Here's some sample C# cookie code that is working for me:

Sample C# Code:
const stringNAME_COOK IE = "YourCookieName ";
const stringNAME_USER ID = "inpUserIDField ";
const stringNAME_SERV ER = "inpServerField ";

private void Page_Load(objec t sender, System.EventArg s e)
{
// Retrieve settings from last visit
try
{
if (Request.Cookie s != null)
{
inpUserID.Value =
Server.HtmlEnco de(Request.Cook ies[NAME_COOKIE][NAME_USERID]).ToString();
inpServer.Value =
Server.HtmlEnco de(Request.Cook ies[NAME_COOKIE][NAME_SERVER]).ToString();
}
}
catch {}
}

private void Submit_ServerCl ick(object sender, System.EventArg s e)
{
// Save user settings
try
{
Response.Cookie s[NAME_COOKIE][NAME_USERID] = inpUserID.Value ;
Response.Cookie s[NAME_COOKIE][NAME_SERVER] = inpServer.Value ;
Response.Cookie s[NAME_COOKIE].Expires = DateTime.MaxVal ue;
}
catch {}
}
www.VoiceInformation.com . . . . . . . . . . . . <a
href="http://www.VoiceInform ation.com">
Virtual Office, Automated Personal Phone Assistant, Follow-Me VMail Web
Messaging </a><br>

www.TekGuard.com . . . . . . . . . . . . . . . . . . . . <a
href="http://www.TekGuard.co m">
Free Mail Server, AntiSpam, PlugIn, WebMail, Free Source Code,
Unlimited Users/Domain </a>

Nov 19 '05 #4

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

Similar topics

0
2722
by: bb | last post by:
Hello In my Session_OnStart in Global.asa, I am setting some cookies. One of them, I set as follows: dim UserID UserID = Request.ServerVariables("LOGON_USER") Response.Cookies("User")("ID") = UCASE(UserID) When I immediately log the cookie value retrieved from
0
1615
by: benny | last post by:
Hi, I used following procedure to created the cookies:- FormsAuthentication.RedirectFromLoginPage(IntToStr(BizCustomerID), wchkRememberLogin.Checked); I found that the setting on web.config :- <authentication mode="Forms"> <forms loginUrl="CustLogin.aspx" protection="All" name="Customer" path="/" timeout="60" />
2
1612
by: benny | last post by:
Hi, I tried the following method to add persistent cookies :- HttpCookieCollection MyCookieCollection = new HttpCookieCollection(); HttpCookie MyCookie = new HttpCookie("LastVisit"); MyCookie.Value = DateTime.Now.ToString(); MyCookieCollection.Add(MyCookie); I found out the above code seem not working if my setting on IE6.0 on
4
3349
by: Joey Powell | last post by:
Hello, I originally configured my application to use persistent cookies in error. Now, I need to find a way to disable those cookies. I have tried changing usernames and passwords for all of the users, but that doesn't help - they can still access our site using their old persistent cookies. How can I disable them and force the users to log in again?
1
1761
by: Marco Rispoli | last post by:
I am using this function to write a cookie: private static void SaveCookie(string CookieName, string CookieValue, bool Persistent) { HttpContext.Current.Response.Cookies.Value = CookieValue; HttpContext.Current.Response.Cookies.Path = "/"; HttpContext.Current.Response.Cookies.Domain = ConfigurationSettings.AppSettings.ToString(); if (Persistent) {HttpContext.Current.Response.Cookies .Expires
0
1898
by: Kepler | last post by:
I'm testing very basic FormsAuthentication and having trouble with non-persistent cookies. Once authenticated with a non-persistent cookie, if I leave the browser alone for 30 minutes, Request.IsAuthenticated returns false on my next request. WHY? At first I thought it had to do with session timeout, but session timeout is set to 20 minutes, and I'm still authenticated after 20 minutes. Thirty minutes is the magic number. I'm at a loss...
0
998
by: vbMark | last post by:
Persistent cookies does not seem to be working. Even setting the date two years in advance, e.g. myCookie.Expires = DateTime.Now.AddYears(2); When the page is reloaded the cookies expiration date is always {1/1/1} Using .Net Framework 1.1 --
4
3848
by: GaryDean | last post by:
I'm using the 2.0 login control with the "remember me" setting. When checked the cookie only last for a few hours then it is asking again for a login. I don't see any time settings. I know back when we did our own authentication we specified FormsAuthentication.RedirectFromLoginPage(tbEmail.Text, True) and the cookie lasted forever. -- Regards, Gary Blakely
9
2684
by: mel | last post by:
Hi all, I need a persistent TCP connection with my web server over page reloads. This means that, even if the user goes to a different page (in my domain), I want to keep a TCP connection live. I know I can do something similar with globalStorage in FF, window.name & userData on IE, but those storages only hold strings.
0
9727
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10398
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10133
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9204
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7669
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5554
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4339
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.