Skip to content

Commit 0bca125

Browse files
committed
Refactored to use entra id by default
1 parent d2137ea commit 0bca125

File tree

1 file changed

+33
-24
lines changed

1 file changed

+33
-24
lines changed

articles/ai-services/openai/includes/text-to-speech-dotnet.md

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,6 @@ recommendations: false
1818

1919
## Set up
2020

21-
### Retrieve key and endpoint
22-
23-
To successfully make a call against Azure OpenAI, you need an **endpoint** and a **key**.
24-
25-
|Variable name | Value |
26-
|--------------------------|-------------|
27-
| `AZURE_OPENAI_ENDPOINT` | This value can be found in the **Keys & Endpoint** section when examining your resource from the Azure portal. Alternatively, you can find the value in the **Azure OpenAI Studio** > **Playground** > **Code View**. An example endpoint is: `https://aoai-docs.openai.azure.com/`.|
28-
| `AZURE_OPENAI_API_KEY` | This value can be found in the **Keys & Endpoint** section when examining your resource from the Azure portal. You can use either `KEY1` or `KEY2`.|
29-
30-
Go to your resource in the Azure portal. The **Endpoint and Keys** can be found in the **Resource Management** section. Copy your endpoint and access key as you need both for authenticating your API calls. You can use either `KEY1` or `KEY2`. Always having two keys allows you to securely rotate and regenerate keys without causing a service disruption.
31-
32-
:::image type="content" source="../media/quickstarts/endpoint.png" alt-text="Screenshot of the overview UI for an Azure OpenAI resource in the Azure portal with the endpoint & access keys location highlighted." lightbox="../media/quickstarts/endpoint.png":::
33-
34-
3521
## Create the .NET app
3622

3723
1. Create a .NET app using the `dotnet new` command:
@@ -46,17 +32,31 @@ Go to your resource in the Azure portal. The **Endpoint and Keys** can be found
4632
cd OpenAISpeech
4733
```
4834
49-
## Install the client library
35+
1. Install the [`Azure.OpenAI`](https://www.nuget.org/packages/Azure.AI.OpenAI/) client library:
36+
37+
```dotnetcli
38+
dotnet add package Azure.AI.OpenAI
39+
```
40+
41+
## Authenticate and connect to Azure OpenAI
42+
43+
To make requests to your Azure OpenAI service, you need the service endpoint as well as authentication credentials via one of the following options:
44+
45+
- [Microsoft Entra ID](/entra/fundamentals/whatis) is the recommended approach for authenticating to Azure services and is more secure than key-based alternatives.
46+
- Access keys allow you to provide a secret key to connect to your resource.
47+
48+
> [!IMPORTANT]
49+
> Access keys should be used with caution. If your service access key is lost or accidentally exposed in an insecure location, your service may become vulnerable. Anyone who has the access key is able to authorize requests against the Azure OpenAI service.
50+
51+
### Get the Azure OpenAI endpoint
5052
51-
Install the [`Azure.OpenAI`](https://www.nuget.org/packages/Azure.AI.OpenAI/) client library:
53+
The service endpoint can be found in the **Keys & Endpoint** section when examining your resource from the Azure portal. Alternatively, you can find the value in the **Azure OpenAI Studio** > **Playground** > **Code View**. An example endpoint is: `https://aoai-docs.openai.azure.com/`.
5254
53-
```dotnetcli
54-
dotnet add package Azure.AI.OpenAI
55-
```
55+
:::image type="content" source="../media/quickstarts/endpoint.png" alt-text="Screenshot of the overview UI for an Azure OpenAI resource in the Azure portal with the endpoint & access keys location highlighted." lightbox="../media/quickstarts/endpoint.png":::
5656
57-
## Passwordless authentication is recommended
57+
### Authenticate using Microsoft Entra ID
5858
59-
Passwordless authentication is more secure than key-based alternatives and is the recommended approach for connecting to Azure services. If you choose to use Passwordless authentication, you'll need to complete the following:
59+
If you choose to use Microsoft Entra ID authentication, you'll need to complete the following:
6060
6161
1. Add the [`Azure.Identity`](https://www.nuget.org/packages/Azure.Identity) package.
6262
@@ -67,6 +67,10 @@ Passwordless authentication is more secure than key-based alternatives and is th
6767
1. Assign the `Cognitive Services User` role to your user account. This can be done in the Azure portal on your OpenAI resource under **Access control (IAM)** > **Add role assignment**.
6868
1. Sign-in to Azure using Visual Studio or the Azure CLI via `az login`.
6969
70+
### Authenticate using keys
71+
72+
The access key value can be found in the **Keys & Endpoint** section when examining your resource from the Azure portal. You can use either `KEY1` or `KEY2`. Always having two keys allows you to securely rotate and regenerate keys without causing a service disruption.
73+
7074
## Update the app code
7175
7276
1. Replace the contents of `program.cs` with the following code and update the placeholder values with your own.
@@ -76,9 +80,14 @@ Passwordless authentication is more secure than key-based alternatives and is th
7680
using Azure.AI.OpenAI;
7781
using Azure.Identity; // Required for Passwordless auth
7882
79-
var endpoint = new Uri("YOUR_OPENAI_ENDPOINT");
80-
var credentials = new AzureKeyCredential("YOUR_OPENAI_KEY");
81-
// var credentials = new DefaultAzureCredential(); // Use this line for Passwordless auth
83+
var endpoint = new Uri(
84+
Environment.GetEnvironmentVariable("YOUR_OPENAI_ENDPOINT") ?? throw new ArgumentNullException());
85+
var credentials = new DefaultAzureCredential();
86+
87+
// Use this line for key auth
88+
// var credentials = new AzureKeyCredential(
89+
// Environment.GetEnvironmentVariable("YOUR_OPENAI_KEY") ?? throw new ArgumentNullException());
90+
8291
var deploymentName = "tts"; // Default deployment name, update with your own if necessary
8392
var speechFilePath = "YOUR_AUDIO_FILE_PATH";
8493

0 commit comments

Comments
 (0)