1

我想獲得身份驗證爲Xamarin現場工程師移動應用程序工作。我有Xamarin沒有身份驗證與我的Azure SQL數據庫一起工作。儘管我已經正確設置了自定義Azure Active Directory,並且可以通過fiddler訪問它,但我使用的代碼並未完成對登錄頁面的訪問,因爲它會在我的android手機上顯示一個空白的身份驗證頁面。Xamarin Android Azure移動應用程序.NET身份驗證與Azure活動目錄

我想讓用戶只有在刷新數據或嘗試聯機時才能登錄。如果它們處於離線狀態或沒有WiFi接入,則自登錄以來沒有。

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(); 
     } 
    } 
} 

回答

相關問題