I'm developing a mobile application (iOS/Android) with Xamarin (C#) and wanted to connect to my ASP.NET backend. I've read in the forum that I should just call Account/Login and use cookies for subsequent requests. I haven't really tried the last part yet (I guess it should work fine with cookies), but I've got a question to the first one.
I'm using RestSharp in my project and calling Account/Login is no problem. Just some easy-to-understand code of the request to put this in context:
RestRequest request = new RestRequest ("Account/Login", Method.POST);
request.AddParameter ("tenancyName", "foo");
request.AddParameter ("usernameOrEmailAddress", "bar");
request.AddParameter ("password", "baz");
If the login is successful, I get this JSON response:
{
"targetUrl": "/Application",
"success": true,
"result": null,
"error": null,
"unAuthorizedRequest": false
}
This is great! I can work with that. But if the credentials are wrong, I get an HTML response (and not a JSON response) and this is something I can't parse. Is there a way to always get a JSON response? I've already tried this:
request.AddHeader("Accept", "application/json");
But that doesn't seem to do anything. Is there some way to achieve this?