Skip to content

Commit 3dd3332

Browse files
committed
move include to host doc
1 parent 153d9d5 commit 3dd3332

File tree

2 files changed

+121
-131
lines changed

2 files changed

+121
-131
lines changed

articles/ai-services/authentication.md

Lines changed: 121 additions & 2 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

@@ -160,7 +160,126 @@ 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](../articles/cognitive-services/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+
### Create a resource with a custom subdomain
173+
174+
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](../articles/cognitive-services/cognitive-services-custom-subdomains.md#how-does-this-impact-existing-resources) to enable custom subdomain for your resource.
175+
176+
1. Start by opening the Azure Cloud Shell. Then [select a subscription](/powershell/module/az.accounts/set-azcontext):
177+
178+
```powershell-interactive
179+
Set-AzContext -SubscriptionName <SubscriptionName>
180+
```
181+
182+
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: ".", "!", ",".
183+
184+
```powershell-interactive
185+
$account = New-AzCognitiveServicesAccount -ResourceGroupName <RESOURCE_GROUP_NAME> -name <ACCOUNT_NAME> -Type <ACCOUNT_TYPE> -SkuName <SUBSCRIPTION_TYPE> -Location <REGION> -CustomSubdomainName <UNIQUE_SUBDOMAIN>
186+
```
187+
188+
3. If successful, the **Endpoint** should show the subdomain name unique to your resource.
189+
190+
191+
### Assign a role to a service principal
192+
193+
Now that you have a custom subdomain associated with your resource, you're going to need to assign a role to a service principal.
194+
195+
> [!NOTE]
196+
> Keep in mind that Azure role assignments may take up to five minutes to propagate.
197+
198+
1. First, let's register an [Azure AD application](/powershell/module/Az.Resources/New-AzADApplication).
199+
200+
```powershell-interactive
201+
$SecureStringPassword = ConvertTo-SecureString -String <YOUR_PASSWORD> -AsPlainText -Force
202+
203+
$app = New-AzureADApplication -DisplayName <APP_DISPLAY_NAME> -IdentifierUris <APP_URIS> -PasswordCredentials $SecureStringPassword
204+
```
205+
206+
You're going to need the **ApplicationId** in the next step.
207+
208+
2. Next, you need to [create a service principal](/powershell/module/az.resources/new-azadserviceprincipal) for the Azure AD application.
209+
210+
```powershell-interactive
211+
New-AzADServicePrincipal -ApplicationId <APPLICATION_ID>
212+
```
213+
214+
>[!NOTE]
215+
> If you register an application in the Azure portal, this step is completed for you.
216+
217+
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.
218+
>[!NOTE]
219+
> The ObjectId of the service principal is used, not the ObjectId for the application.
220+
> 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.
221+
222+
```azurecli-interactive
223+
New-AzRoleAssignment -ObjectId <SERVICE_PRINCIPAL_OBJECTID> -Scope <ACCOUNT_ID> -RoleDefinitionName "Cognitive Services User"
224+
```
225+
226+
### Sample request
227+
228+
In this sample, a password is used to authenticate the service principal. The token provided is then used to call the Computer Vision API.
229+
230+
1. Get your **TenantId**:
231+
```powershell-interactive
232+
$context=Get-AzContext
233+
$context.Tenant.Id
234+
```
235+
236+
2. Get a token:
237+
> [!NOTE]
238+
> If you're using Azure Cloud Shell, the `SecureClientSecret` class isn't available.
239+
240+
#### [PowerShell](#tab/powershell)
241+
```powershell-interactive
242+
$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList "https://login.windows.net/<TENANT_ID>"
243+
$secureSecretObject = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.SecureClientSecret" -ArgumentList $SecureStringPassword
244+
$clientCredential = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential" -ArgumentList $app.ApplicationId, $secureSecretObject
245+
$token=$authContext.AcquireTokenAsync("https://cognitiveservices.azure.com/", $clientCredential).Result
246+
$token
247+
```
248+
249+
#### [Azure Cloud Shell](#tab/azure-cloud-shell)
250+
```Azure Cloud Shell
251+
$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList "https://login.windows.net/<TENANT_ID>"
252+
$clientCredential = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential" -ArgumentList $app.ApplicationId, <YOUR_PASSWORD>
253+
$token=$authContext.AcquireTokenAsync("https://cognitiveservices.azure.com/", $clientCredential).Result
254+
$token
255+
```
256+
257+
---
258+
259+
3. Call the Computer Vision API:
260+
```powershell-interactive
261+
$url = $account.Endpoint+"vision/v1.0/models"
262+
$result = Invoke-RestMethod -Uri $url -Method Get -Headers @{"Authorization"=$token.CreateAuthorizationHeader()} -Verbose
263+
$result | ConvertTo-Json
264+
```
265+
266+
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.
267+
268+
## Authorize access to managed identities
269+
270+
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.
271+
272+
### Enable managed identities on a VM
273+
274+
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:
275+
276+
- [Azure portal](../articles/active-directory/managed-identities-azure-resources/qs-configure-portal-windows-vm.md)
277+
- [Azure PowerShell](../articles/active-directory/managed-identities-azure-resources/qs-configure-powershell-windows-vm.md)
278+
- [Azure CLI](../articles/active-directory/managed-identities-azure-resources/qs-configure-cli-windows-vm.md)
279+
- [Azure Resource Manager template](../articles/active-directory/managed-identities-azure-resources/qs-configure-template-windows-vm.md)
280+
- [Azure Resource Manager client libraries](../articles/active-directory/managed-identities-azure-resources/qs-configure-sdk-windows-vm.md)
281+
282+
For more information about managed identities, see [Managed identities for Azure resources](../articles/active-directory/managed-identities-azure-resources/overview.md).
164283

165284
## Use Azure key vault to securely access credentials
166285

includes/cognitive-services-azure-active-directory-authentication.md

Lines changed: 0 additions & 129 deletions
This file was deleted.

0 commit comments

Comments
 (0)