Reputation: 721
I am trying to access a secured web api on my server that requires authentication(Google/Facebook) from an Xamarin.Ios app.
Downloading and running the sample ToDo App from the azure portal, and adding the authentication as in this tutorial https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-xamarin-ios-get-started-users/ works like a charm using google and facebook.
But when I create a quick ASP.NET application and add a Web Api 2 Controller, and trying to call it from the Xamarin.Ios, it dose not even reach it.
For example, have this controller:
public class GetAdviceController : ApiController
{
[HttpGet]
[Authorize]
[ResponseType(typeof(string))]
public IHttpActionResult GetAdvice()
{
return Ok("RandomAdvice/Passed");
}
}
And this code in Xamarin.Ios
public MobileServiceClient Client = new MobileServiceClient(Constants.ApplicationURL);
HttpClient restClient= new HttpClient();
try
{
MobileServiceUser User = await App.Client.LoginAsync(this, MobileServiceAuthenticationProvider.Google);
//User contains the Token and SID
Console.Error.WriteLine(@"Logged IN");
HttpResponseMessage response = new HttpResponseMessage();
try
{
response = await client.GetAsync("https://mysite.azurewebsites.net/api/getadvice");
//The response is 200 but it does not reach my controller and redirests me to the login screen of the asp.net website. https://i.sstatic.net/Sv1l9.jpg
}
catch (Exception ex)
{
Console.WriteLine( ex.Message);
}
The response.content is https://i.sstatic.net/Sv1l9.jpg just in JSON format.
I am sure I am missing something but I just can`t find what it is. Thank you.
Upvotes: 0
Views: 910
Reputation: 490
Should check both sides: server and client:
On Server, make sure you have [MobileAppController]
for the Web API Controller
[MobileAppController]
public class GetAdviceController : ApiController
{
}
Client Xamarin, if you want to use classic HttpClient
for making request to Web API (Azure server), need to add few Headers:
HttpClient restClient= new HttpClient();
restClient.DefaultRequestHeaders.Accept.Add (new MediaTypeWithQualityHeaderValue ("application/json"));
restClient.DefaultRequestHeaders.Add ("ZUMO-API-VERSION", "2.0.0");
restClient.DefaultRequestHeaders.Add ("X-ZUMO-AUTH", token);
restClient.GetAsync(....);
Use token
from User object that you've authenticated with Google
Upvotes: 1