Skip to content

Latest commit

 

History

History
198 lines (143 loc) · 8.24 KB

File metadata and controls

198 lines (143 loc) · 8.24 KB
author probableprime
ms.service azure-communication-services
ms.topic include
ms.date 06/30/2021
ms.author rifox
ms.custom sfi-ropc-nochange

Prerequisites

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.

Install the SDK

First, include the Communication Services Management SDK in your C# project:

using Azure.ResourceManager.Communication;

Subscription ID

You need to know the ID of your Azure subscription. Get your subscription ID from the portal:

  1. Sign in into your account on the Azure portal.
  2. From the left sidebar, select Subscriptions.
  3. Select the subscription you want to use.
  4. Click Overview.
  5. Select your Subscription ID.

For the examples to work, you need to store your subscription ID in an environment variable called AZURE_SUBSCRIPTION_ID.

Authentication

To communicate with Azure Communication Services, you must first authenticate yourself to Azure. You can authenticate this using a service principal identity.

Option 1: Managed 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);

Option 2: Service Principal

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());

Option 3: User Identity

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());

Manage Communication Services resources

Interact with Azure resources

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.

Create and manage a Communication Services resource

You can use the instance of the Communication Services Management SDK client (Azure.ResourceManager.Communication.CommunicationManagementClient) to perform operations on Communication Services resources.

Create a Communication Services resource

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();

Update a Communication Services resource

...
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();

List all Communication Services resources

var resources = acsClient.CommunicationService.ListBySubscription();
foreach (var resource in resources)
{
    Console.WriteLine(resource.Name);
}

Delete a Communication Services resource

var resourceGroupName = "myResourceGroupName";
var resourceName = "myResource";
await acsClient.CommunicationService.StartDeleteAsync(resourceGroupName, resourceName);

Managing keys and connection strings

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.

Get access keys for a Communication Services resource

var resourceGroupName = "myResourceGroupName";
var resourceName = "myResource";
var keys = await acsClient.CommunicationService.ListKeysAsync(resourceGroupName, resourceName);

Console.WriteLine(keys.Value.PrimaryConnectionString);
Console.WriteLine(keys.Value.SecondaryConnectionString);

Regenerate an access key for a Communication Services resource

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);