title: C# - HttpWebResponse redirect to external URL
body: <p>I'm trying to achieve the following:</p>
  
  <ul>
  <li>I'm building an MVC website that will help me to automatically logon to another site.</li>
  <li><strong>WebsiteA</strong> will call <strong>WebsiteB</strong> using HttpWebRequest.</li>
  <li><strong>WebsiteA</strong> will send the details of the user through headers (request.headers.add)</li>
  <li><strong>WebsiteB</strong> will handle all the user authentication and internally it will redirect to another page granting access to that user.</li>
  </ul>
  
  <p>I've managed to achieve part of it but I'm stuck in displaying the redirection return. Anyone knows if this can be achieved?</p>
  
  <p>Here is some code that gets called in the WebsiteA app:</p>
  
  <pre><code>        [HttpPost]
          [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
          public ActionResult LogIn(myModel model)
          {
              HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://websiteB/LogIn.aspx");
              request.AllowAutoRedirect = true;
              request.Method = "POST";
              request.Headers.Add("MyUserToLogon", model.User); //I'm sending my user through headers
              string postData = "This is a test";
              byte[] byteArray = Encoding.UTF8.GetBytes(postData);
              request.ContentType = "application/x-www-form-urlencoded";
              request.ContentLength = byteArray.Length;
              Stream dataStream = request.GetRequestStream();
              dataStream.Write(byteArray, 0, byteArray.Length);
              dataStream.Close();
  
              //Get the response from the Login Page
              HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  
              // Display the status.
              Console.WriteLine(((HttpWebResponse)response).StatusDescription);
              dataStream = response.GetResponseStream();
              StreamReader reader = new StreamReader(dataStream);
              string responseFromServer = reader.ReadToEnd();
              Console.WriteLine(responseFromServer);
  
              reader.Close();
              dataStream.Close();
              response.Close();
  
              //What to do here? I want to go to the redirected page from WebSiteB/Page1.aspx
              //See code below for WebSiteB.
  
              return null;
          }
  </code></pre>
  
  <p>In WebSiteB I have the code that does the redirection when the user is successfully logged in:</p>
  
  <pre><code>        .......
          users = Request.QueryString["MyUserToLogon"];
          .......
  
          private void LogIn(string user)
          {
              GrantUser(user, Session.SessionID);
  
              HttpCookie mycookie = new HttpCookie("test");
              mycookie .Expires = DateTime.Now.AddMinutes(10);
              Response.Cookies.Add(mycookie);
              Response.AddHeader("mycookie ", mycookie .Value);
  
              Response.Redirect("WebsiteB/Page1.aspx");
          }
  </code></pre>
  
  <p>Any help would be really appreciated. </p>