Skip to content

Latest commit

 

History

History
 
 

xplat-maui

languages page_type name description products urlFragment
csharp
sample
.NET MAUI Multi-platform App Sign-in user
This .NET Multi-platform App UI (.NET MAUI) app signs in the user and then makes a request to Microsoft Graph for the user's profile data. The code in this sample is used by one or more articles on docs.microsoft.com.
azure
azure-active-directory
ms-graph
dotnet-maui
microsoft-identity-platform
ms-identity-docs-code-csharp-maui

.NET MAUI Multi-platform App - Sign-in user | Microsoft identity platform

This sample demonstrates a Windows desktop and Android mobile .NET MAUI Multi-platform App that sign-in users by using the Microsoft.Identity.Client.

A screenshot on Android displaying a response from Microsoft Graph

📃 This sample application backs one or more technical articles on docs.microsoft.com.

Prerequisites

Setup

1. Register the web API application in your Azure Active Directory

First, complete the steps in Register an application with the Microsoft identity platform to register the sample app.

Use the following settings for your app registration:

App registration
setting
Value for this sample app Notes
Name active-directory-dotnet-xplat-maui Suggested value for this sample.
You can change the app name at any time.
Supported account types Accounts in this organizational directory only (Single tenant) Required for this sample.
Support for the Single tenant.
Platform type Mobile and desktop applications Required value for this sample.
Enables the required and optional settings for the app type.
Redirect URI https://login.microsoftonline.com/common/oauth2/nativeclient Required value for this sample.
You can change that later in your own implementation.

ℹ️ Bold text in the table matches (or is similar to) a UI element in the Azure portal, while code formatting indicates a value you enter into a text box or select in the Azure portal.

2. Configure the MAUI app

  1. Open the sign-in-xplat/MainPage.xaml.cs in your code editor.

    private const string ClientId = "APPLICATION_(CLIENT)_ID";
    private const string Tenant = "TENANT_ID";
  2. Open the sign-in-xplat/Platforms/Android/AndroidManifest.xml in your code editor and replace the APPLICATION_(CLIENT)_ID placeholder with your Application (Client) Id.

    <application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round">
      <activity android:name="microsoft.identity.client.BrowserTabActivity"  android:exported="true">
        <intent-filter>
          ...
          <data android:scheme="msalAPPLICATION_(CLIENT)_ID" android:host="auth" />
        </intent-filter>
      </activity>
    </application>

Run the application

1. Run the .NET MAUI App

Android

  1. Select target Android emulator and then press F5.

Windows

  1. Select Windows Machine and then press F5.

2. Signin into the app

  1. Once the app is running you can sign-in with your user credentials.

An screenshot on Android displaying a response from Microsoft Graph

About the code

This app uses the .NET MAUI Multi-platform App UI to provide a cross-platform experience in which the Microsoft Identity Client library is used to sign the user in and out of the application.

When a signed-out user clicks the sign in button, the app offers a native Azure AD sign in experience. After signing in, and if they've not previously done so, the user is asked to consent to the app's request for permission to access their data.

While signing in, a new token is acquired and passed in an Authorization header as Bearer for a subsequent call to Microsoft Graph. When a signed-out user the account is removed from the client cleaning up the user's retrieved information.

This cross-platform solution makes use of conditional compilation so that code is targeted to specific platforms when required.

Reporting problems

Sample app not working?

If you can't get the sample working, you've checked Stack Overflow, and you've already searched the issues in this sample's repository, open an issue report the problem.

  1. Search the GitHub issues in the repository - your problem might already have been reported or have an answer.
  2. Nothing similar? Open an issue that clearly explains the problem you're having running the sample app.

All other issues

⚠️ WARNING: Any issue not limited to running this or another sample app will be closed without being addressed.

For all other requests, see Support and help options for developers | Microsoft identity platform.

Contributing

If you'd like to contribute to this sample, see CONTRIBUTING.MD.

This project has adopted the Microsoft Open Source Code of Conduct. For more information, see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.