若要运行本文中的示例,请创建可访问资源的Microsoft Entra应用程序和服务主体。 可以在订阅范围添加角色分配,并获取所需的 Microsoft Entra Directory (tenant) ID、Application ID 和 Application Secret。
以下代码片段演示如何使用 Microsoft 身份验证库 (MSAL) 获取Microsoft Entra应用程序令牌来访问群集。 要使流成功,必须使用Microsoft Entra ID 注册应用程序,并且必须具有用于应用程序身份验证的凭据,例如Microsoft Entra ID 颁发的应用程序密钥或Microsoft Entra注册的 X.509v2 证书。
配置客户管理的密钥
默认情况下,Azure 数据资源管理器加密使用 Microsoft 托管密钥。 将 Azure 数据资源管理器群集配置为使用客户托管密钥,并指定要与群集关联的密钥。
使用以下代码更新群集:
var tenantId = "xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx"; // Azure AD Directory (tenant) ID
var clientId = "xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx"; // Application ID
var clientSecret = "PlaceholderClientSecret"; // Application secret
var subscriptionId = "xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx";
var credentials = new ClientSecretCredential(tenantId, clientId, clientSecret);
var resourceManagementClient = new ArmClient(credentials, subscriptionId);
var resourceGroupName = "testrg";
var clusterName = "mykustocluster";
var subscription = await resourceManagementClient.GetDefaultSubscriptionAsync();
var resourceGroup = (await subscription.GetResourceGroupAsync(resourceGroupName)).Value;
var clusters = resourceGroup.GetKustoClusters();
var cluster = (await clusters.GetAsync(clusterName)).Value;
var clusterPatch = new KustoClusterPatch(cluster.Data.Location)
{
KeyVaultProperties = new KustoKeyVaultProperties
{
KeyName = "<keyName>",
KeyVersion = "<keyVersion>", // Optional, leave as NULL for the latest version of the key.
KeyVaultUri = new Uri("https://<keyVaultName>.vault.azure.net/"),
UserIdentity = "/subscriptions/<identitySubscriptionId>/resourcegroups/<identityResourceGroupName>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<identityName>" // Use NULL if you want to use system assigned identity.
}
};
await cluster.UpdateAsync(WaitUntil.Completed, clusterPatch);
运行以下命令,检查群集是否已成功更新:
var clusterData = (await resourceGroup.GetKustoClusterAsync(clusterName)).Value.Data;