Skip to content

Commit 0cdfa36

Browse files
Merge pull request #280767 from jeffwmartinez/jefmarti-msi-flow
improving dotnet workflow
2 parents f58af3c + fade2de commit 0cdfa36

File tree

1 file changed

+41
-41
lines changed

1 file changed

+41
-41
lines changed

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

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ms.date: 04/10/2024
88
ms.author: jefmarti
99
---
1010

11-
You can use Azure App Service to work with popular AI frameworks like LangChain and Semantic Kernel connected to OpenAI for creating intelligent apps. In the following tutorial, we are adding an Azure OpenAI service using Semantic Kernel to a .NET 8 Blazor web application.
11+
You can use Azure App Service to work with popular AI frameworks like LangChain and Semantic Kernel connected to OpenAI for creating intelligent apps. In the following tutorial, we're adding an Azure OpenAI service using Semantic Kernel to a .NET 8 Blazor web application.
1212

1313
#### Prerequisites
1414

@@ -17,7 +17,7 @@ You can use Azure App Service to work with popular AI frameworks like LangChain
1717

1818
### Setup Blazor web app
1919

20-
For this Blazor web application, we are building off the Blazor [template](https://dotnet.microsoft.com/learn/aspnet/blazor-tutorial/intro) and creating a new razor page that can send and receive requests to an Azure OpenAI OR OpenAI service using Semantic Kernel.
20+
For this Blazor web application, we're building off the Blazor [template](https://dotnet.microsoft.com/learn/aspnet/blazor-tutorial/intro) and creating a new razor page that can send and receive requests to an Azure OpenAI OR OpenAI service using Semantic Kernel.
2121

2222
1. Right click on the **Pages** folder found under the **Components** folder and add a new item named *OpenAI.razor*
2323
2. Add the following code to the **OpenAI.razor* file and click **Save**
@@ -60,48 +60,11 @@ 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-
10063
### API keys and endpoints
10164

10265
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.
10366

104-
For Azure OpenAI, see [this documentation](../../../ai-services/openai/quickstart.md?pivots=programming-language-csharp&tabs=command-line%2Cpython#retrieve-key-and-endpoint) to retrieve the key and endpoint values. For our application, you need the following values:
67+
For Azure OpenAI, see [this documentation](../../../ai-services/openai/quickstart.md?pivots=programming-language-csharp&tabs=command-line%2Cpython#retrieve-key-and-endpoint) to retrieve the key and endpoint values. If you're planning to use [managed identity](../../overview-managed-identity.md) to secure your app you'll only need the `deploymentName` and `endpoint` values. Otherwise, you need each of the following:
10568

10669
- `deploymentName`
10770
- `endpoint`
@@ -198,7 +161,7 @@ Here we're adding the using statement and creating the Kernel in a method that w
198161

199162
### Add your AI service
200163

201-
Once the Kernel is initialized, we can add our chosen AI service to the kernel. Here we define our model and pass in our key and endpoint information to be consumed by the chosen model.
164+
Once the Kernel is initialized, we can add our chosen AI service to the kernel. Here we define our model and pass in our key and endpoint information to be consumed by the chosen model. If you plan to use managed identity with Azure OpenAI, add the service using the example in the next section.
202165

203166
For Azure OpenAI, use the following code:
204167

@@ -225,6 +188,43 @@ builder.Services.AddOpenAIChatCompletion(
225188
var kernel = builder.Build();
226189
```
227190

191+
### Secure your app with managed identity
192+
193+
If you’re using Azure OpenAI, it's highly recommended to secure your application using [managed identity](../../overview-managed-identity.md) to authenticate your app to your Azure OpenAI resource. This enables your application to access the Azure OpenAI resource without needing to manage API keys. If you're not using Azure OpenAI, your secrets can remain secure using Azure Key Vault outlined above.
194+
195+
Follow the steps below to secure your application with managed identity:
196+
197+
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.
198+
199+
```c#
200+
@using Azure.Identity
201+
```
202+
203+
Next, include the default Azure credentials in the chat completions parameters. The `deploymentName` and `endpoint` parameters are still required and should be secured using the Key Vault method covered in the previous section.
204+
205+
```c#
206+
var kernel = Kernel.CreateBuilder()
207+
.AddAzureOpenAIChatCompletion(
208+
deploymentName: deploymentName,
209+
endpoint: endpoint,
210+
credentials: new DefaultAzureCredential()
211+
)
212+
.Build();
213+
```
214+
215+
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.
216+
217+
1. In your web app resource, navigate to the **Identity** blade and turn on **System assigned** and click **Save**
218+
2. Once System assigned identity is turned on, it register's the web app with Microsoft Entra ID and the web app can be granted permissions to access protected resources.
219+
3. Go to your Azure OpenAI resource and navigate to the **Access control (IAM)** blade on the left pane.
220+
4. Find the Grant access to this resource card and click on **Add role assignment**
221+
5. Search for the **Cognitive Services OpenAI User** role and click **Next**
222+
6. On the **Members** tab, find **Assign access to** and choose the **Managed identity** option
223+
7. Next, click on **+Select Members** and find your web app
224+
8. Click **Review + assign**
225+
226+
Your web app is now added as a cognitive service OpenAI user and can communicate to your Azure OpenAI resource.
227+
228228
### Configure prompt and create semantic function
229229

230230
Now that our chosen OpenAI service client is created with the correct keys we can add a function to handle the prompt. With Semantic Kernel you can handle prompts by the use of a semantic function, which turn the prompt and the prompt configuration settings into a function the Kernel can execute. Learn more on configuring prompts [here](/semantic-kernel/prompts/configure-prompts?tabs=Csharp).

0 commit comments

Comments
 (0)