Now that our Azure Function App is configured for authentication, we will implement authentication inside our mobile app.
We'll be using the Microsoft.Azure.Mobile.Client NuGet package to help with authentication, as well as to call Azure Functions as an authenticated user.
-
Open our newly created Xamarin.Forms app in Visual Studio
-
(PC) In Visual Studio, right-click the
HappyXamDevs
solution > Manage NuGet Packages For Solution..- (Mac) In Visual Studio for Mac, right-click the
HappyXamDevs
project > Add > Add NuGet Packages
- (Mac) In Visual Studio for Mac, right-click the
-
(PC) In the NuGet Package Manager window, select Browse
- (Mac) Skip this step
-
In the NuGet Package Manager window, in the search bar, enter Microsoft.Azure.Mobile.Client
-
In the NuGet Package Manager window, in the search results, select Microsoft.Azure.Mobile.Client
-
(PC) In the NuGet Package Manager window, select Install
- (Mac) In the NuGet Package Manager window, select Add Package
-
(PC) Skip this step
- (Mac) In Visual Studio for Mac, right-click the HappyXamDevs.Android project > Add > Add NuGet Packages
-
(PC) Skip this step
- (Mac) In the NuGet Package Manager window, in the search results, select Microsoft.Azure.Mobile.Client
-
(PC) Skip this step
- (Mac) In the NuGet Package Manager window, select Add Package
-
(PC) Skip this step
- (Mac) In Visual Studio for Mac, right-click the HappyXamDevs.iOS project > Add > Add NuGet Packages
-
(PC) Skip this step
- (Mac) In the NuGet Package Manager window, in the search results, select Microsoft.Azure.Mobile.Client
-
(PC) Skip this step
- (Mac) In the NuGet Package Manager window, select Add Package
-
In the Visual Studio Solution Explorer, right-click on the second-from-the-top HappyXamDevs option > Add > New Folder
Warning: Do not select Add Solution Folder. If you are given the option Add Solution Folder, you have right-clicked on top-most HappyXamDevs option.
-
In the Visual Studio Solution Explorer, name the new folder
Services
-
(PC) In the Visual Studio Solution Explorer, right-click on the newly created Services folder > Add > Class
- (Mac) On Visual Studio for Mac, right-click on the newly created
Services
folder > Add > New File
- (Mac) On Visual Studio for Mac, right-click on the newly created
-
In the Add New Item window, name the file
IAzureService.cs
-
(PC) In the Add New Item window, click Add
- (Mac) In the Add New Item window, click New
-
In the
IAzureService.cs
editor, add the following code:
using System.Threading.Tasks;
namespace HappyXamDevs.Services
{
public interface IAzureService
{
bool IsLoggedIn();
Task<bool> Authenticate();
}
}
Note:
IsLoggedIn
will returntrue
if there is currently a user logged in,false
if not
Note:
Authenticate
will returntrue
if the user is successfully authenticated andfalse
if not. This will be an asynchronous method, so the return value will be wrapped in aSystem.Threading.Tasks.Task
.
-
In the Visual Studio Solution Explorer, right-click on Services folder > Add > Class
- (Mac) On Visual Studio for Mac, right-click on the Services folder > Add > New File
-
In the Add New Item window, name the file
AzureServiceBase.cs
-
(PC) In the Add New Item window, click Add
- (Mac) In the Add New Item window, click New
-
In the
AzureServiceBase.cs
editor, add the following code
Note: Replace
[Your Function App Name]
with the name of your Azure Function App, e.g.HappyXamDevsFunction-Minnick
using System;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.MobileServices;
namespace HappyXamDevs.Services
{
public abstract class AzureServiceBase : IAzureService
{
protected const string AzureAppName = "[Your Function App Name]";
protected readonly static string FunctionAppUrl = $"https://{AzureAppName}.azurewebsites.net";
protected AzureServiceBase()
{
Client = new MobileServiceClient(FunctionAppUrl);
}
public MobileServiceClient Client { get; }
public bool IsLoggedIn()
{
return Client.CurrentUser != null;
}
public async Task<bool> Authenticate()
{
if (IsLoggedIn())
return true;
try
{
await AuthenticateUser();
}
catch (InvalidOperationException)
{
return false;
}
return IsLoggedIn();
}
protected abstract Task AuthenticateUser();
}
}
About The Code
string AzureAppName
andstring FunctionAppUrl
will be used in the platform-specific projects to connect to your Function back end
MobileServiceClient Client
provides APIs for authentication, information about the current user, and back-end services
bool IsLoggedIn()
verifies thatClient
has a valid current user
abstract Task AuthenticateUser()
will be implemented in our Android, iOS and/or UWP projects later
Task<bool> Authenticate()
first checks to see if there already is a logged in user, and if not call theAuthenticateUser()
- Replace
[Your Function App Name]
with the name of your Azure Function App, e.g.HappyXamDevsFunction-Minnick
The next step is to implement the platform-specific code to launch a web view to allow the user to authenticate with Facebook. You can see these steps here: