|
| 1 | +--- |
| 2 | +title: "Quickstart: Sign in users and call Microsoft Graph in a Windows desktop app" |
| 3 | +description: In this quickstart, learn how a Windows Presentation Foundation (WPF) application can get an access token and call an API protected by the Microsoft identity platform. |
| 4 | +services: active-directory |
| 5 | +author: OwenRichards1 |
| 6 | +manager: CelesteDG |
| 7 | +ms.service: active-directory |
| 8 | +ms.subservice: develop |
| 9 | +ms.topic: quickstart |
| 10 | +ms.workload: identity |
| 11 | +ms.date: 08/18/2022 |
| 12 | +ms.author: owenrichards |
| 13 | +ms.custom: aaddev, identityplatformtop40, mode-api |
| 14 | +#Customer intent: As an application developer, I want to learn how my Windows Presentation Foundation (WPF) application can get an access token and call an API that's protected by the Microsoft identity platform. |
| 15 | +--- |
| 16 | + |
| 17 | +# Quickstart: Acquire a token and call the Microsoft Graph API from a Windows desktop app |
| 18 | + |
| 19 | +> [!div renderon="docs"] |
| 20 | +> Welcome! This probably isn't the page you were expecting. While we work on a fix, this link should take you to the right article: |
| 21 | +> |
| 22 | +> > [Quickstart: Windows Presentation Foundation (WPF) desktop app that signs in users and calls a web API](desktop-app-quickstart.md?pivots=devlang-windows-desktop) |
| 23 | +> |
| 24 | +> We apologize for the inconvenience and appreciate your patience while we work to get this resolved. |
| 25 | +
|
| 26 | +> [!div renderon="portal" id="display-on-portal" class="sxs-lookup"] |
| 27 | +> # Quickstart: Acquire a token and call Microsoft Graph API from a Windows desktop app |
| 28 | +
|
| 29 | +> In this quickstart, you download and run a code sample that demonstrates how a Windows Presentation Foundation (WPF) application can sign in users and get an access token to call the Microsoft Graph API. |
| 30 | +> |
| 31 | +> See [How the sample works](#how-the-sample-works) for an illustration. |
| 32 | +> |
| 33 | +> |
| 34 | +> #### Step 1: Configure your application in Azure portal |
| 35 | +> For the code sample in this quickstart to work, add a **Redirect URI** of `https://login.microsoftonline.com/common/oauth2/nativeclient` and `ms-appx-web://microsoft.aad.brokerplugin/{client_id}`. |
| 36 | +> |
| 37 | +> <button id="makechanges" class="nextstepaction configure-app-button"> Make these changes for me </button> |
| 38 | +> |
| 39 | +> > [!div id="appconfigured" class="alert alert-info"] |
| 40 | +> >  Your application is configured with these attributes. |
| 41 | +> |
| 42 | +> #### Step 2: Download your Visual Studio project |
| 43 | +> |
| 44 | +> Run the project using Visual Studio. |
| 45 | +> |
| 46 | +> > [!div class="nextstepaction"] |
| 47 | +> > <button id="downloadsample" class="download-sample-button">Download the code sample</button> |
| 48 | +> |
| 49 | +> [!INCLUDE [active-directory-develop-path-length-tip](../../../includes/active-directory-develop-path-length-tip.md)] |
| 50 | +> |
| 51 | +> #### Step 3: Your app is configured and ready to run |
| 52 | +> We have configured your project with values of your app's properties and it's ready to run. |
| 53 | +> |
| 54 | +> > [!div class="sxs-lookup"] |
| 55 | +> > > [!NOTE] |
| 56 | +> > > `Enter_the_Supported_Account_Info_Here` |
| 57 | +> |
| 58 | +> ## More information |
| 59 | +> |
| 60 | +> ### How the sample works |
| 61 | +>  |
| 62 | +> |
| 63 | +> ### MSAL.NET |
| 64 | +> MSAL ([Microsoft.Identity.Client](https://www.nuget.org/packages/Microsoft.Identity.Client)) is the library used to sign in users and request tokens used to access an API protected by Microsoft identity platform. You can install MSAL by running the following command in Visual Studio's **Package Manager Console**: |
| 65 | +> |
| 66 | +> ```powershell |
| 67 | +> Install-Package Microsoft.Identity.Client -IncludePrerelease |
| 68 | +> ``` |
| 69 | +> |
| 70 | +> ### MSAL initialization |
| 71 | +> |
| 72 | +> You can add the reference for MSAL by adding the following code: |
| 73 | +> |
| 74 | +> ```csharp |
| 75 | +> using Microsoft.Identity.Client; |
| 76 | +> ``` |
| 77 | +> |
| 78 | +> Then, initialize MSAL using the following code: |
| 79 | +> |
| 80 | +> ```csharp |
| 81 | +> IPublicClientApplication publicClientApp = PublicClientApplicationBuilder.Create(ClientId) |
| 82 | +> .WithRedirectUri("https://login.microsoftonline.com/common/oauth2/nativeclient") |
| 83 | +> .WithAuthority(AzureCloudInstance.AzurePublic, Tenant) |
| 84 | +> .Build(); |
| 85 | +> ``` |
| 86 | +> |
| 87 | +> |Where: | Description | |
| 88 | +> |---------|---------| |
| 89 | +> | `ClientId` | Is the **Application (client) ID** for the application registered in the Azure portal. You can find this value in the app's **Overview** page in the Azure portal. | |
| 90 | +> |
| 91 | +> ### Requesting tokens |
| 92 | +> |
| 93 | +> MSAL has two methods for acquiring tokens: `AcquireTokenInteractive` and `AcquireTokenSilent`. |
| 94 | +> |
| 95 | +> #### Get a user token interactively |
| 96 | +> |
| 97 | +> Some situations require forcing users interact with the Microsoft identity platform through a pop-up window to either validate their credentials or to give consent. Some examples include: |
| 98 | +> |
| 99 | +> - The first time users sign in to the application |
| 100 | +> - When users may need to reenter their credentials because the password has expired |
| 101 | +> - When your application is requesting access to a resource that the user needs to consent to |
| 102 | +> - When two factor authentication is required |
| 103 | +> |
| 104 | +> ```csharp |
| 105 | +> authResult = await App.PublicClientApp.AcquireTokenInteractive(_scopes) |
| 106 | +> .ExecuteAsync(); |
| 107 | +> ``` |
| 108 | +> |
| 109 | +> |Where:| Description | |
| 110 | +> |---------|---------| |
| 111 | +> | `_scopes` | Contains the scopes being requested, such as `{ "user.read" }` for Microsoft Graph or `{ "api://<Application ID>/access_as_user" }` for custom web APIs. | |
| 112 | +> |
| 113 | +> #### Get a user token silently |
| 114 | +> |
| 115 | +> You don't want to require the user to validate their credentials every time they need to access a resource. Most of the time you want token acquisitions and renewal without any user interaction. You can use the `AcquireTokenSilent` method to obtain tokens to access protected resources after the initial `AcquireTokenInteractive` method: |
| 116 | +> |
| 117 | +> ```csharp |
| 118 | +> var accounts = await App.PublicClientApp.GetAccountsAsync(); |
| 119 | +> var firstAccount = accounts.FirstOrDefault(); |
| 120 | +> authResult = await App.PublicClientApp.AcquireTokenSilent(scopes, firstAccount) |
| 121 | +> .ExecuteAsync(); |
| 122 | +> ``` |
| 123 | +> |
| 124 | +> |Where: | Description | |
| 125 | +> |---------|---------| |
| 126 | +> | `scopes` | Contains the scopes being requested, such as `{ "user.read" }` for Microsoft Graph or `{ "api://<Application ID>/access_as_user" }` for custom web APIs. | |
| 127 | +> | `firstAccount` | Specifies the first user in the cache (MSAL support multiple users in a single app). | |
| 128 | +> |
| 129 | +> [!INCLUDE [Help and support](../../../includes/active-directory-develop-help-support-include.md)] |
| 130 | +> |
| 131 | +> ## Next steps |
| 132 | +> |
| 133 | +> Try out the Windows desktop tutorial for a complete step-by-step guide on building applications and new features, including a full explanation of this quickstart. |
| 134 | +> |
| 135 | +> > [!div class="nextstepaction"] |
| 136 | +> > [Call Graph API tutorial](./tutorial-v2-windows-desktop.md) |
0 commit comments