共用方式為


使用適用於 NoSQL 的 Azure Cosmos DB 停用密鑰型驗證

適用於:NoSQL

部署指南序列中目前位置 (『Prepare』) 的圖表。

部署指南順序的圖表,包括這些位置,順序如下:概觀、概念、準備、角色型訪問控制、網路和參考。 「準備」位置目前已醒目提示。

本文涵蓋針對 NoSQL 帳戶停用 Azure Cosmos DB 金鑰型授權(或資源擁有者密碼認證驗證)的程式。

停用密鑰型授權可防止您的帳戶使用,而不需要更安全的 Microsoft Entra 驗證方法。 此程式是應在安全工作負載的新帳戶上執行的步驟。 或者,在移轉至安全工作負載模式的現有帳戶上執行此程式。

必要條件

停用金鑰型驗證

首先,停用現有帳戶的密鑰型驗證,讓應用程式必須使用Microsoft Entra 驗證。 使用 az resource update 來修改 properties.disableLocalAuth 現有的帳戶。

az resource update \
    --resource-group "<name-of-existing-resource-group>" \
    --name "<name-of-existing-account>" \
    --resource-type "Microsoft.DocumentDB/databaseAccounts" \
    --set properties.disableLocalAuth=true \
    --set properties.disableKeyBasedMetadataWriteAccess=true

首先,建立已停用密鑰型驗證的新帳戶,讓應用程式必須使用Microsoft Entra 驗證。

  1. 建立新的 Bicep 檔案,以停用金鑰型驗證來部署新的帳戶。 將檔案 命名為 deploy-new-account.bicep

    metadata description = 'Deploys a new Azure Cosmos DB account with key-based auth disabled.'
    
    @description('Name of the Azure Cosmos DB account.')
    param name string = 'csms-${uniqueString(resourceGroup().id)}'
    
    @description('Primary location for the Azure Cosmos DB account.')
    param location string = resourceGroup().location
    
    resource account 'Microsoft.DocumentDB/databaseAccounts@2024-05-15' = {
      name: name
      location: location
      kind: 'GlobalDocumentDB'
      properties: {
        databaseAccountOfferType: 'Standard'
        locations: [
          {
            locationName: location
          }
        ]
        disableLocalAuth: true,
        disableKeyBasedMetadataWriteAccess: true
      }
    }
    
  2. 使用 az deployment group create 部署具有新帳戶的 Bicep 檔案。

    az deployment group create \
        --resource-group "<name-of-existing-resource-group>" \
        --template-file deploy-new-account.bicep
    

首先,停用現有帳戶的密鑰型驗證,讓應用程式必須使用Microsoft Entra 驗證。 分別使用 Get-AzResourceSet-AzResource 來讀取和更新現有的帳戶。

$parameters = @{
    ResourceGroupName = "<name-of-existing-resource-group>"
    ResourceName = "<name-of-existing-account>"
    ResourceType = "Microsoft.DocumentDB/databaseAccounts"
}
$resource = Get-AzResource @parameters

$resource.Properties.DisableLocalAuth = $true
$resource.Properties.DisableKeyBasedMetadataWriteAccess = $true

$resource | Set-AzResource -Force

驗證已停用驗證

嘗試使用 Azure SDK,使用資源擁有者密碼認證 (ROPC) 連線到適用於 NoSQL 的 Azure Cosmos DB。 此嘗試應該會失敗。 如有必要,這裡會提供常見程序設計語言的程式代碼範例。

using Microsoft.Azure.Cosmos;

string connectionString = "AccountEndpoint=<nosql-endpoint>;AccountKey=<key>;";

CosmosClient client = new(connectionString);

重要

此程式代碼範例會使用 NuGet 的連結 Microsoft.Azure.Cosmos 庫。

後續步驟