你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用 Microsoft Entra ID 为 Azure Cosmos DB 帐户配置托管标识

适用对象: NoSQL MongoDB Cassandra Gremlin

Azure 资源的托管标识在 Microsoft Entra ID 中为 Azure 服务提供了一个自动托管标识。 本文介绍如何为 Azure Cosmos DB 帐户创建托管标识。

先决条件

添加系统分配的标识

使用 Azure 门户

若要对现有 Azure Cosmos DB 帐户启用系统分配的托管标识,请导航到 Azure 门户中的帐户,然后从左侧菜单中选择“标识”。

The Identity menu entry

在“系统分配”部分下,将“状态”切换为“启用”并选择“保存”。 系统会要求你确认是否创建了系统分配的托管标识。

Enabling a system-assigned identity

创建并分配标识后,可以检索其对象(主体)ID。

Retrieving the object ID of a system-assigned identity

使用 Azure 资源管理器 (ARM) 模板

重要

使用托管标识时,请确保使用 2021-03-15 或更高的 apiVersion

若要在新的或现有的 Azure Cosmos DB 帐户中启用系统分配的标识,请在资源定义中包含以下属性:

"identity": {
    "type": "SystemAssigned"
}

然后,ARM 模板的 resources 节应如下所示:

"resources": [
    {
        "type": " Microsoft.DocumentDB/databaseAccounts",
        "identity": {
            "type": "SystemAssigned"
        },
        // ...
    },
    // ...
]

创建或更新 Azure Cosmos DB 帐户后,它将显示以下属性:

"identity": {
    "type": "SystemAssigned",
    "tenantId": "<azure-ad-tenant-id>",
    "principalId": "<azure-ad-principal-id>"
}

使用 Azure CLI

若要在新建 Azure Cosmos DB 帐户时启用系统分配的标识,请添加 --assign-identity 选项:

resourceGroupName='myResourceGroup'
accountName='mycosmosaccount'

az cosmosdb create \
    -n $accountName \
    -g $resourceGroupName \
    --locations regionName='West US 2' failoverPriority=0 isZoneRedundant=False \
    --assign-identity

你还可使用 az cosmosdb identity assign 命令在现有帐户上添加系统分配的标识:

resourceGroupName='myResourceGroup'
accountName='mycosmosaccount'

az cosmosdb identity assign \
    -n $accountName \
    -g $resourceGroupName

创建或更新 Azure Cosmos DB 帐户后,可提取使用 az cosmosdb identity show 命令分配的标识:

resourceGroupName='myResourceGroup'
accountName='mycosmosaccount'

az cosmosdb identity show \
    -n $accountName \
    -g $resourceGroupName
{
    "type": "SystemAssigned",
    "tenantId": "<azure-ad-tenant-id>",
    "principalId": "<azure-ad-principal-id>"
}

添加用户分配的标识

使用 Azure 门户

若要对现有 Azure Cosmos DB 帐户启用用户分配的托管标识,请导航到 Azure 门户中的帐户,然后从左侧菜单中选择“标识”。

The Identity menu entry

在“用户分配”部分下,选择“+ 添加”。

Enabling a user-assigned identity

找到并选择要分配给 Azure Cosmos DB 帐户的所有标识,然后选择“添加”。

Selecting all the identities to assign

使用 Azure 资源管理器 (ARM) 模板

重要

使用托管标识时,请确保使用 2021-03-15 或更高的 apiVersion

若要在新的或现有的 Azure Cosmos DB 帐户中启用用户分配的标识,请在资源定义中包含以下属性:

"identity": {
    "type": "UserAssigned",
    "userAssignedIdentities": {
        "<identity-resource-id>": {}
    }
}

然后,ARM 模板的 resources 节应如下所示:

"resources": [
    {
        "type": " Microsoft.DocumentDB/databaseAccounts",
        "identity": {
            "type": "UserAssigned",
            "userAssignedIdentities": {
                "<identity-resource-id>": {}
            }
        },
        // ...
    },
    // ...
]

创建或更新 Azure Cosmos DB 帐户后,它将显示以下属性:

"identity": {
    "type": "UserAssigned",
    "tenantId": "<azure-ad-tenant-id>",
    "principalId": "<azure-ad-principal-id>"
}

使用 Azure CLI

若要在创建新的 Azure Cosmos DB 帐户时启用用户分配的标识,请添加 --assign-identity 选项并传递要分配的标识的资源 ID:

resourceGroupName='myResourceGroup'
accountName='mycosmosaccount'

az cosmosdb create \
    -n $accountName \
    -g $resourceGroupName \
    --locations regionName='West US 2' failoverPriority=0 isZoneRedundant=False \
    --assign-identity <identity-resource-id>

还可使用 az cosmosdb identity assign 命令在现有帐户上添加用户分配的标识:

resourceGroupName='myResourceGroup'
accountName='mycosmosaccount'

az cosmosdb identity assign \
    -n $accountName \
    -g $resourceGroupName
    --identities <identity-resource-id>

创建或更新 Azure Cosmos DB 帐户后,可提取使用 az cosmosdb identity show 命令分配的标识:

resourceGroupName='myResourceGroup'
accountName='mycosmosaccount'

az cosmosdb identity show \
    -n $accountName \
    -g $resourceGroupName
{
    "type": "UserAssigned",
    "tenantId": "<azure-ad-tenant-id>",
    "principalId": "<azure-ad-principal-id>"
}

删除系统分配的标识或用户分配的标识

使用 Azure 资源管理器 (ARM) 模板

重要

使用托管标识时,请确保使用 2021-03-15 或更高的 apiVersion

若要从 Azure Cosmos DB 帐户中删除系统分配的标识,请将 identity 属性的 type 设置为 None

"identity": {
    "type": "None"
}

使用 Azure CLI

若要从 Azure Cosmos DB 帐户中删除所有托管的标识,请使用 az cosmosdb identity remove 命令:

resourceGroupName='myResourceGroup'
accountName='mycosmosaccount'

az cosmosdb identity remove \
    -n $accountName \
    -g $resourceGroupName

后续步骤