Skip to content

Commit 5a1063c

Browse files
authored
Merge pull request #118669 from longb1/patch-1
Update authentication.md
2 parents 9b9afe4 + 0e7fccc commit 5a1063c

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

articles/ai-services/authentication.md

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,12 @@ Now that you have a custom subdomain associated with your resource, you're going
216216
New-AzADServicePrincipal -ApplicationId <APPLICATION_ID>
217217
```
218218

219-
>[!NOTE]
219+
> [!NOTE]
220220
> If you register an application in the Azure portal, this step is completed for you.
221221
222222
3. The last step is to [assign the "Cognitive Services User" role](/powershell/module/az.Resources/New-azRoleAssignment) to the service principal (scoped to the resource). By assigning a role, you're granting service principal access to this resource. You can grant the same service principal access to multiple resources in your subscription.
223-
>[!NOTE]
223+
224+
> [!NOTE]
224225
> The ObjectId of the service principal is used, not the ObjectId for the application.
225226
> The ACCOUNT_ID will be the Azure resource Id of the Azure AI services account you created. You can find Azure resource Id from "properties" of the resource in Azure portal.
226227
@@ -239,32 +240,31 @@ In this sample, a password is used to authenticate the service principal. The to
239240
```
240241

241242
2. Get a token:
242-
> [!NOTE]
243-
> If you're using Azure Cloud Shell, the `SecureClientSecret` class isn't available.
244-
245-
#### [PowerShell](#tab/powershell)
246243
```powershell-interactive
247-
$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList "https://login.windows.net/<TENANT_ID>"
248-
$secureSecretObject = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.SecureClientSecret" -ArgumentList $SecureStringPassword
249-
$clientCredential = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential" -ArgumentList $app.ApplicationId, $secureSecretObject
250-
$token=$authContext.AcquireTokenAsync("https://cognitiveservices.azure.com/", $clientCredential).Result
251-
$token
252-
```
244+
$tenantId = $context.Tenant.Id
245+
$clientId = $app.ApplicationId
246+
$clientSecret = "<YOUR_PASSWORD>"
247+
$resourceUrl = "https://cognitiveservices.azure.com/"
253248
254-
#### [Azure Cloud Shell](#tab/azure-cloud-shell)
255-
```Azure Cloud Shell
256-
$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList "https://login.windows.net/<TENANT_ID>"
257-
$clientCredential = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential" -ArgumentList $app.ApplicationId, <YOUR_PASSWORD>
258-
$token=$authContext.AcquireTokenAsync("https://cognitiveservices.azure.com/", $clientCredential).Result
259-
$token
260-
```
261-
262-
---
249+
$tokenEndpoint = "https://login.microsoftonline.com/$tenantId/oauth2/token"
250+
$body = @{
251+
grant_type = "client_credentials"
252+
client_id = $clientId
253+
client_secret = $clientSecret
254+
resource = $resourceUrl
255+
}
256+
257+
$responseToken = Invoke-RestMethod -Uri $tokenEndpoint -Method Post -Body $body
258+
$accessToken = $responseToken.access_token
259+
```
263260

261+
> [!NOTE]
262+
> Anytime you use passwords in a script, the most secure option is to use the PowerShell Secrets Management module and integrate with a solution such as Azure KeyVault.
263+
264264
3. Call the Computer Vision API:
265265
```powershell-interactive
266266
$url = $account.Endpoint+"vision/v1.0/models"
267-
$result = Invoke-RestMethod -Uri $url -Method Get -Headers @{"Authorization"=$token.CreateAuthorizationHeader()} -Verbose
267+
$result = Invoke-RestMethod -Uri $url -Method Get -Headers @{"Authorization"="Bearer $accessToken"} -Verbose
268268
$result | ConvertTo-Json
269269
```
270270

0 commit comments

Comments
 (0)