[英]Retrieve Azure Data Factory Service Identity Application ID
我使用 C# 代碼創建了一個數據工廠和 Key Vault,我想設置 Key Vault 的訪問策略。 為此,我想要使用 C# 代碼的數據工廠“服務標識應用程序 ID” (在附圖中以紅色突出顯示)。 我怎么能那樣做?
如果您想使用應用程序 ID 獲取訪問令牌,希望此文檔可以提供幫助。
是的,您可以從 C# 代碼執行此操作。
這是我編寫的用於從 C# 代碼獲取服務標識應用程序 ID 的快速示例代碼。
先決條件是從包管理器控制台(工具 -> NuGet 包管理器 -> 包管理器控制台)安裝以下包:
Install-Package Microsoft.Azure.Management.DataFactory -Prerelease
Install-Package Microsoft.Azure.Management.ResourceManager -Prerelease
Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory
安裝包后,使用下面的代碼
using System;
using Microsoft.Rest;
using Microsoft.Azure.Management.ResourceManager;
using Microsoft.Azure.Management.DataFactory;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
namespace GetDataFactory
{
class Program
{
static void Main(string[] args)
{
// Set variables
string tenantID = "<your tenant ID>";
string applicationId = "<your application ID>";
string authenticationKey = "<your authentication key for the application>";
string subscriptionId = "<your subscription ID where the data factory resides>";
string resourceGroup = "<your resource group where the data factory resides>";
string dataFactoryName = "<specify the name of data factory to create. It must be globally unique.>";
// Authenticate and create a data factory management client
var context = new AuthenticationContext("https://login.windows.net/" + tenantID);
ClientCredential cc = new ClientCredential(applicationId, authenticationKey);
AuthenticationResult result = context.AcquireTokenAsync("https://management.azure.com/", cc).Result;
ServiceClientCredentials cred = new TokenCredentials(result.AccessToken);
var client = new DataFactoryManagementClient(cred) { SubscriptionId = subscriptionId };
var myFactory = client.Factories.Get(resourceGroup, dataFactoryName);
//Getting principal Id as you mentioned in question, but you can get more information from the Identity object as per your need.
Guid? principalId = myFactory.Identity.PrincipalId;
}
}
}
獲得所有身份信息后,您可以更新 keyvault 的訪問策略,以向應用程序(您在圖像中突出顯示其 ID)授予所需的權限(如列出密鑰、獲取/列出機密等)
使用 KeyVaultManagementClient 類 -
使用 Fluent API -
在 Github 上查看此示例 - https://github.com/Azure-Samples/key-vault-dotnet-manage-key-vaults
Utilities.Log("Authorizing the application associated with the current service principal..."); vault1 = vault1.Update().DefineAccessPolicy().ForServicePrincipal(SdkContext.AzureCredentialsFactory.FromFile(Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION")).ClientId).AllowKeyAllPermissions().AllowSecretPermissions(SecretPermissions.Get).AllowSecretPermissions(SecretPermissions.List).Attach().Apply(); Utilities.Log("Updated key vault"); Utilities.PrintVault(vault1); //============================================================ // Update a key vault Utilities.Log("Update a key vault to enable deployments and add permissions to the application..."); vault1 = vault1.Update().WithDeploymentEnabled().WithTemplateDeploymentEnabled().UpdateAccessPolicy(vault1.AccessPolicies[0].ObjectId).AllowSecretAllPermissions().Parent().Apply(); Utilities.Log("Updated key vault"); // Print the network security group Utilities.PrintVault(vault1);
使用休息 API
https://learn.microsoft.com/en-us/rest/api/keyvault/vaults/updateaccesspolicy
如果要檢索現有 ADF 的應用程序 ID,則需要進行 2 次訪問。
第一個是檢索資源管理器的服務標識。 @rohit 的第一個代碼塊在 c# 中執行此操作。 這將檢索主體的對象 ID,而不是作為該對象屬性的應用程序 ID。
第二種是通過 RM 從活動目錄中檢索應用程序 ID。 然后您可以使用它來分配訪問策略。 例如,在 powershell 中你會這樣做:
第一步:
$principal = (Get-AzureRmDataFactoryV2 -ResourceGroupName "yourRG" -Name yourADF).identity.PrincipalId
然后第二步...
$appId = (Get-AzureRmADServicePrincipal -ObjectId $principal).ApplicationId
c# 等價物應該很容易從中找出。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.