Skip to content

Commit 5b044fd

Browse files
committed
updating with managed identity examples
1 parent 9ca0037 commit 5b044fd

File tree

4 files changed

+120
-3
lines changed

4 files changed

+120
-3
lines changed

articles/app-service/includes/deploy-intelligent-apps/deploy-intelligent-apps-linux-dotnet-pivot.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,43 @@ Next, we need to add the new page to the navigation so we can navigate to the se
6060

6161
After the Navigation is updated, we can start preparing to build the OpenAI client to handle our requests.
6262

63+
### Secure your app with managed identity
64+
65+
Although optional, it's highly recommended to secure your application using [managed identity](../../../overview-managed-identity.md) to authenticate your app to your Azure OpenAI resource. Skip this step if you are not using Azure OpenAI. This enables your application to access the Azure OpenAI resource without needing to manage API keys.
66+
67+
Follow the steps below to secure your application:
68+
69+
Add the identity package `Azure.Identity`. This package enables using Azure credentials in your app. Install the package using Nuget package manager and add the using statement to the top of the OpenAI.razor file.
70+
71+
```c#
72+
@using Azure.Identity
73+
```
74+
75+
Next, include the default Azure credentials in the chat completions options
76+
77+
```c#
78+
var kernel = Kernel.CreateBuilder()
79+
.AddAzureOpenAIChatCompletion(
80+
deploymentName: deploymentName,
81+
endpoint: endpoint,
82+
credentials: new DefaultAzureCredential()
83+
)
84+
.Build();
85+
```
86+
87+
Once the credentials are added to the application, you’ll then need to enable managed identity in your application and grant access to the resource.
88+
89+
1. In your web app resource, navigate to the **Identity** blade and turn on **System assigned** and click **Save**
90+
2. Once System assigned identity is turned on, it will register the web app with Microsoft Entra ID and the web app can be granted permissions to access protected resources.
91+
3. Go to your Azure OpenAI resource and navigate to the **Access control (IAM)** blade on the left pane.
92+
4. Find the Grant access to this resource card and click on **Add role assignment**
93+
5. Search for the **Cognitive Services OpenAI User** role and click **Next**
94+
6. On the **Members** tab, find **Assign access to** and choose the **Managed identity** option
95+
7. Next, click on **+Select Members** and find your web app
96+
8. Click **Review + assign**
97+
98+
Your web app is now added as a cognitive service OpenAI user and can communicate to your Azure OpenAI resource.
99+
63100
### API keys and endpoints
64101

65102
In order to make calls to OpenAI with your client, you need to first grab the Keys and Endpoint values from Azure OpenAI, or OpenAI and add them as secrets for use in your application. Retrieve and save the values for later use.
@@ -315,7 +352,6 @@ Now save the application and follow the next steps to deploy it to App Service.
315352

316353
If you have followed the steps above, you're ready to deploy to App Service. If you run into any issues remember that you need to have done the following: grant your app access to your Key Vault, add the app settings with key vault references as your values. App Service resolves the app settings in your application that match what you've added in the portal.
317354

318-
319355
### Authentication
320356

321357
Although optional, it's highly recommended that you also add authentication to your web app when using an Azure OpenAI or OpenAI service. This can add a level of security with no other code. Learn how to enable authentication for your web app [here](../../scenario-secure-app-authentication-app-service.md).

