Skip to content

Commit d2ea401

Browse files
authored
Merge pull request #51111 from wwlpublish/b0c67b7f93b8007a757ece3715f23dfc52a7087020481635f12fb53ee3418090-live
Modules/M02-authentication-microsoft-authentication-library
2 parents 8f8fa1e + 893f05e commit d2ea401

14 files changed

+38
-178
lines changed

learn-pr/wwl-azure/implement-authentication-by-using-microsoft-authentication-library/1-introduction.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Introduction
44
metadata:
55
title: Introduction
66
description: "Introduction"
7-
ms.date: 07/16/2024
7+
ms.date: 06/26/2025
88
author: wwlpublish
99
ms.author: jeffko
1010
ms.topic: unit

learn-pr/wwl-azure/implement-authentication-by-using-microsoft-authentication-library/2-microsoft-authentication-library-overview.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ title: Explore the Microsoft Authentication Library
44
metadata:
55
title: Explore the Microsoft Authentication Library
66
description: "Explore the Microsoft Authentication Library"
7-
ms.date: 07/16/2024
7+
ms.date: 06/26/2025
88
author: wwlpublish
99
ms.author: jeffko
1010
ms.topic: unit
1111
ms.custom:
12-
- sfi-ropc-nochange
13-
- N/A
12+
- N/A
1413
durationInMinutes: 3
1514
content: |
1615
[!include[](includes/2-microsoft-authentication-library-overview.md)]

learn-pr/wwl-azure/implement-authentication-by-using-microsoft-authentication-library/3-initialize-client-applications.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Initialize client applications
44
metadata:
55
title: Initialize client applications
66
description: "Initialize client applications"
7-
ms.date: 07/16/2024
7+
ms.date: 06/26/2025
88
author: wwlpublish
99
ms.author: jeffko
1010
ms.topic: unit
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
### YamlMime:ModuleUnit
22
uid: learn.wwl.implement-authentication-by-using-microsoft-authentication-library.exercise-implement-interactive-msal-net
3-
title: Exercise - Implement interactive authentication by using MSAL.NET
3+
title: Exercise - Implement interactive authentication with MSAL.NET
44
metadata:
5-
title: Exercise - Implement interactive authentication by using MSAL.NET
6-
description: "Exercise - Implement interactive authentication by using MSAL.NET"
7-
ms.date: 07/16/2024
5+
title: Exercise - Implement interactive authentication with MSAL.NET
6+
description: "Exercise - Implement interactive authentication with MSAL.NET"
7+
ms.date: 06/26/2025
88
author: wwlpublish
99
ms.author: jeffko
1010
ms.topic: unit
1111
ms.custom:
1212
- N/A
13-
durationInMinutes: 10
13+
durationInMinutes: 15
1414
content: |
1515
[!include[](includes/4-interactive-authentication-msal.md)]

learn-pr/wwl-azure/implement-authentication-by-using-microsoft-authentication-library/5-knowledge-check.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ title: Module assessment
44
metadata:
55
title: Module assessment
66
description: "Knowledge check"
7-
ms.date: 07/16/2024
7+
ms.date: 06/26/2025
88
author: wwlpublish
99
ms.author: jeffko
1010
ms.topic: unit
1111
ms.custom:
1212
- N/A
13-
module_assessment: true
1413
durationInMinutes: 3
1514
quiz:
1615
title: "Check your knowledge"

learn-pr/wwl-azure/implement-authentication-by-using-microsoft-authentication-library/6-summary.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Summary
44
metadata:
55
title: Summary
66
description: "Summary"
7-
ms.date: 07/16/2024
7+
ms.date: 06/26/2025
88
author: wwlpublish
99
ms.author: jeffko
1010
ms.topic: unit

learn-pr/wwl-azure/implement-authentication-by-using-microsoft-authentication-library/includes/1-introduction.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ After completing this module, you'll be able to:
55
* Explain the benefits of using MSAL and the application types and scenarios it supports
66
* Instantiate both public and confidential client apps from code
77
* Register an app with the Microsoft identity platform
8-
* Create an app that retrieves a token by using the MSAL.NET library
8+
* Create an app that retrieves a token with the MSAL.NET SDK

learn-pr/wwl-azure/implement-authentication-by-using-microsoft-authentication-library/includes/2-microsoft-authentication-library-overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
The Microsoft Authentication Library (MSAL) enables developers to acquire security tokens from the Microsoft identity platform to authenticate users and access secured web APIs. It can be used to provide secure access to Microsoft Graph, other Microsoft APIs, third-party web APIs, or your own web API MSAL supports many different application architectures and platforms including .NET, JavaScript, Java, Python, Android, and iOS.
1+
The Microsoft Authentication Library (MSAL) enables developers to acquire security tokens from the Microsoft identity platform to authenticate users and access secured web APIs. It can be used to provide secure access to Microsoft Graph, other Microsoft APIs, third-party web APIs, or your own web API. MSAL supports many different application architectures and platforms including .NET, JavaScript, Java, Python, Android, and iOS.
22

