Skip to content

Commit c032f74

Browse files
authored
Merge pull request #52708 from andretms/andret-Ignite-2018-Win-Desktop-v2-Quickstart
Andret Ignite 2018 win desktop v2 quickstart
2 parents 0ad3ea2 + 373523d commit c032f74

File tree

3 files changed

+51
-19
lines changed

3 files changed

+51
-19
lines changed

articles/active-directory/develop/tutorial-v2-windows-desktop.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ ms.devlang: na
1414
ms.topic: article
1515
ms.tgt_pltfrm: na
1616
ms.workload: identity
17-
ms.date: 04/09/2018
17+
ms.date: 09/19/2018
1818
ms.author: andret
1919
ms.custom: aaddev
2020
---

includes/active-directory-develop-guidedsetup-windesktop-setup.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ ms.devlang: na
1313
ms.topic: include
1414
ms.tgt_pltfrm: na
1515
ms.workload: identity
16-
ms.date: 04/19/2018
16+
ms.date: 09/19/2018
1717
ms.author: andret
1818
ms.custom: include file
1919

@@ -30,27 +30,26 @@ The application that you create with this guide displays a button that's used to
3030
>
3131
3232
To create your application, do the following:
33+
3334
1. In Visual Studio, select **File** > **New** > **Project**.
3435
2. Under **Templates**, select **Visual C#**.
35-
3. Select **WPF App** or **WPF Application**, depending on the version of Visual Studio version you're using.
36+
3. Select **WPF App (.NET Framework)**, depending on the version of Visual Studio version you're using.
3637

3738
## Add MSAL to your project
39+
3840
1. In Visual Studio, select **Tools** > **NuGet Package Manager**> **Package Manager Console**.
3941
2. In the Package Manager Console window, paste the following Azure PowerShell command:
4042

4143
```powershell
42-
Install-Package Microsoft.Identity.Client -Pre -Version 1.1.4-preview0002
44+
Install-Package Microsoft.Identity.Client -Pre
4345
```
4446
4547
> [!NOTE]
46-
> This command installs Microsoft Authentication Library. MSAL handles acquiring, caching, and refreshing user tokens that are used to access the APIs that are protected by Azure Active Directory v2.
48+
> This command installs Microsoft Authentication Library. MSAL handles acquiring, caching, and refreshing user tokens that are used to access the APIs that are protected by Azure Active Directory v2.0
4749
>
4850
49-
> [!NOTE]
50-
> This quickstart does not use yet the latest version of MSAL.NET, but we are working on updating it
51-
>
52-
5351
## Add the code to initialize MSAL
52+
5453
In this step, you create a class to handle interaction with MSAL, such as handling of tokens.
5554
5655
1. Open the *App.xaml.cs* file, and then add the reference for MSAL to the class:
@@ -94,4 +93,3 @@ A *MainWindow.xaml* file should automatically be created as a part of your proje
9493
</StackPanel>
9594
</Grid>
9695
```
97-

includes/active-directory-develop-guidedsetup-windesktop-use.md

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
---
2+
title: include file
3+
description: include file
4+
services: active-directory
5+
documentationcenter: dev-center-name
6+
author: andretms
7+
manager: mtillman
8+
editor: ''
9+
10+
ms.assetid: 820acdb7-d316-4c3b-8de9-79df48ba3b06
11+
ms.service: active-directory
12+
ms.devlang: na
13+
ms.topic: include
14+
ms.tgt_pltfrm: na
15+
ms.workload: identity
16+
ms.date: 09/18/2018
17+
ms.author: andret
18+
ms.custom: include file
19+
20+
---
121

222
## Use MSAL to get a token for the Microsoft Graph API
323

@@ -8,7 +28,6 @@ In this section, you use MSAL to get a token for the Microsoft Graph API.
828
```csharp
929
using Microsoft.Identity.Client;
1030
```
11-
<!-- Workaround for Docs conversion bug -->
1231

1332
2. Replace the `MainWindow` class code with the following:
1433

@@ -33,9 +52,15 @@ In this section, you use MSAL to get a token for the Microsoft Graph API.
3352
{
3453
AuthenticationResult authResult = null;
3554

55+
var app = App.PublicClientApp;
56+
ResultText.Text = string.Empty;
57+
TokenInfoText.Text = string.Empty;
58+
59+
var accounts = await app.GetAccountsAsync();
60+
3661
try
3762
{
38-
authResult = await App.PublicClientApp.AcquireTokenSilentAsync(_scopes, App.PublicClientApp.Users.FirstOrDefault());
63+
authResult = await app.AcquireTokenSilentAsync(_scopes, accounts.FirstOrDefault());
3964
}
4065
catch (MsalUiRequiredException ex)
4166
{
@@ -69,17 +94,22 @@ In this section, you use MSAL to get a token for the Microsoft Graph API.
6994

7095
<!--start-collapse-->
7196
### More information
97+
7298
#### Get a user token interactively
99+
73100
Calling the `AcquireTokenAsync` method results in a window that prompts users to sign in. Applications usually require users to sign in interactively the first time they need to access a protected resource. They might also need to sign in when a silent operation to acquire a token fails (for example, when a users password is expired).
74101

75102
#### Get a user token silently
103+
76104
The `AcquireTokenSilentAsync` method handles token acquisitions and renewals without any user interaction. After `AcquireTokenAsync` is executed for the first time, `AcquireTokenSilentAsync` is the usual method to use to obtain tokens that access protected resources for subsequent calls, because calls to request or renew tokens are made silently.
77105

78106
Eventually, the `AcquireTokenSilentAsync` method will fail. Reasons for failure might be that the user has either signed out or changed their password on another device. When MSAL detects that the issue can be resolved by requiring an interactive action, it fires an `MsalUiRequiredException` exception. Your application can handle this exception in two ways:
79107

80108
* It can make a call against `AcquireTokenAsync` immediately. This call results in prompting the user to sign in. This pattern is usually used in online applications where there is no available offline content for the user. The sample generated by this guided setup follows this pattern, which you can see in action the first time you execute the sample.
81-
* Because no user has used the application, `PublicClientApp.Users.FirstOrDefault()` contains a null value, and an `MsalUiRequiredException` exception is thrown.
82-
* The code in the sample then handles the exception by calling `AcquireTokenAsync`, which results in prompting the user to sign in.
109+
110+
* Because no user has used the application, `PublicClientApp.Users.FirstOrDefault()` contains a null value, and an `MsalUiRequiredException` exception is thrown.
111+
112+
* The code in the sample then handles the exception by calling `AcquireTokenAsync`, which results in prompting the user to sign in.
83113

84114
* It can instead present a visual indication to users that an interactive sign-in is required, so that they can select the right time to sign in. Or the application can retry `AcquireTokenSilentAsync` later. This pattern is frequently used when users can use other application functionality without disruption--for example, when offline content is available in the application. In this case, users can decide when they want to sign in to either access the protected resource or refresh the outdated information. Alternatively, the application can decide to retry `AcquireTokenSilentAsync` when the network is restored after having been temporarily unavailable.
85115
<!--end-collapse-->
@@ -114,6 +144,7 @@ public async Task<string> GetHttpContentWithToken(string url, string token)
114144
}
115145
}
116146
```
147+
117148
<!--start-collapse-->
118149
### More information about making a REST call against a protected API
119150

