| author | probableprime |
|---|---|
| ms.service | azure-communication-services |
| ms.topic | include |
| ms.date | 06/30/2021 |
| ms.author | rifox |
| ms.custom | sfi-ropc-nochange |
- An Azure account with an active subscription. Create an account for free.
- The latest version .NET Core SDK for your operating system.
- Get the latest version of the .NET Identity SDK.
- Get the latest version of the .NET Management SDK.
If you plan to use phone numbers, you can't use the free trial account. Check that your subscription meets all the requirements if you plan to purchase phone numbers before creating your resource.
First, include the Communication Services Management SDK in your C# project:
using Azure.ResourceManager.Communication;You need to know the ID of your Azure subscription. Get your subscription ID from the portal:
- Sign in into your account on the Azure portal.
- From the left sidebar, select Subscriptions.
- Select the subscription you want to use.
- Click Overview.
- Select your Subscription ID.
For the examples to work, you need to store your subscription ID in an environment variable called AZURE_SUBSCRIPTION_ID.
To communicate with Azure Communication Services, you must first authenticate yourself to Azure. You can authenticate this using a service principal identity.
If your code is running as a service in Azure, the easiest way to authenticate is to acquire a managed identity from Azure. For more information, see:
using Azure.Identity;
using Azure.ResourceManager.Communication;
using Azure.ResourceManager.Communication.Models;
using System;
...
var subscriptionId = "AZURE_SUBSCRIPTION_ID";
var acsClient = new CommunicationManagementClient(subscriptionId, new ManagedIdentityCredential());ClientId of the managed identity that you created must be passed to the ManagedIdentityCredential explicitly.
using Azure.Identity;
using Azure.ResourceManager.Communication;
using Azure.ResourceManager.Communication.Models;
using System;
...
var subscriptionId = "AZURE_SUBSCRIPTION_ID";
var managedIdentityCredential = new ManagedIdentityCredential("AZURE_CLIENT_ID");
var acsClient = new CommunicationManagementClient(subscriptionId, managedIdentityCredential);Instead of using a managed identity, you can authenticate to Azure using a service principal that you manage. For more information, see creating and managing a service principal in Microsoft Entra ID.
After you create your service principal, you need to collect the following information about it from the Azure portal:
- Client ID
- Client Secret
- Tenant ID
Store these values as environment variables named AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, and AZURE_TENANT_ID, respectively. You can then create a Communication Services management client like this:
using Azure.Identity;
using Azure.ResourceManager.Communication;
using Azure.ResourceManager.Communication.Models;
using System;
...
var subscriptionId = Environment.GetEnvironmentVariable("AZURE_SUBSCRIPTION_ID");
var acsClient = new CommunicationManagementClient(subscriptionId, new EnvironmentCredential());If you want to call Azure on behalf of an interactive user, rather than using a service identity, you can use the following code to create an Azure Communication Services Management client. This opens a browser window to prompt the user for their MSA or Microsoft Entra credentials.
using Azure.Identity;
using Azure.ResourceManager.Communication;
using Azure.ResourceManager.Communication.Models;
using System;
...
var subscriptionId = Environment.GetEnvironmentVariable("AZURE_SUBSCRIPTION_ID");
var communicationServiceClient = new CommunicationManagementClient(subscriptionId, new InteractiveBrowserCredential());Once you authenticate, you can use your management client to make API calls.
For each of the following examples, we assign our Communication Services resources to an existing resource group.
If you need to create a resource group, you can use the Azure portal or the Azure Resource Manager SDK.
You can use the instance of the Communication Services Management SDK client (Azure.ResourceManager.Communication.CommunicationManagementClient) to perform operations on Communication Services resources.
When creating a Communication Services resource, specify the resource group name and resource name. The Location property is always global, and during public preview the DataLocation value must be UnitedStates.
var resourceGroupName = "myResourceGroupName";
var resourceName = "myResource";
var resource = new CommunicationServiceResource { Location = "Global", DataLocation = "UnitedStates" };
var operation = await acsClient.CommunicationService.StartCreateOrUpdateAsync(resourceGroupName, resourceName, resource);
await operation.WaitForCompletionAsync();...
var resourceGroupName = "myResourceGroupName";
var resourceName = "myResource";
var resource = new CommunicationServiceResource { Location = "Global", DataLocation = "UnitedStates" };
resource.Tags.Add("environment","test");
resource.Tags.Add("department","tech");
// Use existing resource name and new resource object
var operation = await acsClient.CommunicationService.StartCreateOrUpdateAsync(resourceGroupName, resourceName, resource);
await operation.WaitForCompletionAsync();var resources = acsClient.CommunicationService.ListBySubscription();
foreach (var resource in resources)
{
Console.WriteLine(resource.Name);
}var resourceGroupName = "myResourceGroupName";
var resourceName = "myResource";
await acsClient.CommunicationService.StartDeleteAsync(resourceGroupName, resourceName);Every Communication Services resource has a pair of access keys and corresponding connection strings. You can access these keys using the Management SDK and then make them available to other Communication Services SDKs to authenticate themselves to Azure Communication Services.
var resourceGroupName = "myResourceGroupName";
var resourceName = "myResource";
var keys = await acsClient.CommunicationService.ListKeysAsync(resourceGroupName, resourceName);
Console.WriteLine(keys.Value.PrimaryConnectionString);
Console.WriteLine(keys.Value.SecondaryConnectionString);var resourceGroupName = "myResourceGroupName";
var resourceName = "myResource";
var keyParams = new RegenerateKeyParameters { KeyType = KeyType.Primary };
var keys = await acsClient.CommunicationService.RegenerateKeyAsync(resourceGroupName, resourceName, keyParams);
Console.WriteLine(keys.Value.PrimaryKey);