Warning: Complete steps in 3-CreateAnAzureServiceInTheMobileApp before beginning the steps below
Note: If you are using Visual Studio for Mac, skip this UWP implementation
-
In the Visual Studio Solution Explorer, right-click
HappyXamDevs.UWP
> Add > New Folder -
In the Visual Studio Solution Explorer, name the new folder
Services
-
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
-
In the Add New Item window, name the new file
AzureService.cs
-
In the AzureService.cs editor, enter the following code:
using System.Threading.Tasks;
using HappyXamDevs.Services;
using Microsoft.WindowsAzure.MobileServices;
[assembly: Xamarin.Forms.Dependency(typeof(HappyXamDevs.UWP.Services.AzureService))]
namespace HappyXamDevs.UWP.Services
{
public class AzureService : AzureServiceBase
{
protected override Task AuthenticateUser()
{
return Client.LoginAsync(MobileServiceAuthenticationProvider.Facebook, "happyxamdevs");
}
}
}
About the Code
Task AuthenticateUser()
callsLoginAsync
requesting a login using Facebook. The"happyxamdevs"
parameter is the name of the URL scheme that we registered in the Facebook App's ALLOWED EXTERNAL REDIRECT URLS settings
[assembly: Xamarin.Forms.Dependency(typeof(HappyXamDevs.UWP.Services.AzureService))]
is the Xamarin.Forms dependency service. This is a way to create a platform-specific implementation and access them from the Xamarin.Forms project. This registers a Android implementation forIAzureService
.
Just like with Android and iOS you will need to configure your app to handle the call back from the URL scheme.
-
In the Visual Studio Solution Explorer, open HappyXamDevs.UWP > App.xaml.cs
-
In the
App.xaml.cs
editor, add theOnActivated
method:
protected override void OnActivated(IActivatedEventArgs args)
{
if (args is ProtocolActivatedEventArgs protocolArgs
&& Window.Current.Content is Frame content
&& content.Content is MainPage)
{
content.Navigate(typeof(MainPage), protocolArgs.Uri);
}
Window.Current.Activate();
base.OnActivated(args);
}
-
In the Visual Studio Solution Explorer, open HappyXamDevs.UWP > MainPage.xaml.cs
-
In the
MainPage.xaml.cs
editor, at the top of the file, add the following using statements:
using HappyXamDevs.Services;
using HappyXamDevs.UWP.Services;
using Microsoft.WindowsAzure.MobileServices;
- In the
MainPage.xaml.cs
editor, add the following method:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (e.Parameter is Uri uriParameter)
{
var azureService = Xamarin.Forms.DependencyService.Get<IAzureService>() as AzureService;
azureService.Client.ResumeWithURL(uriParameter);
}
}
-
In the Visual Studio Solution Explorer, open HappyXamDevs.UWP > Package.appxmanifest
-
In the Package.appxmanifest editor, select the Declarations tab.
-
In the Declarations tab, open the Available Declarations drop-down
-
In the Available Declarations drop-down, select Protocol
-
In the Declarations tab, click Add
-
In the Declarations tab, in the Supported Definitions window, select the newly created Protocol declaration
-
In the Declarations tab, make the following selections:
- Display Name: Happy Xamarin Developers
- Name: happyxamdevs
Note:
Name
must match the domain of Allowed external redirect URLs configured in your Azure Functions -
In Visual Studio, save the Package.appxmanifest by selecting File > Save All
Now that your UWP app authentication has been implemented, the next step is to implement the same for Android and iOS if you haven't done so already.
Once you have set up authentication in all your apps, it is time to create the Login page.