你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
Azure 通信服务是一个完全托管的通信平台,可让开发人员在其应用程序中生成实时通信功能。 通过将托管标识与 Azure 通信服务配合使用,可以简化应用程序的身份验证过程,同时提高其安全性。 本文档介绍如何将托管标识与 Azure 通信服务配合使用。
在 Azure 通信服务中使用托管标识
Azure 通信服务支持使用托管标识向服务进行身份验证。 通过使用托管标识,无需管理自己的访问令牌和凭据。
可以为 Azure 通信服务资源分配两种类型的标识:
- 系统分配的标识,该标识与资源关联,如果删除资源,会同时删除该标识。 资源只能有一个系统分配的标识。
- 用户分配的标识,该标识是可以分配给 Azure 通信服务资源的 Azure 资源。 删除资源时不会删除此标识。 资源可以有多个用户指定的身份。
若要将托管标识与 Azure 通信服务配合使用,请执行以下步骤:
- 向托管标识授予对通信服务资源的访问权限。 此分配可以通过 Azure 门户、Azure CLI 和 Azure 通信管理 SDK 进行。
- 使用托管标识通过 Azure 通信服务进行身份验证。 可以通过支持托管标识的 Azure SDK 或 REST API 完成身份验证。
添加系统分配的标识
添加用户分配的标识
将用户分配的标识分配给 Azure 通信服务资源需要先创建标识,然后将其资源标识符添加到通信服务资源。
首先,需要创建用户分配的托管标识资源。
根据这些说明创建用户分配的托管标识资源。
在应用页面的左侧导航中,向下滚动到“设置”组。
选择“标识”。
选择“用户分配”“添加”。>
使用 Azure 通信服务管理 SDK 的托管标识
还可以使用 Azure 通信管理 SDK 将托管标识分配给 Azure 通信服务资源。
可以通过在创建时或在更新资源时引入资源定义中的标识属性来实现此分配。
可以通过设置 Identity
上的 CommunicationServiceResourceData
属性,使用适用于 .NET 的 Azure 通信管理 SDK 将托管标识分配给 Azure 通信服务资源。
例如:
public async Task CreateResourceWithSystemAssignedManagedIdentity()
{
ArmClient armClient = new ArmClient(new DefaultAzureCredential());
SubscriptionResource subscription = await armClient.GetDefaultSubscriptionAsync();
//Create Resource group
ResourceGroupCollection rgCollection = subscription.GetResourceGroups();
// With the collection, we can create a new resource group with an specific name
string rgName = "myRgName";
AzureLocation ___location = AzureLocation.WestUS2;
ArmOperation<ResourceGroupResource> lro = await rgCollection.CreateOrUpdateAsync(WaitUntil.Completed, rgName, new ResourceGroupData(___location));
ResourceGroupResource resourceGroup = lro.Value;
// get resource group collection
CommunicationServiceResourceCollection collection = resourceGroup.GetCommunicationServiceResources();
string communicationServiceName = "myCommunicationService";
// Create Communication Service Resource
var identity = new ManagedServiceIdentity(ManagedServiceIdentityType.SystemAssigned);
CommunicationServiceResourceData data = new CommunicationServiceResourceData("global")
{
DataLocation = "UnitedStates",
Identity = identity
};
var communicationServiceLro = await collection.CreateOrUpdateAsync(WaitUntil.Completed, communicationServiceName, data);
var resource = communicationServiceLro.Value;
}
有关使用 .NET 管理 SDK 的详细信息,请参阅 适用于 .NET 的 Azure 通信管理 SDK。
有关管理资源实例的详细信息,请参阅 管理通信服务资源实例。
注释
资源可以同时具有系统分配的标识和用户分配的标识。 在本例中,该 type
属性为 SystemAssigned,UserAssigned
.
您还可以通过将type
属性指定为None
,从资源中删除所有托管身份分配。