Drew
Drew

Reputation: 2621

Create a cookie using Windows Forms C# Application?

I am writing an extension for IE using C# and Windows Forms / BHO Objects. I need to create a cookie which I can use to access information from other classes / forms in the app.

The various methods for creating cookies all seem to be related to ASP.NET and using the Http Response Filter, which do not help me here. I am not sure that I even need a session for what I am trying to accomplish, I just need to be able to create the cookie, access the information from the application, and then purge when needed. Thanks!

Upvotes: 0

Views: 3459

Answers (2)

Nivid Dholakia
Nivid Dholakia

Reputation: 5442

I also used similar code for our application cookies.

 HttpWebRequest runTest;
 //...do login request
//get cookies from response

CookieContainer myContainer = new CookieContainer();
for (int i = 0; i < Response.Cookies.Count; i++)
{
 HttpCookie http_cookie = Request.Cookies[i];
 Cookie cookie = new Cookie(http_cookie.Name, http_cookie.Value, http_cookie.Path);
 myContainer.Add(new Uri(Request.Url.ToString()), cookie);
}

//later:
 HttpWebRequest request =       
(HttpWebRequest)WebRequest.Create("http://www.url.com/foobar");
 request.CookieContainer = myContainer;

Upvotes: 1

Sascha
Sascha

Reputation: 10347

Sessions and cookies are web technology. You probably won't need to have that in your Windows application. Instead you can use a static class.

internal static class MySharedInfo {
    /* static proerties and whatever you need */
}

This is just one way. A really suitable method depends on what you actually want to achieve.

Upvotes: 1

Related Questions