Skip to content

Commit 368652d

Browse files
Azure Docs - ADAL to MSAL migration for Immersive Reader code sample
1 parent 70c4ac5 commit 368652d

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

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,22 +48,22 @@ 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
Open the NuGet Package Manager Console from **Tools -> NuGet Package Manager -> Package Manager Console** and run the following command:
5656

5757
```powershell
58-
Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory -Version 5.2.0
58+
Install-Package Microsoft.Identity.Client -Version 4.42.0
5959
```
6060

6161
### Update the controller to acquire the token
6262

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

6565
```csharp
66-
using Microsoft.IdentityModel.Clients.ActiveDirectory;
66+
using Microsoft.Identity.Client;
6767
```
6868

6969
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.
@@ -74,6 +74,21 @@ private readonly string ClientId; // Azure AD ApplicationId
7474
private readonly string ClientSecret; // Azure AD Application Service Principal password
7575
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/')
7676
77+
private IConfidentialClientApplication _confidentialClientApplication;
78+
private IConfidentialClientApplication ConfidentialClientApplication
79+
{
80+
get {
81+
if (_confidentialClientApplication == null) {
82+
_confidentialClientApplication = ConfidentialClientApplicationBuilder.Create(ClientId)
83+
.WithClientSecret(ClientSecret)
84+
.WithAuthority($"https://login.windows.net/{TenantId}")
85+
.Build();
86+
}
87+
88+
return _confidentialClientApplication;
89+
}
90+
}
91+
7792
public HomeController(Microsoft.Extensions.Configuration.IConfiguration configuration)
7893
{
7994
TenantId = configuration["TenantId"];
@@ -105,15 +120,14 @@ public HomeController(Microsoft.Extensions.Configuration.IConfiguration configur
105120
/// <summary>
106121
/// Get an Azure AD authentication token
107122
/// </summary>
108-
private async Task<string> GetTokenAsync()
123+
public async Task<string> GetTokenAsync()
109124
{
110-
string authority = $"https://login.windows.net/{TenantId}";
111125
const string resource = "https://cognitiveservices.azure.com/";
112126

113-
AuthenticationContext authContext = new AuthenticationContext(authority);
114-
ClientCredential clientCredential = new ClientCredential(ClientId, ClientSecret);
115-
116-
AuthenticationResult authResult = await authContext.AcquireTokenAsync(resource, clientCredential);
127+
var authResult = await ConfidentialClientApplication.AcquireTokenForClient(
128+
new[] { $"{resource}/.default" })
129+
.ExecuteAsync()
130+
.ConfigureAwait(false);
117131

118132
return authResult.AccessToken;
119133
}
@@ -129,7 +143,7 @@ public async Task<JsonResult> GetTokenAndSubdomain()
129143
}
130144
catch (Exception e)
131145
{
132-
string message = "Unable to acquire Azure AD token. Check the debugger for more information.";
146+
string message = "Unable to acquire Azure AD token. Check the console for more information.";
133147
Debug.WriteLine(message, e);
134148
return new JsonResult(new { error = message });
135149
}
-17.7 KB
Loading
-7.22 KB
Loading
-19.5 KB
Loading

0 commit comments

Comments
 (0)