33
MSAL gives you many ways to get tokens, with a consistent API for many platforms. Using MSAL provides the following benefits:
44

learn-pr/wwl-azure/implement-authentication-by-using-microsoft-authentication-library/includes/3-initialize-client-applications.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
With MSAL.NET 3.x, the recommended way to instantiate an application is by using the application builders: `PublicClientApplicationBuilder` and `ConfidentialClientApplicationBuilder`. They offer a powerful mechanism to configure the application either from the code, or from a configuration file, or even by mixing both approaches.
22

3-
Before initializing an application, you first need to register it so that your app can be integrated with the Microsoft identity platform. After registration, you may need the following information (which can be found in the Azure portal):
3+
Before initializing an application, you first need to register it so that your app can be integrated with the Microsoft identity platform. After registration, you might need the following information (which can be found in the Azure portal):
44

55
* **Application (client) ID** - This is a string representing a GUID.
66
* **Directory (tenant) ID** - Provides identity and access management (IAM) capabilities to applications and resources used by your organization. It can specify if you're writing a line of business application solely for your organization (also named single-tenant application).
77
* The identity provider URL (named the **instance**) and the sign-in audience for your application. These two parameters are collectively known as the authority.
88
* **Client credentials** - which can take the form of an application secret (client secret string) or certificate (of type `X509Certificate2`) if it's a confidential client app.
9-
* For web apps, and sometimes for public client apps (in particular when your app needs to use a broker), you need to set the **Redirect URI** where the identity provider will contact back your application with the security tokens.
9+
* For web apps, and sometimes for public client apps (in particular when your app needs to use a broker), you need to set the **Redirect URI** where the identity provider sends the security token back to your application.
1010

1111
## Initializing public and confidential client applications from code
1212

Lines changed: 18 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -1,169 +1,30 @@
1-
In this exercise you learn how to perform the following actions:
1+
In this exercise, you register an application in Microsoft Entra ID, then create a .NET console application that uses MSAL.NET to perform interactive authentication and acquire an access token for Microsoft Graph. You learn how to configure authentication scopes, handle user consent, and see how tokens are cached for subsequent runs.
22