@@ -128,13 +159,15 @@ To sign out a user, add the following method to your `MainWindow.xaml.cs` file:
128159
/// <summary>
129160
/// Sign out the current user
130161
/// </summary>
131-
private void SignOutButton_Click(object sender, RoutedEventArgs e)
162+
private async void SignOutButton_Click(object sender, RoutedEventArgs e)
132163
{
133-
if (App.PublicClientApp.Users.Any())
164+
var accounts = await App.PublicClientApp.GetAccountsAsync();
165+
166+
if (accounts.Any())
134167
{
135168
try
136169
{
137-
App.PublicClientApp.Remove(App.PublicClientApp.Users.FirstOrDefault());
170+
await App.PublicClientApp.RemoveAsync(accounts.FirstOrDefault());
138171
this.ResultText.Text = "User has signed-out";
139172
this.CallGraphButton.Visibility = Visibility.Visible;
140173
this.SignOutButton.Visibility = Visibility.Collapsed;
@@ -146,6 +179,7 @@ private void SignOutButton_Click(object sender, RoutedEventArgs e)
146179
}
147180
}
148181
```
182+
149183
<!--start-collapse-->
150184
### More information about user sign-out
151185

@@ -167,13 +201,13 @@ private void DisplayBasicTokenInfo(AuthenticationResult authResult)
167201
TokenInfoText.Text = "";
168202
if (authResult != null)
169203
{
170-
TokenInfoText.Text += $"Name: {authResult.User.Name}" + Environment.NewLine;
171-
TokenInfoText.Text += $"Username: {authResult.User.DisplayableId}" + Environment.NewLine;
204+
TokenInfoText.Text += $"Username: {authResult.Account.Username}" + Environment.NewLine;
172205
TokenInfoText.Text += $"Token Expires: {authResult.ExpiresOn.ToLocalTime()}" + Environment.NewLine;
173206
TokenInfoText.Text += $"Access Token: {authResult.AccessToken}" + Environment.NewLine;
174207
}
175208
}
176209
```
210+
177211
<!--start-collapse-->
178212
### More information
179213

0 commit comments

Comments
 (0)