articles/app-service/includes/deploy-intelligent-apps/deploy-intelligent-apps-linux-java-pivot.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,46 @@ For this Spring Boot application, we are building off the [quickstart](../../qui
2929
}
3030
```
3131

32+
### Secure your app with managed identity
33+
34+
Although optional, it's highly recommended to secure your application using [managed identity](../../../overview-managed-identity.md) to authenticate your app to your Azure OpenAI resource. Skip this step if you are not using Azure OpenAI. This enables your application to access the Azure OpenAI resource without needing to manage API keys.
35+
36+
Follow the steps below to secure your application:
37+
38+
Add the Azure OpenAI dependency package. This package enables using Azure credentials in your app.
39+
40+
```java
41+
<dependency>
42+
<groupId>com.azure</groupId>
43+
<artifactId>azure-ai-openai</artifactId>
44+
<version>1.0.0-beta.9</version>
45+
</dependency>
46+
```
47+
48+
Next, include the default Azure default credentials when creating the client
49+
50+
```java
51+
TokenCredential defaultCredential = new DefaultAzureCredentialBuilder().build();
52+
53+
OpenAIClient client = new OpenAIClientBuilder()
54+
.credential(defaultCredential)
55+
.endpoint("{endpoint}")
56+
.buildClient();
57+
```
58+
59+
Once the credentials are added to the application, you’ll then need to enable managed identity in your application and grant access to the resource.
60+
61+
1. In your web app resource, navigate to the **Identity** blade and turn on **System assigned** and click **Save**
62+
2. Once System assigned identity is turned on, it will register the web app with Microsoft Entra ID and the web app can be granted permissions to access protected resources.
63+
3. Go to your Azure OpenAI resource and navigate to the **Access control (IAM)** blade on the left pane.
64+
4. Find the Grant access to this resource card and click on **Add role assignment**
65+
5. Search for the **Cognitive Services OpenAI User** role and click **Next**
66+
6. On the **Members** tab, find **Assign access to** and choose the **Managed identity** option
67+
7. Next, click on **+Select Members** and find your web app
68+
8. Click **Review + assign**
69+
70+
Your web app is now added as a cognitive service OpenAI user and can communicate to your Azure OpenAI resource.
71+
3272
### API Keys and Endpoints
3373

3474
First, you need to grab the keys and endpoint values from Azure OpenAI, or OpenAI and add them as secrets for use in your application. Retrieve and save the values for later use to build the client.

articles/app-service/includes/deploy-intelligent-apps/deploy-intelligent-apps-linux-python-pivot.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,45 @@ Next, copy and replace the *hello.html* file with the following code:
8282

8383
After the files are updated, we can start preparing our environment variables to work with OpenAI.
8484

85+
### Secure your app with managed identity
86+
87+
Although optional, it's highly recommended to secure your application using [managed identity](../../../overview-managed-identity.md) to authenticate your app to your Azure OpenAI resource. Skip this step if you are not using Azure OpenAI. This enables your application to access the Azure OpenAI resource without needing to manage API keys.
88+
89+
Follow the steps below to secure your application:
90+
91+
Add the identity package `Azure.Identity`. This package enables using Azure credentials in your app. Install the package and import the default credential and bearer token provider.
92+
93+
```python
94+
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
95+
```
96+
97+
Next, include the default Azure credentials and token provider in the AzureOpenAI options.
98+
99+
```python
100+
token_provider = get_bearer_token_provider(
101+
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
102+
)
103+
104+
client = AzureOpenAI(
105+
api_version="2024-02-15-preview",
106+
azure_endpoint="https://{your-custom-endpoint}.openai.azure.com/",
107+
azure_ad_token_provider=token_provider
108+
)
109+
```
110+
111+
Once the credentials are added to the application, you’ll then need to enable managed identity in your application and grant access to the resource.
112+
113+
1. In your web app resource, navigate to the **Identity** blade and turn on **System assigned** and click **Save**
114+
2. Once System assigned identity is turned on, it will register the web app with Microsoft Entra ID and the web app can be granted permissions to access protected resources.
115+
3. Go to your Azure OpenAI resource and navigate to the **Access control (IAM)** blade on the left pane.
116+
4. Find the Grant access to this resource card and click on **Add role assignment**
117+
5. Search for the **Cognitive Services OpenAI User** role and click **Next**
118+
6. On the **Members** tab, find **Assign access to** and choose the **Managed identity** option
119+
7. Next, click on **+Select Members** and find your web app
120+
8. Click **Review + assign**
121+
122+
Your web app is now added as a cognitive service OpenAI user and can communicate to your Azure OpenAI resource.
123+
85124
### API Keys and Endpoints
86125

87126
In order to make calls to OpenAI with your client, you need to first grab the Keys and Endpoint values from Azure OpenAI, or OpenAI and add them as secrets for use in your application. Retrieve and save the values for later use.

articles/app-service/toc.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,10 @@
383383
href: deploy-container-azure-pipelines.md
384384
- name: Deploy a multi-container WordPress app
385385
href: tutorial-multi-container-app.md
386+
- name: Integrate AI with App Service
387+
items:
388+
- name: Deploy an application that uses OpenAI on App Service
389+
href: deploy-intelligent-apps.md
386390
- name: Reliability
387391
items:
388392
- name: Reliability in Azure App Service
@@ -435,8 +439,6 @@
435439
href: operating-system-functionality.md
436440
- name: Kudu service
437441
href: resources-kudu.md
438-
- name: Deploy an application that uses OpenAI on App Service
439-
href: deploy-intelligent-apps.md
440442
- name: gRPC configuration
441443
href: configure-grpc.md
442444
- name: Recommended services (preview)

0 commit comments

Comments
 (0)