views:

22

answers:

2

How can I SetCookie in Page1.aspx by a System.Net.HttpWebRequest request for Page2.aspx which handle the SetCookie() function?

Page1.aspx and Page2.aspx are in the same webapp.

Page1.aspx:

protected void Page_Load(object sender, EventArgs e)
        {
            string url = "http://localhost/Page2.aspx";
            System.Net.HttpWebRequest myReq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
            System.Net.HttpWebResponse HttpWResp = (System.Net.HttpWebResponse)myReq.GetResponse();
            System.IO.Stream myStream = HttpWResp.GetResponseStream();
        }

Page2.aspx:

protected void Page_Load(object sender, EventArgs e)
        {
            string userName = "Lily";
            FormsAuthentication.SetAuthCookie(userName, true); 
        }
A: 

You need to use a CookieContainer.

However, if you're just trying to send the auth cookie back to the client, you should move the Page2 code to a static function, and call the function from both pages.

SLaks
A: 

If I'm reading this right, you should be able to iterate through HttpWResp.Headers() until you get the one with a "Set-Cookie" key, then parse that string, then set another cookie in your Page1.aspx code.

joelt