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

回答

相关问题