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

x
?
Solved

Replacing a Cookie instance

Posted on 2007-09-29
3
Medium Priority
?
241 Views
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);

Console.WriteLine(MyHttpRequest.CookieContainer.GetCookieHeader(uri));
// displays "MyCookie: MyValue"

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

Console.WriteLine(MyHttpRequest.CookieContainer.GetCookieHeader(uri));
// displays "MyCookie: MyNewValue; MyCookie: MyValue," rather than just "MyCookie: MyNewValue"
0
Comment
Question by:arichexe
  • 2
3 Comments
 
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
0
 

Author Comment

by:arichexe
ID: 20009304
How would I code that?
0
 
LVL 51

Accepted Solution

by:
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);
Response.Cookies.Add(myCookie);

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

0

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