Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
Someone please show me how to *create* subitems (or subkeys) for my cookies. I have found code out there for doing this in VB.NET, but I can not seem to get it to work in C#. Here is what I have:
void Page_Load(Object Sender, EventArgs e) {
 HttpCookie cookie = Request.Cookies["userCookie"];
 if (cookie == null) {
cookie = new HttpCookie("userInfo");
cookie.HasKeys = true; // this bombs: HasKeys is read only
cookie.Value["userInfo"]["userName"] = "jdoe"; // this bombs. Why?
cookie.Value["realName"] = "John Doe"; // this bombs, too. Why?
cookie.Value["lastVisit"] = DateTime.Now.ToString; // can't get this far
cookie.Expires = DateTime.Now.AddDays(365);
cookie.Path = Server.MapPath("/");
Response.Cookies.Add(cookie);
 }
}

Posted

Okay, I got this to work:

void Page_Load(Object Sender, EventArgs e) {
HttpCookie cookie = Request.Cookies["userCookie"];
if (cookie == null) {
cookie = new HttpCookie("userInfo");
cookie.Values.Add("userName", "jdoe");
cookie.Values.Add("realName", "John Doe");
cookie.Values.Add("lastVisit", DateTime.Now.ToString());
cookie.Expires = DateTime.Now.AddDays(365);
cookie.Path = Server.MapPath("/");
Response.Cookies.Add(cookie);
} else {
if (cookie.Value.ToString() == "") {
loginPanel.Visible = true;
} else {
 // if userInfo is found in the database 
 welcomeName.Text = "Welcome back " + cookie["realName"].ToString();
 welcomePanel.Visible = true;
}
}
}

 

Just posting this to help others that might browse here looking for solutions.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...