建立非互動式驗證 .NET HDInsight 應用程式

使用應用程式本身的身分識別 (非互動式) 或使用應用程式的登入使用者的身分識別 (互動式),執行 Microsoft .NET Azure HDInsight 應用程式。 本文將說明如何建立非互動式驗證 .NET 應用程式,來連線到 Azure 及管理 HDInsight。 如需互動式應用程式的範例,請參閱連線至 Azure HDInsight

從非互動式 .NET 應用程式,您需要︰

必要條件

HDInsight 叢集。 請參閱入門教學課程

將角色指派給 Microsoft Entra 應用程式

為 Microsoft Entra 應用程式指派角色,以授與執行動作的權限。 您可以針對訂用帳戶、資源群組或資源的層級設定範圍。 較低的範圍層級會繼承較高層級的權限。 例如,為資源群組的讀取者角色新增應用程式,代表應用程式可以讀取資源群組及其所包含的任何資源。 本文章會在資源群組層級設定範圍。 如需詳細資訊,請參閱指派 Azure 角色來管理 Azure 訂閱資源的存取權

將擁有者角色新增至 Microsoft Entra 應用程式

  1. 登入 Azure 入口網站
  2. 瀏覽資源群組,其中包含您稍後在本文章中要在其中執行 Hive 查詢的 HDInsight 叢集。 如果您有大量的資源群組,您可以使用篩選來找出您想要的資源群組。
  3. 在資源群組功能表中,選取 [存取控制 (IAM)]
  4. 選取 [角色指派] 索引標籤,以查看目前的角色指派。
  5. 從頁面頂端選取 [+ 新增]
  6. 請遵循指示,將擁有者角色新增至 Microsoft Entra 應用程式。 成功新增角色之後,應用程式會列在 [擁有者] 角色底下。

開發 HDInsight 用戶端應用程式

  1. 建立 C# 主控台應用程式。

  2. 新增以下 NuGet 套件:

    • Install-Package Microsoft.Azure.Common.Authentication -Pre
    • Install-Package Microsoft.Azure.Management.HDInsight -Pre
    • Install-Package Microsoft.Azure.Management.Resources -Pre
  3. 執行下列程式碼:

    using System;
    using System.Security;
    using Microsoft.Azure;
    using Microsoft.Azure.Common.Authentication;
    using Microsoft.Azure.Common.Authentication.Factories;
    using Microsoft.Azure.Common.Authentication.Models;
    using Microsoft.Azure.Management.Resources;
    using Microsoft.Azure.Management.HDInsight;
    
    namespace CreateHDICluster
    {
        internal class Program
        {
            private static HDInsightManagementClient _hdiManagementClient;
    
            private static Guid SubscriptionId = new Guid("<Enter your Azure subscription ID>");
            private static string tenantID = "<Enter your tenant ID (also called directory ID)>";
            private static string applicationID = "<Enter your application ID>";
            private static string secretKey = "<Enter the application secret key>";
    
            private static void Main(string[] args)
            {
                var key = new SecureString();
                foreach (char c in secretKey) { key.AppendChar(c); }
    
                var tokenCreds = GetTokenCloudCredentials(tenantID, applicationID, key);
                var subCloudCredentials = GetSubscriptionCloudCredentials(tokenCreds, SubscriptionId);
    
                var resourceManagementClient = new ResourceManagementClient(subCloudCredentials);
                resourceManagementClient.Providers.Register("Microsoft.HDInsight");
    
                _hdiManagementClient = new HDInsightManagementClient(subCloudCredentials);
    
                var results = _hdiManagementClient.Clusters.List();
                foreach (var name in results.Clusters)
                {
                    Console.WriteLine("Cluster Name: " + name.Name);
                    Console.WriteLine("\t Cluster type: " + name.Properties.ClusterDefinition.ClusterType);
                    Console.WriteLine("\t Cluster location: " + name.Location);
                    Console.WriteLine("\t Cluster version: " + name.Properties.ClusterVersion);
                }
                Console.WriteLine("Press Enter to continue");
                Console.ReadLine();
            }
    
            /// Get the access token for a service principal and provided key.          
            public static TokenCloudCredentials GetTokenCloudCredentials(string tenantId, string clientId, SecureString secretKey)
            {
                var authFactory = new AuthenticationFactory();
                var account = new AzureAccount { Type = AzureAccount.AccountType.ServicePrincipal, Id = clientId };
                var env = AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud];
                var accessToken =
                    authFactory.Authenticate(account, env, tenantId, secretKey, ShowDialog.Never).AccessToken;
    
                return new TokenCloudCredentials(accessToken);
            }
    
            public static SubscriptionCloudCredentials GetSubscriptionCloudCredentials(SubscriptionCloudCredentials creds, Guid subId)
            {
                return new TokenCloudCredentials(subId.ToString(), ((TokenCloudCredentials)creds).Token);
            }
        }
    }
    

下一步