title: C# WebClient FormsAuthentication
body: <p>EDIT: At the bottom .</p>
  
  <p>I've this site which uses a WebService. 
  Authentication is enabled using this way : 
  "<a href="https://msdn.microsoft.com/en-us/library/xdt4thhy.aspx" rel="nofollow">How to: Implement Simple Forms Authentication</a>".</p>
  
  <p>I've a WinForms application who cant connect to the WebService (because this one needs an authentication).
  There is an example how to perform this authentication between the WebService and the WinForms ? I heard about System.Net.HttpWebRequest, but I didn't find an example to use it .</p>
  
  <pre><code>class CookieWebClient : WebClient
  {
          public CookieContainer CookieContainer { get; private set; }
  
          /// &lt;summary&gt;
          /// This will instanciate an internal CookieContainer.
          /// &lt;/summary&gt;
          public CookieWebClient()
          {
              this.CookieContainer = new CookieContainer();
          }
  
          /// &lt;summary&gt;
          /// Use this if you want to control the CookieContainer outside this class.
          /// &lt;/summary&gt;
          public CookieWebClient(CookieContainer cookieContainer)
          {
              this.CookieContainer = cookieContainer;
          }
  
          protected override WebRequest GetWebRequest(Uri address)
          {
              var request = base.GetWebRequest(address) as HttpWebRequest;
              if (request == null) return base.GetWebRequest(address);
              request.CookieContainer = CookieContainer;
              return request;
          }
  }
  </code></pre>
  
  <p>and when I want to authenticate myself : </p>
  
  <pre><code>using (var client = new CookieWebClient())
  {
      var values = new NameValueCollection
      {
          { "UserEmail.Text", "Administrator" },
          { "UserPass.Text", "admin" },
      };
      client.UploadValues("http://localhost:54787/logon.aspx", values);
  
      // If the previous call succeeded we now have a valid authentication cookie
      // so we could download the protected page
      string result = client.DownloadString("http://localhost:54787/MyWantedPage.aspx");
  }
  </code></pre>
  
  <p>The problem is maybe, that I don't really put the right user and password (Administrator/admin) in the right field on the Logon.aspx... (UserMail and UserPass).</p>
  
  <p>What do you thing ? (of course I assume is easier to use WebClient class.. ) 
  (If I really got it, NetworkCredentials is only available with Windows Authentication and Basic Authenticaiton isnt ? I've in my side, FormsAuthentication - the same one on the URL I gave)</p>
  
  <p>EDIT :</p>
  
  <p>OK, I saw here : </p>
  
  <p><a href="http://odetocode.com/articles/162.aspx" rel="nofollow">http://odetocode.com/articles/162.aspx</a></p>
  
  <p>And so, I also tried to perform the HTTPWebRequest saw it on the page.</p>
  
  <p>I get this error when closing the response : </p>
  
  <pre><code>webRequest.GetResponse().Close();
  </code></pre>
  
  <p>the error is below : </p>
  
  <pre><code>System.Net.WebException: Le serveur distant a retourné une erreur : (500) Erreur interne du serveur.
  </code></pre>
  
  <p>à System.Net.HttpWebRequest.GetResponse()</p>
  
  <p>Apparently there is really a problem with my WebClient POST request and my HTTPWebRequest, may be something to configure on the IIS server ?</p>
  
  <p>Thanks again the community !</p>