title: How can I login to ASP.Net Forms Authenticated site using C#?
body: <p>I am trying to Screen Scrape a WebSite that uses <strong>ASP.Net Forms Authentication</strong>. I have the following code which makes a <code>Get request to get the cookies</code> and then I want to post the Login info with the cookies I just got to the login.aspx page.</p>
  
  <p>When I watch what is submitted to the login.aspx page with Fiddler I see the Posted data but for the submitted cookies I see the message <code>" but it says "This request did not send any cookie data."</code></p>
  
  <p>If I login into the app using Internet Explorer I can see that the cookies and the posted data are submitted to the login.aspx page and everything is working fine.</p>
  
  <p>But if I loop through I can print out the cookies that I am assuming should be sent along with the request using this block</p>
  
  <pre><code>   foreach (System.Net.Cookie cook in getCookies.CookieContainer.GetCookies(new Uri("https://app.example.com")))
     {
          Console.WriteLine(cook.Name + ": " + cook.Value);
      }
  </code></pre>
  
  <p>What is is that I am doing wrong that is resulting in the cookies not being sent with my request?</p>
  
  <pre><code>    public void Login()
      {
          HttpWebRequest getCookies = (HttpWebRequest)WebRequest.Create("https://app.example.com");
  
          CookieContainer cookieJar = new CookieContainer();
          getCookies.Accept = "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/vnd.ms-xpsdocument, application/x-ms-application, application/x-ms-xbap, application/xaml+xml, */*";
          getCookies.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0E; .NET4.0C)";
          getCookies.Headers.Add("Accept-Encoding", "gzip, deflate");
          getCookies.AllowAutoRedirect = true;
  
          getCookies.CookieContainer = cookieJar;
          using (HttpWebResponse cookieResponse = getCookies.GetResponse() as HttpWebResponse)
          {
              StreamReader responseReader = new StreamReader(cookieResponse.GetResponseStream());
              string responseData = responseReader.ReadToEnd();
  
              string ViewState = this.GetViewStateFromHtml(responseData);
  
              getCookies = (HttpWebRequest)WebRequest.Create("https://app.example.com/Membership/Login.aspx?ReturnUrl=%2fHome%2fHomeSummary.aspx");
              getCookies.Method = "Post";
              getCookies.ContentType = "application/x-www-form-urlencoded";
              getCookies.Accept = "text/html, application/xhtml+xml. */*";
              getCookies.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0E; .NET4.0C)";
              getCookies.Headers.Add("Accept-Encoding", "gzip, deflate");
              getCookies.AllowAutoRedirect = true;
  
              ASCIIEncoding encoding = new ASCIIEncoding();
              byte[] byte1 = encoding.GetBytes(this.GetPostData(Username, Password));
  
              getCookies.ContentLength = byte1.Length;
  
              StreamWriter requestWriter = new StreamWriter(getCookies.GetRequestStream());
              requestWriter.Write(this.GetPostData(Username, Password));
              requestWriter.Close();
              getCookies.CookieContainer = cookieJar;
  
              foreach (System.Net.Cookie cook in getCookies.CookieContainer.GetCookies(new Uri("https://app.example.com")))
              {
                  Console.WriteLine(cook.Name + ": " + cook.Value);
              }
  
              using (HttpWebResponse postResponse = (HttpWebResponse)getCookies.GetResponse())
              {
                  StreamReader postLoginResponseReader = new StreamReader(postResponse.GetResponseStream());
                  string postLoginResponseData = postLoginResponseReader.ReadToEnd();
                  File.WriteAllText(@"C:\postLoginResponse.txt", postLoginResponseData);
              }
              Console.WriteLine(@"Check C:\postLoginResponse.txt");
              Console.ReadLine();
          }
  </code></pre>