2016-04-12 5 views
0

Xamarin.Iosアプリからの認証(Google/Facebook)が必要なサーバー上のセキュリティ保護されたWeb APIにアクセスしようとしています。Xamarin.Iosから保護されたWeb APIを呼び出すことはできません

AzureポータルからサンプルToDo Appをダウンロードして実行し、このチュートリアルのように認証を追加すると、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がちょうどJSON形式でhttp://imgur.com/Fp9FhdRあります。

私は何かが欠けていると確信していますが、私はそれが何であるかを見つけることができません。 ありがとうございます。

+0

任意のヘッダーにトークンを設定していますか?私はこれを示す何も見ません。 – Cheesebaron

答えて

0

は両側をチェックする必要があります。サーバーとクライアント:サーバーオン

、あなたは古典を使用したい場合は、ウェブAPIコントローラ

[MobileAppController] 
public class GetAdviceController : ApiController 
{ 
} 

クライアントXamarinため[MobileAppController]を持っていることを確認してくださいHttpClient Web API(Azureサーバー)にリクエストするために、少数のヘッダーを追加する必要があります。

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を使用

関連する問題