Reputation: 53
I am trying to get the Authentication to work for the Xamarin Field Engineer Mobile App. I have the Xamarin working with my Azure SQL db without authentication. Although I have followed the setup of Custom Azure Active Directory correctly and can access it via fiddler, the code I am using doesn't complete the access to the login page as it bring up a blank Authentication page on my android phone.
I am trying to get the user to login only when they are refreshing the data or are trying to get online. If they are offline or don't have wifi access there is no since logging in.
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();
}
}
}
Upvotes: 1
Views: 219
Reputation: 3875
You need to add an authentication delegating handler to your client. Here's a sample handler: https://github.com/lindydonna/ContosoMoments/blob/master/src/Mobile/ContosoMoments/AuthHandler.cs
To use, specify the handler when you initialize your client, like so:
var authHandler = new AuthHandler();
MobileService = new MobileServiceClient(ApplicationURL, new LoggingHandler(true), authHandler);
Upvotes: 0