3-
* Register an application with the Microsoft identity platform
4-
* Use the `PublicClientApplicationBuilder` class in MSAL.NET
5-
* Acquire a token interactively in a console application
6-
7-
## Prerequisites
8-
9-
* An **Azure account** with an active subscription. If you don't already have one, you can sign up for a free trial at [https://azure.com/free](https://azure.com/free)
10-
* **Visual Studio Code**: You can install Visual Studio Code from [https://code.visualstudio.com](https://code.visualstudio.com/).
11-
* A version of the .NET SDK [https://dotnet.microsoft.com/download/dotnet](https://dotnet.microsoft.com/en-us/download/dotnet) (6.0, 7.0, or 8.0)
12-
13-
## Register a new application
14-
15-
1. Sign in to the portal: [https://portal.azure.com](https://portal.azure.com)
16-
17-
1. Search for and select **Microsoft Entra ID**.
18-
19-
1. Under **Manage**, select **App registrations** > **New registration**.
20-
21-
1. When the **Register an application** page appears, enter your application's registration information:
22-
23-
| Field | Value |
24-
|--|--|
25-
| **Name** | `az204appreg` |
26-
| **Supported account types** | Select **Accounts in this organizational directory only** |
27-
| **Redirect URI (optional)** | Select **Public client/native (mobile & desktop)** and enter `http://localhost` in the box to the right. |
28-
29-
1. Select **Register**.
30-
31-
Microsoft Entra ID assigns a unique application (client) ID to your app, and you're taken to your application's **Overview** page.
32-
33-
## Set up the console application
34-
35-
1. Launch Visual Studio Code and open a terminal by selecting **Terminal** and then **New Terminal**.
36-
37-
1. Create a folder for the project and change in to the folder.
38-
39-
```ps
40-
md az204-auth
41-
cd az204-auth
42-
```
43-
44-
1. Create the .NET console app.
45-
46-
```ps
47-
dotnet new console
48-
```
49-
50-
1. Open the *az204-auth* folder in Visual Studio Code.
51-
52-
```ps
53-
code . -r
54-
```
55-
56-
## Build the console app
57-
58-
In this section, you add the necessary packages and code to the project.
59-
60-
### Add packages and using statements
3+
Tasks performed in this exercise:
614

62-
1. Add the `Microsoft.Identity.Client` package to the project in a terminal in Visual Studio Code.
63-
64-
```ps
65-
dotnet add package Microsoft.Identity.Client
66-
```
67-
68-
2. Open the *Program.cs* file and add `using` statements to include `Microsoft.Identity.Client` and to enable async operations.
69-
70-
```csharp
71-
using System.Threading.Tasks;
72-
using Microsoft.Identity.Client;
73-
```
74-
75-
3. Change the Main method to enable async.
76-
77-
```csharp
78-
public static async Task Main(string[] args)
79-
```
80-
81-
### Add code for the interactive authentication
82-
83-
1. You need two variables to hold the Application (client) and Directory (tenant) IDs. You can copy those values from the portal. Add the following code and replace the string values with the appropriate values from the portal.
84-
85-
```csharp
86-
private const string _clientId = "APPLICATION_CLIENT_ID";
87-
private const string _tenantId = "DIRECTORY_TENANT_ID";
88-
```
89-
90-
2. Use the `PublicClientApplicationBuilder` class to build out the authorization context.
91-
92-
```csharp
93-
var app = PublicClientApplicationBuilder
94-
.Create(_clientId)
95-
.WithAuthority(AzureCloudInstance.AzurePublic, _tenantId)
96-
.WithRedirectUri("http://localhost")
97-
.Build();
98-
```
99-
100-
Code | Description
101-
| - | - |
102-
`.Create` | Creates a `PublicClientApplicationBuilder` from a clientID.
103-
`.WithAuthority` | Adds a known Authority corresponding to an ADFS server. In the code we're specifying the Public cloud, and using the tenant for the app we registered.
104-
105-
### Acquire a token
106-
107-
When you registered the *az204appreg* app, it automatically generated an API permission `user.read` for Microsoft Graph. You use that permission to acquire a token.
108-
109-
1. Set the permission scope for the token request. Add the following code below the `PublicClientApplicationBuilder`.
110-
111-
```csharp
112-
string[] scopes = { "user.read" };
113-
```
114-
115-
1. Add code to request the token and write the result out to the console.
116-
117-
```csharp
118-
AuthenticationResult result = await app.AcquireTokenInteractive(scopes).ExecuteAsync();
119-
120-
Console.WriteLine($"Token:\t{result.AccessToken}");
121-
```
122-
123-
## Review completed application
124-
125-
The contents of the *Program.cs* file should resemble the following example:
126-
127-
```csharp
128-
using System;
129-
using System.Threading.Tasks;
130-
using Microsoft.Identity.Client;
5+
* Register an application with the Microsoft identity platform
6+
* Create a .NET console app that implements the **PublicClientApplicationBuilder** class to configure authentication.
7+
* Acquire a token interactively using the **user.read** Microsoft Graph permission.
1318

132-
namespace az204_auth
133-
{
134-
class Program
135-
{
136-
private const string _clientId = "APPLICATION_CLIENT_ID";
137-
private const string _tenantId = "DIRECTORY_TENANT_ID";
9+
This exercise takes approximately **15** minutes to complete.
13810

139-
public static async Task Main(string[] args)
140-
{
141-
var app = PublicClientApplicationBuilder
142-
.Create(_clientId)
143-
.WithAuthority(AzureCloudInstance.AzurePublic, _tenantId)
144-
.WithRedirectUri("http://localhost")
145-
.Build();
146-
string[] scopes = { "user.read" };
147-
AuthenticationResult result = await app.AcquireTokenInteractive(scopes).ExecuteAsync();
11+
## Before you start
14812

149-
Console.WriteLine($"Token:\t{result.AccessToken}");
150-
}
151-
}
152-
}
153-
```
13+
To complete the exercise, you need:
15414

155-
## Run the application
15+
* An Azure subscription. If you don't already have one, you can sign up for one [https://azure.microsoft.com/](https://azure.microsoft.com/).
15616

157-
1. In the Visual Studio Code terminal run `dotnet build` to check for errors, then `dotnet run` to run the app.
17+
## Get started
15818

159-
1. The app opens the default browser prompting you to select the account you want to authenticate with. If there are multiple accounts listed select the one associated with the tenant used in the app.
19+
Select the **Launch Exercise** button to open the exercise instructions in a new browser window. When you're finished with the exercise, return here to:
16020

161-
1. If this is the first time you've authenticated to the registered app you receive a **Permissions requested** notification asking you to approve the app to read data associated with your account. Select **Accept**.
21+
> [!div class="checklist"]
22+
> * Complete the module
23+
> * Earn a badge for completing this module
16224
163-
:::image type="content" source="../media/permission-consent.png" alt-text="Select **Accept** to grant the permission.":::
25+
<br/>
16426

165-
1. You should see the results similar to the example below in the console.
27+
<a href="https://go.microsoft.com/fwlink/?linkid=2325009" target="_blank">
28+
<img src="../media/launch-exercise.png" alt="Button to launch exercise.">
29+
</a>
16630

167-
```
168-
Token: eyJ0eXAiOiJKV1QiLCJub25jZSI6IlVhU.....
169-
```

0 commit comments

Comments
 (0)