title: How to use authentication with MVC Web API?
body: <p>I'm trying to configure authentication for an MVC Web API that is accessed by another MVC site. I have tried many things in the web.config, many suggestions from SO. However all to no avail. </p>
  
  <p>I am using the following code from the MVC website:</p>
  
  <pre><code>var web = new WebClient();
  web.UseDefaultCredentials = false;
  web.Credentials = new NetworkCredential(username, password);
  </code></pre>
  
  <p>I then use that web client to invoke methods on the other MVC site that only contains API controllers. Without authentication everything works like it should but I can't get authentication to work. When making the request I get an exception that a 401 is returned (which is a good thing if you ask me but it doesn't appear to send the credentials). </p>
  
  <p>I also tried to put the username and password in the URL but that didn't work either. </p>
  
  <p>Here is the relevant section of the web.config file of the Web API site:</p>
  
  <pre><code>    &lt;authentication&gt;
        &lt;forms 
          cookieless="UseUri"
          enableCrossAppRedirects="false"&gt;
          &lt;credentials passwordFormat="Clear"&gt;
            &lt;user name="site" password="XYZ123!"/&gt;
          &lt;/credentials&gt;
        &lt;/forms&gt;
      &lt;/authentication&gt;
      &lt;authorization&gt;
        &lt;deny users="?"/&gt;
      &lt;/authorization&gt;
  </code></pre>
  
  <p>I want to put a single username/password there just to make sure that it is the website that is invoking methods. My API controllers have the 'Authorize' attribute btw.</p>
  
  <p>My question is: how can I add authentication to the Web API site so that I can invoke methods on it using authentication?</p>