joe_pool_is Posted April 11, 2005 Posted April 11, 2005 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); } } Quote Avoid Sears Home Improvement
joe_pool_is Posted April 11, 2005 Author Posted April 11, 2005 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. Quote Avoid Sears Home Improvement
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.