MVC 5 Authentication and HttpClient No MediaTypeFormatter is available

Viewed 2639

I have a MVC 5 Web Api and a desktop app that connects to it.

I have a controller and connect to it from the app without any issue, but when I put the [Authorize] attribute in the controller, the httpclient in desktop app stops working and says that there is not MediaTypeFormatter available.

I think the HttpClient is working fine, I tried defining authentication using the request header: httpClient.DefaultRequestHeaders.Authorization and using this way httpclient = new HttpClient(new HttpClientHandler { Credentials = new NetworkCredential("UserName", "password") });.

In the pass using MVC 4 it works with authentication.

In this case, wit MVC 5, it works perfectly when using Anonymous, but when I authenticate I get this exception in the httpresponse.Content.ReadAsAsync

No MediaTypeFormatter is available to read an object of type 'Dictionary`2' from content with media type 'text/html'.

Where should I see?

Update

I read the httpResponse as a string, to see what the httpclient is getting and I in the string the HTML of the login page. Aparently the authentication fails and the MVC + API redirects to the login page. What should I do now? Why the authentication doesnt work like in the previous MVC 4 web API? Is because the new OWIN authentication?

5 Answers
Related