|
| 1 | +--- |
| 2 | +title: Include file |
| 3 | +description: Include file |
| 4 | +services: azure-communication-services |
| 5 | +author: memontic |
| 6 | +ms.service: azure-communication-services |
| 7 | +ms.date: 07/15/2024 |
| 8 | +ms.topic: include |
| 9 | +ms.custom: include file |
| 10 | +ms.author: memontic |
| 11 | +--- |
| 12 | + |
| 13 | +#### [Connection String](#tab/connection-string) |
| 14 | + |
| 15 | +For simplicity, this quickstart uses a connection string to authenticate. In production environments, we recommend using [service principals](../../../identity/service-principal.md). |
| 16 | + |
| 17 | +Get the connection string from your Azure Communication Services resource in the Azure portal. On the left, navigate to the `Keys` tab. Copy the `Connection string` field for the primary key. The connection string is in the format `endpoint=https://{your Azure Communication Services resource name}.communication.azure.com/;accesskey={secret key}`. |
| 18 | + |
| 19 | +:::image type="content" source="../media/get-started/get-communication-resource-connection-string.png" lightbox="../media/get-started/get-communication-resource-connection-string.png" alt-text="Screenshot that shows an Azure Communication Services resource in the Azure portal, viewing the 'Connection string' field in the 'Primary key' section."::: |
| 20 | + |
| 21 | +Set the environment variable `COMMUNICATION_SERVICES_CONNECTION_STRING` to the value of your connection string. |
| 22 | +Open a console window and enter the following command: |
| 23 | +```console |
| 24 | +setx COMMUNICATION_SERVICES_CONNECTION_STRING "<your connection string>" |
| 25 | +``` |
| 26 | +After you add the environment variable, you might need to restart any running programs that will need to read the environment variable, including the console window. For example, if you're using Visual Studio as your editor, restart Visual Studio before running the example. |
| 27 | + |
| 28 | +For more information on how to set an environment variable for your system, follow the steps at [Store your connection string in an environment variable](../../../create-communication-resource.md#store-your-connection-string-in-an-environment-variable). |
| 29 | + |
| 30 | +To instantiate a `NotificationMessagesClient`, add the following code to the `Main` method: |
| 31 | +```csharp |
| 32 | +// Retrieve connection string from environment variable |
| 33 | +string connectionString = |
| 34 | + Environment.GetEnvironmentVariable("COMMUNICATION_SERVICES_CONNECTION_STRING"); |
| 35 | +
|
| 36 | +// Instantiate the client |
| 37 | +var notificationMessagesClient = new NotificationMessagesClient(connectionString); |
| 38 | +``` |
| 39 | + |
| 40 | +#### [Microsoft Entra ID](#tab/aad) |
| 41 | + |
| 42 | +You can also authenticate with Microsoft Entra ID using the [Azure Identity library](https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/identity/Azure.Identity). |
| 43 | + |
| 44 | +The [`Azure.Identity`](https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/identity/Azure.Identity) package provides various credential types that your application can use to authenticate. You can choose from the various options to authenticate the identity client detailed at [Azure Identity - Credential providers](/dotnet/api/overview/azure/identity-readme#credentials) and [Azure Identity - Authenticate the client](/dotnet/api/overview/azure/identity-readme#authenticate-the-client). This option walks through one way of using the [`DefaultAzureCredential`](/dotnet/api/overview/azure/identity-readme#defaultazurecredential). |
| 45 | + |
| 46 | +The `DefaultAzureCredential` attempts to authenticate via [`several mechanisms`](/dotnet/api/overview/azure/identity-readme#defaultazurecredential) and might obtain its authentication credentials if you're signed into Visual Studio or Azure CLI. However, this option walks you through setting up with environment variables. |
| 47 | + |
| 48 | +To create a `DefaultAzureCredential` object: |
| 49 | +1. To set up your service principle app, follow the instructions at [Creating a Microsoft Entra registered Application](../../../identity/service-principal.md?pivots=platform-azcli#creating-a-microsoft-entra-registered-application). |
| 50 | + |
| 51 | +1. Set the environment variables `AZURE_CLIENT_ID`, `AZURE_CLIENT_SECRET`, and `AZURE_TENANT_ID` using the output of your app's creation. |
| 52 | + Open a console window and enter the following commands: |
| 53 | + ```console |
| 54 | + setx AZURE_CLIENT_ID "<your app's appId>" |
| 55 | + setx AZURE_CLIENT_SECRET "<your app's password>" |
| 56 | + setx AZURE_TENANT_ID "<your app's tenant>" |
| 57 | + ``` |
| 58 | + After you add the environment variables, you might need to restart any running programs that will need to read the environment variables, including the console window. For example, if you're using Visual Studio as your editor, restart Visual Studio before running the example. |
| 59 | + |
| 60 | +1. To use the [`DefaultAzureCredential`](/dotnet/api/overview/azure/identity-readme#defaultazurecredential) provider, or other credential providers provided with the Azure SDK, install the `Azure.Identity` NuGet package and add the following `using` directive to your *Program.cs* file. |
| 61 | + ```csharp |
| 62 | + using Azure.Identity; |
| 63 | + ``` |
| 64 | + |
| 65 | +1. To instantiate a `NotificationMessagesClient`, add the following code to the `Main` method. |
| 66 | + ```csharp |
| 67 | + // Configure authentication |
| 68 | + var endpoint = new Uri("https://<resource name>.communication.azure.com"); |
| 69 | + var credential = new DefaultAzureCredential(); |
| 70 | +
|
| 71 | + // Instantiate the client |
| 72 | + var notificationMessagesClient = |
| 73 | + new NotificationMessagesClient(endpoint, credential); |
| 74 | + ``` |
| 75 | + |
| 76 | +#### [AzureKeyCredential](#tab/azurekeycredential) |
| 77 | + |
| 78 | +You can also authenticate with an AzureKeyCredential. |
| 79 | + |
| 80 | +Get the endpoint and key from your Azure Communication Services resource in the Azure portal. On the left, navigate to the `Keys` tab. Copy the `Endpoint` and the `Key` field for the primary key. |
| 81 | + |
| 82 | +:::image type="content" source="../media/get-started/get-communication-resource-endpoint-and-key.png" lightbox="../media/get-started/get-communication-resource-endpoint-and-key.png" alt-text="Screenshot that shows an Azure Communication Services resource in the Azure portal, viewing the 'Connection string' field in the 'Primary key' section."::: |
| 83 | + |
| 84 | +Set the environment variable `COMMUNICATION_SERVICES_KEY` to the value of your connection string. |
| 85 | +Open a console window and enter the following command: |
| 86 | +```console |
| 87 | +setx COMMUNICATION_SERVICES_KEY "<your key>" |
| 88 | +``` |
| 89 | +After you add the environment variable, you might need to restart any running programs that will need to read the environment variable, including the console window. For example, if you're using Visual Studio as your editor, restart Visual Studio before running the example. |
| 90 | + |
| 91 | +For more information on how to set an environment variable for your system, follow the steps at [Store your connection string in an environment variable](../../../create-communication-resource.md#store-your-connection-string-in-an-environment-variable). |
| 92 | + |
| 93 | +To instantiate a `NotificationMessagesClient`, add the following code to the `Main` method: |
| 94 | +```csharp |
| 95 | +// Retrieve key from environment variable |
| 96 | +string key = |
| 97 | + Environment.GetEnvironmentVariable("COMMUNICATION_SERVICES_KEY"); |
| 98 | +
|
| 99 | +// Configure authentication |
| 100 | +var endpoint = new Uri("https://<resource name>.communication.azure.com"); |
| 101 | +var credential = new AzureKeyCredential(key); |
| 102 | +
|
| 103 | +// Instantiate the client |
| 104 | +var notificationMessagesClient = |
| 105 | + new NotificationMessagesClient(endpoint, credential); |
| 106 | +``` |
| 107 | + |
| 108 | +--- |
0 commit comments