You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/ai/how-to/app-service-aoai-auth.md
+57-23Lines changed: 57 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,34 +1,30 @@
1
1
---
2
-
title: "Authenticate and Authorize App Service to Azure OpenAI using Microsoft Entra and the Semantic Kernel SDK"
3
-
description: "Learn how to authenticate and authorize your app service application to an Azure OpenAI resource by using Microsoft Entra managed identities and the Semantic Kernel SDK for .NET."
2
+
title: "Authenticate an Azure hosted .NET app to Azure OpenAI using Microsoft Entra ID"
3
+
description: "Learn how to authenticate your Azure hosted .NET app to an Azure OpenAI resource using Microsoft Entra ID."
4
4
author: haywoodsloan
5
5
ms.topic: how-to
6
6
ms.custom: devx-track-azurecli
7
-
ms.date: 11/24/2024
7
+
ms.date: 01/29/2025
8
8
zone_pivot_groups: azure-interface
9
9
#customer intent: As a .NET developer, I want authenticate and authorize my App Service to Azure OpenAI by using Microsoft Entra so that I can securely use AI in my .NET application.
10
10
---
11
11
12
-
# Authenticate an AI app hosted on Azure App Service to Azure OpenAI using Microsoft Entra ID
12
+
# Authenticate to Azure OpenAI from an Azure hosted app using Microsoft Entra ID
13
13
14
-
This article demonstrates how to use [Microsoft Entra ID managed identities](/azure/app-service/overview-managed-identity)to authenticate and authorize an App Service application to an Azure OpenAI resource.
14
+
This article demonstrates how to use [Microsoft Entra ID managed identities](/azure/app-service/overview-managed-identity)and the [Microsoft.Extensions.AI library](/dotnet/ai/ai-extensions) to authenticate an Azure hosted app to an Azure OpenAI resource.
15
15
16
-
This article also demonstrates how to use the [Semantic Kernel SDK](/semantic-kernel/overview) to easily implement Microsoft Entra authentication in your .NET application.
17
-
18
-
By using a managed identity from Microsoft Entra, your App Service application can easily access protected Azure OpenAI resources without having to manually provision or rotate any secrets.
16
+
A managed identity from Microsoft Entra ID allows your app to easily access other Microsoft Entra protected resources such as Azure OpenAI. The identity is managed by the Azure platform and doesn't require you to provision, manage, or rotate any secrets.
19
17
20
18
## Prerequisites
21
19
22
20
* An Azure account that has an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
*[Create and deploy an Azure OpenAI Service resource](/azure/ai-services/openai/how-to/create-resource)
27
23
*[Create and deploy a .NET application to App Service](/azure/app-service/quickstart-dotnetcore)
28
24
29
25
## Add a managed identity to App Service
30
26
31
-
Your application can be granted two types of identities:
27
+
Managed identities provide an automatically managed identity in Microsoft Entra ID for applications to use when connecting to resources that support Microsoft Entra authentication. Applications can use managed identities to obtain Microsoft Entra tokens without having to manage any credentials. Your application can be assigned two types of identities:
32
28
33
29
* A **system-assigned identity** is tied to your application and is deleted if your app is deleted. An app can have only one system-assigned identity.
34
30
* A **user-assigned identity** is a standalone Azure resource that can be assigned to your app. An app can have multiple user-assigned identities.
@@ -41,6 +37,11 @@ Your application can be granted two types of identities:
41
37
1. Select **Identity**.
42
38
1. On the **System assigned** tab, toggle *Status* to **On**, and then select **Save**.
43
39
40
+
:::image type="content" source="../media/azure-hosted-apps/system-assigned-managed-identity-in-azure-portal.png" alt-text="A screenshot showing how to add a system assigned managed identity to an app.":::
41
+
42
+
> [!NOTE]
43
+
> The preceding screenshot demonstrates this process on an Azure App Service, but the steps are similar on other hosts such as Azure Container Apps.
44
+
44
45
## [User-assigned](#tab/user-assigned)
45
46
46
47
To add a user-assigned identity to your app, create the identity, and then add its resource identifier to your app config.
@@ -54,6 +55,11 @@ To add a user-assigned identity to your app, create the identity, and then add i
54
55
> [!IMPORTANT]
55
56
> After you select **Add**, the app restarts.
56
57
58
+
:::image type="content" source="../media/azure-hosted-apps/user-assigned-managed-identity-in-azure-portal.png" alt-text="A screenshot showing how to add a system assigned managed identity to an app.":::
59
+
60
+
> [!NOTE]
61
+
> The preceding screenshot demonstrates this process on an Azure App Service, but the steps are similar on other hosts such as Azure Container Apps.
## Add an Azure OpenAI user role to your managed identity
95
+
## Add an Azure OpenAI user role to the identity
90
96
91
97
:::zone target="docs" pivot="azure-portal"
92
98
93
99
1. In the [Azure Portal](https://aka.ms/azureportal), navigate to the scope that you want to grant **Azure OpenAI** access to. The scope can be a **Management group**, **Subscription**, **Resource group**, or a specific **Azure OpenAI** resource.
94
100
1. In the left navigation pane, select **Access control (IAM)**.
95
101
1. Select **Add**, then select **Add role assignment**.
102
+
103
+
:::image type="content" source="../media/azure-hosted-apps/add-entra-role.png" alt-text="A screenshot showing how to add an RBAC role.":::
104
+
96
105
1. On the **Role** tab, select the **Cognitive Services OpenAI User** role.
97
106
1. On the **Members** tab, select the managed identity.
98
107
1. On the **Review + assign** tab, select **Review + assign** to assign the role.
## Implement token-based authentication using Semantic Kernel SDK
151
+
## Implement identity authentication in your app code
152
+
153
+
1. Add the following NuGet packages to your app:
154
+
155
+
```dotnetcli
156
+
dotnet add package Azure.Identity
157
+
dotnet add package Azure.AI.OpenAI
158
+
dotnet add package Microsoft.Extensions.Azure
159
+
dotnet add package Microsoft.Extensions.AI
160
+
dotnet add package Microsoft.Extensions.AI.OpenAI
161
+
```
162
+
163
+
The preceding packages each handle the following concerns for this scenario:
164
+
165
+
- **[Azure.Identity](https://www.nuget.org/packages/Azure.Identity)**: Provides core functionality to work with Microsoft Entra ID
166
+
- **[Azure.AI.OpenAI](https://www.nuget.org/packages/Azure.AI.OpenAI)**: Enables your app to interface with the Azure OpenAI service
167
+
- **[Microsoft.Extensions.Azure](https://www.nuget.org/packages/Microsoft.Extensions.Azure)**: Provides helper extensions to register services for dependency injection
168
+
- **[Microsoft.Extensions.AI](https://www.nuget.org/packages/Microsoft.Extensions.AI)**: Provides AI abstractions for common AI tasks
169
+
- **[Microsoft.Extensions.AI.OpenAI](https://www.nuget.org/packages/Microsoft.Extensions.AI.OpenAI)**: Enables you to use OpenAI service types as AI abstractions provided by **Microsoft.Extensions.AI**
170
+
171
+
1. In the `Program.cs` file of your app, create a `DefaultAzureCredential` object to discover and configure available credentials:
139
172
140
-
1. Initialize a `DefaultAzureCredential` object to assume your app's managed identity:
> Learn more about ASP.NET Core dependency injection and how to register other AI services types in the Azure SDK for .NET [dependency injection](/dotnet/azure/sdk/dependency-injection) documentation.
0 commit comments