1

Xamarin Field Engineer Mobile Appで認証を取得しようとしています。私はXamarinが認証なしで私のAzure SQLデータベースを使って作業しています。私はCustom Azure Active Directoryの設定に正しく従い、フィドラー経由でアクセスすることができますが、使用しているコードはログインページへのアクセスを完了しないため、アンドロイド電話で空の認証ページが表示されます。Xamarin Android Azure Mobile App Azure Active Directoryでの.NET認証

私は、ユーザーにデータをリフレッシュしているとき、またはオンラインにしようとしているときにのみログインしようとしています。オフラインまたはWi-Fiアクセスがない場合は、ログインしてから何もしません。

using System; 
using System.Collections.Generic; 
using System.Threading.Tasks; 
using Microsoft.WindowsAzure.MobileServices; 
using Microsoft.WindowsAzure.MobileServices.SQLiteStore; 
using Microsoft.WindowsAzure.MobileServices.Sync; 
using FieldEngineerLite.Helpers; 
using FieldEngineerLite.Models; 
using Microsoft.WindowsAzure.MobileServices.Eventing; 
using System.Diagnostics; 
using System.Net; 
using System.Security.Policy; 

namespace FieldEngineerLite 
{ 

    public class JobService 
    { 
     public bool LoginInProgress = false; 
     public bool Online = false;   

     public IMobileServiceClient MobileService = null; 
     private IMobileServiceSyncTable<Job> jobTable; 

     // Placeholder string for Try App Service is ZUMOAPPURL 
     // To use with your own app, use URL in the form https://xamaringisdemo.azurewebsites.net/ 
     private const string MobileUrl = "https://xamaringisdemo.azurewebsites.net/"; 

     public async Task InitializeAsync() 
     { 
      this.MobileService = 
       new MobileServiceClient(MobileUrl, new LoggingHandler()); 

      var store = new MobileServiceSQLiteStore("local.db"); 
      store.DefineTable<Job>(); 

      await MobileService.SyncContext.InitializeAsync(store, StoreTrackingOptions.NotifyLocalAndServerOperations); 
      jobTable = MobileService.GetSyncTable<Job>(); 
     } 

     public async Task<IEnumerable<Job>> ReadJobs(string search) 
     { 
      return await jobTable.ToEnumerableAsync(); 
     } 

     public async Task UpdateJobAsync(Job job) 
     { 
      job.Status = Job.CompleteStatus; 

      await jobTable.UpdateAsync(job); 

      // trigger an event so that the job list is refreshed 
      await MobileService.EventManager.PublishAsync(new MobileServiceEvent("JobChanged")); 
     } 

     public async Task SyncAsync() 
     { 
      if(await EnsureLogin()); 
      try 
      { 
       await this.MobileService.SyncContext.PushAsync(); 
       await jobTable.PullAsync(null, jobTable.CreateQuery()); 
      } 
      catch (Exception e) 
      { 
       Debug.WriteLine(e); 
      } 
     } 

     public async Task CompleteJobAsync(Job job) 
     { 
      await UpdateJobAsync(job); 

      if (Online) 
       await this.SyncAsync(); 
     } 

     public async Task<bool> EnsureLogin() 
     { 
      LoginInProgress = true; 
      while (this.MobileService.CurrentUser == null) 
      { 
       try 
       { 
        await this.MobileService.LoginAsync(App.UIContext, 
         MobileServiceAuthenticationProvider.WindowsAzureActiveDirectory); 
        Online = true; 
       } 
       catch (Exception ex) 
       { 
        Console.WriteLine("failed to authenticate: " + ex.Message); 
        Online = false; 
       } 
      } 
      LoginInProgress = false; 
      return await EnsureLogin(); 
     } 
    } 
} 

答えて

0

クライアントに認証委任ハンドラを追加する必要があります。ここではサンプルハンドラーです:https://github.com/lindydonna/ContosoMoments/blob/master/src/Mobile/ContosoMoments/AuthHandler.cs

使用するには、あなたがあなたのクライアントを初期化するときに同じように、ハンドラを指定します。

var authHandler = new AuthHandler(); MobileService = new MobileServiceClient(ApplicationURL, new LoggingHandler(true), authHandler);

関連する問題