Skip to content

Commit d044cce

Browse files
authored
Merge pull request #252640 from PatrickFarley/cogserv-updates
[ai-svcs] Cogserv updates
2 parents e38801b + a32e372 commit d044cce

File tree

4 files changed

+157
-132
lines changed

4 files changed

+157
-132
lines changed

articles/ai-services/TOC.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@
7979
href: security-features.md
8080
- name: Authenticate requests
8181
href: authentication.md
82+
- name: Disable local authentication
83+
href: disable-local-auth.md
8284
- name: Rotate keys
8385
href: rotate-keys.md
8486
- name: Use environment variables

articles/ai-services/authentication.md

Lines changed: 125 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Each request to an Azure AI service must include an authentication header. This
1818

1919
* Authenticate with a [single-service](#authenticate-with-a-single-service-resource-key) or [multi-service](#authenticate-with-a-multi-service-resource-key) resource key
2020
* Authenticate with a [token](#authenticate-with-an-access-token)
21-
* Authenticate with [Azure Active Directory (AAD)](#authenticate-with-an-access-token)
21+
* Authenticate with [Azure Active Directory (AAD)](#authenticate-with-azure-active-directory)
2222

2323
## Prerequisites
2424

@@ -40,7 +40,7 @@ Let's quickly review the authentication headers available for use with Azure AI
4040

4141
The first option is to authenticate a request with a resource key for a specific service, like Translator. The keys are available in the Azure portal for each resource that you've created. To use a resource key to authenticate a request, it must be passed along as the `Ocp-Apim-Subscription-Key` header.
4242

43-
These sample requests demonstrates how to use the `Ocp-Apim-Subscription-Key` header. Keep in mind, when using this sample you'll need to include a valid resource key.
43+
These sample requests demonstrate how to use the `Ocp-Apim-Subscription-Key` header. Keep in mind, when using this sample you'll need to include a valid resource key.
4444

4545
This is a sample call to the Translator service:
4646
```cURL
@@ -160,7 +160,129 @@ curl -X POST 'https://api.cognitive.microsofttranslator.com/translate?api-versio
160160
--data-raw '[{ "text": "How much for the cup of coffee?" }]' | json_pp
161161
```
162162

163-
[!INCLUDE [](../../includes/cognitive-services-azure-active-directory-authentication.md)]
163+
## Authenticate with Azure Active Directory
164+
165+
> [!IMPORTANT]
166+
> Azure AD authentication always needs to be used together with custom subdomain name of your Azure resource. [Regional endpoints](./cognitive-services-custom-subdomains.md#is-there-a-list-of-regional-endpoints) do not support Azure AD authentication.
167+
168+
In the previous sections, we showed you how to authenticate against Azure AI services using a single-service or multi-service subscription key. While these keys provide a quick and easy path to start development, they fall short in more complex scenarios that require Azure [role-based access control (Azure RBAC)](../../articles/role-based-access-control/overview.md). Let's take a look at what's required to authenticate using Azure Active Directory (Azure AD).
169+
170+
In the following sections, you'll use either the Azure Cloud Shell environment or the Azure CLI to create a subdomain, assign roles, and obtain a bearer token to call the Azure AI services. If you get stuck, links are provided in each section with all available options for each command in Azure Cloud Shell/Azure CLI.
171+
172+
> [!IMPORTANT]
173+
> If your organization is doing authentication through Azure AD, you should [disable local authentication](./disable-local-auth.md) (authentication with keys) so that users in the organization must always use Azure AD.
174+
175+
### Create a resource with a custom subdomain
176+
177+
The first step is to create a custom subdomain. If you want to use an existing Azure AI services resource which does not have custom subdomain name, follow the instructions in [Azure AI services custom subdomains](./cognitive-services-custom-subdomains.md#how-does-this-impact-existing-resources) to enable custom subdomain for your resource.
178+
179+
1. Start by opening the Azure Cloud Shell. Then [select a subscription](/powershell/module/az.accounts/set-azcontext):
180+
181+
```powershell-interactive
182+
Set-AzContext -SubscriptionName <SubscriptionName>
183+
```
184+
185+
2. Next, [create an Azure AI services resource](/powershell/module/az.cognitiveservices/new-azcognitiveservicesaccount) with a custom subdomain. The subdomain name needs to be globally unique and cannot include special characters, such as: ".", "!", ",".
186+
187+
```powershell-interactive
188+
$account = New-AzCognitiveServicesAccount -ResourceGroupName <RESOURCE_GROUP_NAME> -name <ACCOUNT_NAME> -Type <ACCOUNT_TYPE> -SkuName <SUBSCRIPTION_TYPE> -Location <REGION> -CustomSubdomainName <UNIQUE_SUBDOMAIN>
189+
```
190+
191+
3. If successful, the **Endpoint** should show the subdomain name unique to your resource.
192+
193+
194+
### Assign a role to a service principal
195+
196+
Now that you have a custom subdomain associated with your resource, you're going to need to assign a role to a service principal.
197+
198+
> [!NOTE]
199+
> Keep in mind that Azure role assignments may take up to five minutes to propagate.
200+
201+
1. First, let's register an [Azure AD application](/powershell/module/Az.Resources/New-AzADApplication).
202+
203+
```powershell-interactive
204+
$SecureStringPassword = ConvertTo-SecureString -String <YOUR_PASSWORD> -AsPlainText -Force
205+
206+
$app = New-AzureADApplication -DisplayName <APP_DISPLAY_NAME> -IdentifierUris <APP_URIS> -PasswordCredentials $SecureStringPassword
207+
```
208+
209+
You're going to need the **ApplicationId** in the next step.
210+
211+
2. Next, you need to [create a service principal](/powershell/module/az.resources/new-azadserviceprincipal) for the Azure AD application.
212+
213+
```powershell-interactive
214+
New-AzADServicePrincipal -ApplicationId <APPLICATION_ID>
215+
```
216+
217+
>[!NOTE]
218+
> If you register an application in the Azure portal, this step is completed for you.
219+
220+
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.
221+
>[!NOTE]
222+
> The ObjectId of the service principal is used, not the ObjectId for the application.
223+
> 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.
224+
225+
```azurecli-interactive
226+
New-AzRoleAssignment -ObjectId <SERVICE_PRINCIPAL_OBJECTID> -Scope <ACCOUNT_ID> -RoleDefinitionName "Cognitive Services User"
227+
```
228+
229+
### Sample request
230+
231+
In this sample, a password is used to authenticate the service principal. The token provided is then used to call the Computer Vision API.
232+
233+
1. Get your **TenantId**:
234+
```powershell-interactive
235+
$context=Get-AzContext
236+
$context.Tenant.Id
237+
```
238+
239+
2. Get a token:
240+
> [!NOTE]
241+
> If you're using Azure Cloud Shell, the `SecureClientSecret` class isn't available.
242+
243+
#### [PowerShell](#tab/powershell)
244+
```powershell-interactive
245+
$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList "https://login.windows.net/<TENANT_ID>"
246+
$secureSecretObject = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.SecureClientSecret" -ArgumentList $SecureStringPassword
247+
$clientCredential = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential" -ArgumentList $app.ApplicationId, $secureSecretObject
248+
$token=$authContext.AcquireTokenAsync("https://cognitiveservices.azure.com/", $clientCredential).Result
249+
$token
250+
```
251+
252+
#### [Azure Cloud Shell](#tab/azure-cloud-shell)
253+
```Azure Cloud Shell
254+
$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList "https://login.windows.net/<TENANT_ID>"
255+
$clientCredential = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential" -ArgumentList $app.ApplicationId, <YOUR_PASSWORD>
256+
$token=$authContext.AcquireTokenAsync("https://cognitiveservices.azure.com/", $clientCredential).Result
257+
$token
258+
```
259+
260+
---
261+
262+
3. Call the Computer Vision API:
263+
```powershell-interactive
264+
$url = $account.Endpoint+"vision/v1.0/models"
265+
$result = Invoke-RestMethod -Uri $url -Method Get -Headers @{"Authorization"=$token.CreateAuthorizationHeader()} -Verbose
266+
$result | ConvertTo-Json
267+
```
268+
269+
Alternatively, the service principal can be authenticated with a certificate. Besides service principal, user principal is also supported by having permissions delegated through another Azure AD application. In this case, instead of passwords or certificates, users would be prompted for two-factor authentication when acquiring token.
270+
271+
## Authorize access to managed identities
272+
273+
Azure AI services support Azure Active Directory (Azure AD) authentication with [managed identities for Azure resources](../../articles/active-directory/managed-identities-azure-resources/overview.md). Managed identities for Azure resources can authorize access to Azure AI services resources using Azure AD credentials from applications running in Azure virtual machines (VMs), function apps, virtual machine scale sets, and other services. By using managed identities for Azure resources together with Azure AD authentication, you can avoid storing credentials with your applications that run in the cloud.
274+
275+
### Enable managed identities on a VM
276+
277+
Before you can use managed identities for Azure resources to authorize access to Azure AI services resources from your VM, you must enable managed identities for Azure resources on the VM. To learn how to enable managed identities for Azure Resources, see:
278+
279+
- [Azure portal](../../articles/active-directory/managed-identities-azure-resources/qs-configure-portal-windows-vm.md)
280+
- [Azure PowerShell](../../articles/active-directory/managed-identities-azure-resources/qs-configure-powershell-windows-vm.md)
281+
- [Azure CLI](../../articles/active-directory/managed-identities-azure-resources/qs-configure-cli-windows-vm.md)
282+
- [Azure Resource Manager template](../../articles/active-directory/managed-identities-azure-resources/qs-configure-template-windows-vm.md)
283+
- [Azure Resource Manager client libraries](../../articles/active-directory/managed-identities-azure-resources/qs-configure-sdk-windows-vm.md)
284+
285+
For more information about managed identities, see [Managed identities for Azure resources](../../articles/active-directory/managed-identities-azure-resources/overview.md).
164286

165287
## Use Azure key vault to securely access credentials
166288

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
title: Disable local authentication in Azure AI Services
3+
titleSuffix: Azure AI services
4+
description: "This article describes disabling local authentication in Azure AI Services."
5+
services: cognitive-services
6+
author: PatrickFarley
7+
manager: nitinme
8+
ms.service: cognitive-services
9+
ms.topic: how-to
10+
ms.date: 09/22/2023
11+
ms.author: pafarley
12+
---
13+
14+
# Disable local authentication in Azure AI Services
15+
16+
Azure AI Services provides Azure Active Directory (Azure AD) authentication support for all resources. This gives organizations control to disable local authentication methods and enforce Azure AD authentication. This feature provides you with seamless integration when you require centralized control and management of identities and resource credentials.
17+
18+
You can disable local authentication using the Azure policy [Cognitive Services accounts should have local authentication methods disabled](https://ms.portal.azure.com/#view/Microsoft_Azure_Policy/PolicyDetailBlade/definitionId/%2Fproviders%2FMicrosoft.Authorization%2FpolicyDefinitions%2F71ef260a-8f18-47b7-abcb-62d0673d94dc). You can set it at the subscription level or resource group level to enforce the policy for a group of services.
19+
20+
Disabling local authentication doesn't take effect immediately. Allow a few minutes for the service to block future authentication requests.
21+
22+
You can use PowerShell to determine whether the local authentication policy is currently enabled. First sign in with the `Connect-AzAccount` command. Then use the cmdlet **[Get-AzCognitiveServicesAccount](/powershell/module/az.cognitiveservices/get-azcognitiveservicesaccount)** to retrieve your resource, and check the property `DisableLocalAuth`. A value of `true` means local authentication is disabled.
23+
24+
25+
## Re-enable local authentication
26+
27+
To enable local authentication, execute the PowerShell cmdlet **[Set-AzCognitiveServicesAccount](/powershell/module/az.cognitiveservices/set-azcognitiveservicesaccount)** with the parameter `-DisableLocalAuth false`.  Allow a few minutes for the service to accept the change to allow local authentication requests.
28+
29+
## Next steps
30+
- [Authenticate requests to Azure AI services](./authentication.md)

0 commit comments

Comments
 (0)