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?
These issue was because the authentication was not working. MVC5 use Web Forms authentication, so I had to cread a new way to authenticate the HttpClient.
The error was because, when authentication fails, MVC5 redirects the HttpClient to the Login Page. So I was not getting JSON, I was getting HTML. The HTML of the Login Page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With