Skip to content

Commit 50b2218

Browse files
authored
Merge pull request #199094 from cristobal-buenrostro/cristobalb/adal-msal-doc
Azure Docs - ADAL to MSAL migration for Immersive Reader code sample
2 parents aac2ad8 + 0469112 commit 50b2218

File tree

5 files changed

+58
-19
lines changed

5 files changed

+58
-19
lines changed

articles/applied-ai-services/immersive-reader/how-to-cache-token.md

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,44 @@ This article demonstrates how to cache the authentication token in order to impr
1919

2020
## Using ASP.NET
2121

22-
Import the **Microsoft.IdentityModel.Clients.ActiveDirectory** NuGet package, which is used to acquire a token. Next, use the following code to acquire an `AuthenticationResult`, using the authentication values you got when you [created the Immersive Reader resource](./how-to-create-immersive-reader.md).
22+
Import the **Microsoft.Identity.Client** NuGet package, which is used to acquire a token.
23+
24+
Create a confidential client application property.
25+
26+
```csharp
27+
private IConfidentialClientApplication _confidentialClientApplication;
28+
private IConfidentialClientApplication ConfidentialClientApplication
29+
{
30+
get {
31+
if (_confidentialClientApplication == null) {
32+
_confidentialClientApplication = ConfidentialClientApplicationBuilder.Create(ClientId)
33+
.WithClientSecret(ClientSecret)
34+
.WithAuthority($"https://login.windows.net/{TenantId}")
35+
.Build();
36+
}
37+
38+
return _confidentialClientApplication;
39+
}
40+
}
41+
```
42+
43+
Next, use the following code to acquire an `AuthenticationResult`, using the authentication values you got when you [created the Immersive Reader resource](./how-to-create-immersive-reader.md).
2344

