[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Replacing a Cookie instance

Posted on 2007-09-29
Medium Priority
Last Modified: 2008-01-09
According to MS, the CookieContainer.SetCookies Method "ADDS Cookie instances for one or more cookies from an HTTP cookie header to the CookieContainer for a specific URI," but what if I wanted to REPLACE a Cookie instance?  The below creates two instances of the same header.  I just want one.

Uri uri = new Uri(url);

// displays "MyCookie: MyValue"

MyHttpRequest.CookieContainer.SetCookies(uri,"MyCookie: MyNewValue);

// displays "MyCookie: MyNewValue; MyCookie: MyValue," rather than just "MyCookie: MyNewValue"
Question by:arichexe
  • 2
LVL 51

Expert Comment

by:Ted Bouskill
ID: 20009283
The Request object is what you received from the client.  If you want to send a cookie back you need to use the Response object

Author Comment

ID: 20009304
How would I code that?
LVL 51

Accepted Solution

Ted Bouskill earned 400 total points
ID: 20011720
// Add a new cookie
HttpCookie myCookie = new HttpCookie("UserSettings");
myCookie["Font"] = "Arial";
myCookie["Color"] = "Blue";
myCookie.Expires = DateTime.Now.AddDays(1d);

// Write to an existing cookie
Response.Cookies["UserSettings"]["Font"] = "Arial";
Response.Cookies["UserSettings"]["Color"] = "Blue";
Response.Cookies["UserSettings"].Expires = DateTime.Now.AddDays(1d);


Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question