services | platforms | author | level | client | service | endpoint | page_type | languages | products | description | urlFragment | ||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
active-directory |
dotnet |
Shama-K |
200 |
UWP |
Microsoft Graph |
Microsoft identity platform |
sample |
|
|
This sample demonstrates how to use the Microsoft Authentication Library for .NET to get an access token and call the Microsoft Graph from a UWP app. |
uwp-signing-in-graph-aad |
Universal Windows Platform application signing in users with Microsoft and calling the Microsoft Graph
Getting Started | Library | Docs | Support |
---|
- Universal Windows Platform application signing in users with Microsoft and calling the Microsoft Graph
- About this sample
- Steps to run
- How to run this sample
- Community help and support
- Contributing
- More information
This sample demonstrates how to use the Microsoft Authentication Library (MSAL) for .NET to get an access token and call the Microsoft Graph using the MS Graph SDK from a Universal Windows Platform (UWP) application.
- The .NET client UWP application uses the Microsoft Authentication Library (MSAL) to sign-in a user and obtain a JWT access token from Azure Active Directory (Azure AD).
- The access token is then used as a bearer token to call Microsoft Graph and fetch the signed-in user's details.
Looking for previous versions of this code sample? Check out the tags on the releases GitHub page.
You can get full explanation about this sample, and build it from scratch by going to Call the Microsoft Graph API from a Universal Windows Platform (UWP) application. You would have to change a few things (see below, build from scratch)
To run this sample, you'll need:
- Visual Studio 2022
- An Internet connection
- Windows SDK minimum version 10.0.17663.0
- An Azure Active Directory (Azure AD) tenant. For more information on how to get an Azure AD tenant, see How to get an Azure AD tenant
- A user account in your Azure AD tenant. This sample will not work with a Microsoft account (formerly Windows Live account). Therefore, if you signed in to the Azure portal with a Microsoft account and have never created a user account in your directory before, you need to do that now.
- AzureAD Powershell Module if opting to use the automatic set up in Step 2 (available at the Powershell Gallery)
From your shell or command line:
git clone https://github.com/Azure-Samples/active-directory-dotnet-native-uwp-v2.git
or download and extract the repository .zip file.
Given that the names of the referenced NuGet packages are quite long, you might want to clone it in a folder close to the root of your hard drive, to avoid file size limitations on Windows.
As a first step you'll need to:
- Sign in to the Azure portal using either a work or school account or a personal Microsoft account.
- If your account is present in more than one Azure AD tenant, select your profile at the top right corner in the menu on top of the page, and then switch directory. Change your portal session to the desired Azure AD tenant.
- Navigate to the Microsoft identity platform for developers App registrations page.
- Select New registration.
- In the Register an application page that appears, enter your application's registration information:
- In the Name section, enter a meaningful application name that will be displayed to users of the app, for example
UWP-App-calling-MSGraph
. - Under Supported account types, select Accounts in any organizational directory and personal Microsoft accounts (e.g. Skype, Xbox, Outlook.com).
- In the Name section, enter a meaningful application name that will be displayed to users of the app, for example
- Select Register to create the application.
- In the app's registration screen, find and note the Application (client) ID. You use this value in your app's configuration file(s) later in your code.
- The redirect URI is tied to the application's identity. To find it, execute the following code inside your app:
// returns smth like S-1-15-2-2601115387-131721061-1180486061-1362788748-631273777-3164314714-2766189824
string sid = Windows.Security.Authentication.Web.WebAuthenticationBroker.GetCurrentApplicationCallbackUri().Host.ToUpper();
// This is redirect uri you need to register in the app registration portal. The app config does not need it.
string redirectUri = $"ms-appx-web://microsoft.aad.brokerplugin/{sid}";
-
In the app's registration screen, select Authentication in the menu.
- If you don't have a platform added, select Add a platform and select the Public client (mobile & desktop) option.
- In the Redirect URIs | add the redirect URI of your app (see above).
-
Select Save to save your changes.
-
In the app's registration screen, click on the API permissions blade in the left to open the page where we add access to the APIs that your application needs.
- Click the Add a permission button and then,
- Ensure that the Microsoft APIs tab is selected.
- In the Commonly used Microsoft APIs section, click on Microsoft Graph
- In the Delegated permissions section, select User.Read in the list. Use the search box if necessary.
- Click on the Add permissions button at the bottom.
Open the project in your IDE (like Visual Studio) to configure the code.
In the steps below, "ClientID" is the same as "Application ID" or "AppId".
- Open the
Native_UWP_V2\MainPage.xaml.cs
file - Find the below line
and replace the existing value with the application ID (clientId) of the
private const string ClientId = "4a1aa1d5-c567-49d0-ad0b-cd957a47f842"
UWP-App-calling-MSGraph
application copied from the Azure portal. - Another option is to modify this line which is currently set to "Common" that means that an user from any Tenant can log-in into the application. To restrict the log-in to current tenant, you should change the value to tenant Id or tenant name as explained inside comment for the line.
- Run the application from Visual Studio (Debug | Start without Debugging), directly on the local machine, or after deploying to a device or an emulator.
We use Stack Overflow with the community to provide support. We highly recommend you ask your questions on Stack Overflow first and browse existing issues to see if someone has asked your question before. Make sure that your questions or comments are tagged with [msal.dotnet].
If you find and bug in the sample please raise the issue on GitHub Issues.
If you find a bug in msal.Net, please raise the issue on MSAL.NET GitHub Issues.
To provide a recommendation, visit our User Voice page.
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.
For more information see MSAL.NET's conceptual documentation: