使用 Microsoft Entra ID 為您的 Azure Cosmos DB 帳戶設定受控識別

適用於:NoSQL MongoDB Cassandra Gremlin Table

Azure 資源受控識別會在 Microsoft Entra ID 中為 Azure 服務提供自動受控識別。 本文說明如何建立 Azure Cosmos DB 帳戶的受控識別。

必要條件

新增系統指派的身分識別

使用 Azure 入口網站

若要在現有的 Azure Cosmos DB 帳戶上啟用系統指派的受控識別,請在 Azure 入口網站中瀏覽至您的帳戶,然後從左側功能表中選取 [身分識別]

The Identity menu entry

在 [系統指派] 區段下,將 [狀態] 翻轉至 [開啟],然後選取 [儲存]。 系統會要求您確認已建立系統指派的受控識別。

Enabling a system-assigned identity

一旦建立並指派了身分識別之後,您就可擷取其物件 (主體) 識別碼。

Retrieving the object ID of a system-assigned identity

使用 Azure Resource Manager (ARM) 範本

重要

使用受控識別時,請務必使用 apiVersion2021-03-15 或更高版本。

若要在新的或現有的 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 Resource Manager (ARM) 範本

重要

使用受控識別時,請務必使用 apiVersion2021-03-15 或更高版本。

若要在新的或現有的 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 選項,並傳遞您要指派的身分識別的資源識別碼:

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 Resource Manager (ARM) 範本

重要

使用受控識別時,請務必使用 apiVersion2021-03-15 或更高版本。

若要從您的 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

下一步