top of page
Search
  • Writer's pictureCedric Gabrang

Xamarin Forms: Integrating Microsoft (Graph API) O365 Login

Updated: Jul 1, 2019

The Microsoft Graph API offers a single endpoint https://graph.microsoft.com, provide access to rich, people-centric data and insights exposed as resources of Microsoft 365 services. You can use REST APIs or SDKs to access the endpoint and build apps that support scenarios spanning across productivity, collaboration, education, security, identity, access, device management, and much more. (https://docs.microsoft.com/en-us/graph/overview )


Microsoft Graph exposes REST APIs and client libraries to access Microsoft 365 services.


In this post, I will show you on how to integrate Microsoft 365 login in your Xamarin.Forms application.


Register a new application using the App registrations in the Azure portal so that your app can be integrated with the Microsoft identity platform and call Microsoft Graph.


Step 1: Sign in to the Azure portal using either a work or school account or a personal Microsoft account.


Step 2: In the left-hand navigation pane, select the Azure Active Directory service, and then select App registrations > New registration.


Step 3: When the Register an application page appears, enter your application's registration information:


  • Name - Enter a meaningful application name that will be displayed to users of the app.

  • Supported account types - Select which accounts you would like your application to support.


Step 4: Select Register.


Step 5: Select Add a Redirect URI.


Step 6: Select the type of app you're building, select Public client (mobile & desktop), and then check the first item on the Suggested Redirect URIs and enter the redirect URI (or reply URL) for your application. Then click Save.


Step 7: In your Xamarin.Forms application project, right-click on the solution then select Manage Nuget Packages for Solution. Install the following packages with their corresponding versions:

  • Microsoft.Graph (1.6.0)

  • Microsoft.Graph.Core (1.8.1)

  • Microsoft.Identity.Client (1.1.4 preview0002)


Step 8: Go to your App.xaml.cs then add the code below:



Step 9: Create new class and name it AuthenticationHelper. Add the following codes below:


Step 10: Create new ContentPage and name it LoginPage. Remove everything inside the ContentPage.Content tag and add the following below:


Go to your LoginPage.xaml.cs, remove everything and add the these codes below:



Step 11: In your Android project, go to MainActivity.cs and follow the code changes below:



Step 12: Open your AndroidManifest.xml and add the following inside the application tag:

Step 13: This time, in your IOS project, modify AppDelegate.cs by following the code changes below:


Step 14: Open Info.plist, go to Advanced tab and expand URL Types. Add Identifier, URL Schemes (Redirect URI) and select Role:



Step 15: Clean and Rebuild your solution.


And here’s what the app looks like when is running.


Here’s the GitHub repo of this demo.


Try it yourself! Until next time. Happy coding!

216 views0 comments

Recent Posts

See All

Xamarin Forms: Implementing SQLite Database

SQLite is a software library that provides a relational database management system. The lite in SQLite means light weight in terms of setup, database administration, and required resource. SQLite is t

Xamarin Forms: Creating Simple Login

Do you want to explore more? Are you looking for a Xamarin kickstarter? In this post, we will create simple login and registration. Step 1: Right-click on your PCL project then Add > New Item > Conten

bottom of page