从 Xamarin.Ios 调用安全 Web Api 无效
Calling secure Web Api from Xamarin.Ios not working
我正在尝试访问我服务器上的安全网络 api,该网络需要来自 Xamarin.Ios 应用程序的身份验证 (Google/Facebook)。
从 Azure 门户下载和 运行 示例 ToDo 应用程序,并按照本教程添加身份验证 https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-xamarin-ios-get-started-users/ 使用 google 和 facebook 非常有效。
但是当我创建一个快速 ASP.NET 应用程序并添加一个 Web Api 2 控制器,并尝试从 Xamarin.Ios 调用它时,它甚至无法访问它。
例如,有这个控制器:
public class GetAdviceController : ApiController
{
[HttpGet]
[Authorize]
[ResponseType(typeof(string))]
public IHttpActionResult GetAdvice()
{
return Ok("RandomAdvice/Passed");
}
}
而这段代码在 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. http://imgur.com/Fp9FhdR
}
catch (Exception ex)
{
Console.WriteLine( ex.Message);
}
response.content 是 http://imgur.com/Fp9FhdR 只是 JSON 格式。
我确信我遗漏了一些东西,但我就是找不到它是什么。
谢谢。
应该检查双方:服务器和客户端:
在服务器上,确保您有 [MobileAppController]
用于 Web API 控制器
[MobileAppController]
public class GetAdviceController : ApiController
{
}
Client Xamarin,如果你想使用经典 HttpClient
向 Web API(Azure 服务器)发出请求,需要添加几个 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(....);
使用您已通过 Google
验证的用户对象中的 token
我正在尝试访问我服务器上的安全网络 api,该网络需要来自 Xamarin.Ios 应用程序的身份验证 (Google/Facebook)。
从 Azure 门户下载和 运行 示例 ToDo 应用程序,并按照本教程添加身份验证 https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-xamarin-ios-get-started-users/ 使用 google 和 facebook 非常有效。
但是当我创建一个快速 ASP.NET 应用程序并添加一个 Web Api 2 控制器,并尝试从 Xamarin.Ios 调用它时,它甚至无法访问它。
例如,有这个控制器:
public class GetAdviceController : ApiController
{
[HttpGet]
[Authorize]
[ResponseType(typeof(string))]
public IHttpActionResult GetAdvice()
{
return Ok("RandomAdvice/Passed");
}
}
而这段代码在 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. http://imgur.com/Fp9FhdR
}
catch (Exception ex)
{
Console.WriteLine( ex.Message);
}
response.content 是 http://imgur.com/Fp9FhdR 只是 JSON 格式。
我确信我遗漏了一些东西,但我就是找不到它是什么。 谢谢。
应该检查双方:服务器和客户端:
在服务器上,确保您有 [MobileAppController]
用于 Web API 控制器
[MobileAppController]
public class GetAdviceController : ApiController
{
}
Client Xamarin,如果你想使用经典 HttpClient
向 Web API(Azure 服务器)发出请求,需要添加几个 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(....);
使用您已通过 Google
验证的用户对象中的token