How to use authentication using MVC Web API?

I am trying to configure authentication for the MVC web API accessed by another MVC site. I have tried many things in web.config, many suggestions from SO. However, all to no avail.

I am using the following code from the MVC website:

var web = new WebClient();
web.UseDefaultCredentials = false;
web.Credentials = new NetworkCredential(username, password);

Then I use this web client to call methods on another MVC site that contains only API controllers. Without authentication, everything works as it should, but I cannot get authentication to work. When I execute the request, I get an exception that returns 401 (which is good if you ask me, but it does not seem to send credentials).

I also tried putting the username and password in the url, but that didn't work either.

Here is the relevant section of the web.config file of the API website:

    <authentication>
      <forms 
        cookieless="UseUri"
        enableCrossAppRedirects="false">
        <credentials passwordFormat="Clear">
          <user name="site" password="XYZ123!"/>
        </credentials>
      </forms>
    </authentication>
    <authorization>
      <deny users="?"/>
    </authorization>

/, , -, . API- "".

: -API, ?

+4
2

. AuthorizationAttribute, / 401, .

. .

+4

, this answer. . , , .

+1

All Articles