2445
> [!IMPORTANT]
2546
> The [Microsoft.IdentityModel.Clients.ActiveDirectory](https://www.nuget.org/packages/Microsoft.IdentityModel.Clients.ActiveDirectory) NuGet package and Azure AD Authentication Library (ADAL) have been deprecated. No new features have been added since June 30, 2020. We strongly encourage you to upgrade, see the [migration guide](../../active-directory/develop/msal-migration.md) for more details.
2647
2748

2849
```csharp
29-
private async Task<AuthenticationResult> GetTokenAsync()
50+
public async Task<string> GetTokenAsync()
3051
{
31-
AuthenticationContext authContext = new AuthenticationContext($"https://login.windows.net/{TENANT_ID}");
32-
ClientCredential clientCredential = new ClientCredential(CLIENT_ID, CLIENT_SECRET);
33-
AuthenticationResult authResult = await authContext.AcquireTokenAsync("https://cognitiveservices.azure.com/", clientCredential);
34-
return authResult;
52+
const string resource = "https://cognitiveservices.azure.com/";
53+
54+
var authResult = await ConfidentialClientApplication.AcquireTokenForClient(
55+
new[] { $"{resource}/.default" })
56+
.ExecuteAsync()
57+
.ConfigureAwait(false);
58+
59+
return authResult.AccessToken;
3560
}
3661
```
3762

articles/applied-ai-services/immersive-reader/includes/quickstarts/immersive-reader-client-library-csharp.md

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ In this quickstart, you build a web app from scratch and integrate Immersive Rea
2020
## Prerequisites
2121

2222
* Azure subscription - [Create one for free](https://azure.microsoft.com/free/cognitive-services)
23-
* [Visual Studio 2019](https://visualstudio.microsoft.com/downloads)
23+
* [Visual Studio 2022](https://visualstudio.microsoft.com/downloads)
2424
* An Immersive Reader resource configured for Azure Active Directory authentication. Follow [these instructions](../../how-to-create-immersive-reader.md) to get set up. You will need some of the values created here when configuring the sample project properties. Save the output of your session into a text file for future reference.
2525

2626
## Create a web app project
2727

28-
Create a new project in Visual Studio, using the ASP.NET Core Web Application template with built-in Model-View-Controller, and ASP.NET Core 2.1. Name the project "QuickstartSampleWebApp".
28+
Create a new project in Visual Studio, using the ASP.NET Core Web Application template with built-in Model-View-Controller, and ASP.NET Core 6. Name the project "QuickstartSampleWebApp".
2929

3030
![New project - C#](../../media/quickstart-csharp/1-createproject.png)
3131

@@ -48,25 +48,25 @@ Right-click on the project in the _Solution Explorer_ and choose **Manage User S
4848
}
4949
```
5050

51-
### Install Active Directory NuGet package
51+
### Install Identity Client NuGet package
5252

53-
The following code uses objects from the **Microsoft.IdentityModel.Clients.ActiveDirectory** NuGet package so you'll need to add a reference to that package in your project.
53+
The following code uses objects from the **Microsoft.Identity.Client** NuGet package so you'll need to add a reference to that package in your project.
5454

5555
> [!IMPORTANT]
5656
> The [Microsoft.IdentityModel.Clients.ActiveDirectory](https://www.nuget.org/packages/Microsoft.IdentityModel.Clients.ActiveDirectory) NuGet package and Azure AD Authentication Library (ADAL) have been deprecated. No new features have been added since June 30, 2020. We strongly encourage you to upgrade, see the [migration guide](../../../../active-directory/develop/msal-migration.md) for more details.
5757
5858
Open the NuGet Package Manager Console from **Tools -> NuGet Package Manager -> Package Manager Console** and run the following command:
5959

6060
```powershell
61-
Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory -Version 5.2.0
61+
Install-Package Microsoft.Identity.Client -Version 4.42.0
6262
```
6363

6464
### Update the controller to acquire the token
6565

6666
Open _Controllers\HomeController.cs_, and add the following code after the _using_ statements at the top of the file.
6767

6868
```csharp
69-
using Microsoft.IdentityModel.Clients.ActiveDirectory;
69+
using Microsoft.Identity.Client;
7070
```
7171

7272
Now, we'll configure the controller to obtain the Azure AD values from _secrets.json_. At the top of the _HomeController_ class, after ```public class HomeController : Controller {```, add the following code.
@@ -77,6 +77,21 @@ private readonly string ClientId; // Azure AD ApplicationId
7777
private readonly string ClientSecret; // Azure AD Application Service Principal password
7878
private readonly string Subdomain; // Immersive Reader resource subdomain (resource 'Name' if the resource was created in the Azure portal, or 'CustomSubDomain' option if the resource was created with Azure CLI PowerShell. Check the Azure portal for the subdomain on the Endpoint in the resource Overview page, for example, 'https://[SUBDOMAIN].cognitiveservices.azure.com/')
7979
80+
private IConfidentialClientApplication _confidentialClientApplication;
81+
private IConfidentialClientApplication ConfidentialClientApplication
82+
{
83+
get {
84+
if (_confidentialClientApplication == null) {
85+
_confidentialClientApplication = ConfidentialClientApplicationBuilder.Create(ClientId)
86+
.WithClientSecret(ClientSecret)
87+
.WithAuthority($"https://login.windows.net/{TenantId}")
88+
.Build();
89+
}
90+
91+
return _confidentialClientApplication;
92+
}
93+
}
94+
8095
public HomeController(Microsoft.Extensions.Configuration.IConfiguration configuration)
8196
{
8297
TenantId = configuration["TenantId"];
@@ -108,15 +123,14 @@ public HomeController(Microsoft.Extensions.Configuration.IConfiguration configur
108123
/// <summary>
109124
/// Get an Azure AD authentication token
110125
/// </summary>
111-
private async Task<string> GetTokenAsync()
126+
public async Task<string> GetTokenAsync()
112127
{
113-
string authority = $"https://login.windows.net/{TenantId}";
114128
const string resource = "https://cognitiveservices.azure.com/";
115129

116-
AuthenticationContext authContext = new AuthenticationContext(authority);
117-
ClientCredential clientCredential = new ClientCredential(ClientId, ClientSecret);
118-
119-
AuthenticationResult authResult = await authContext.AcquireTokenAsync(resource, clientCredential);
130+
var authResult = await ConfidentialClientApplication.AcquireTokenForClient(
131+
new[] { $"{resource}/.default" })
132+
.ExecuteAsync()
133+
.ConfigureAwait(false);
120134

121135
return authResult.AccessToken;
122136
}
@@ -132,7 +146,7 @@ public async Task<JsonResult> GetTokenAndSubdomain()
132146
}
133147
catch (Exception e)
134148
{
135-
string message = "Unable to acquire Azure AD token. Check the debugger for more information.";
149+
string message = "Unable to acquire Azure AD token. Check the console for more information.";
136150
Debug.WriteLine(message, e);
137151
return new JsonResult(new { error = message });
138152
}
-17.7 KB
Loading
-7.22 KB
Loading
-19.5 KB
Loading

0 commit comments

Comments
 (0)