다음을 통해 공유


비대화식 인증 .NET HDInsight 애플리케이션 만들기

애플리케이션의 자체 ID(비대화형) 또는 애플리케이션의 로그인한 사용자 ID(대화형)에서 Microsoft .NET Azure HDInsight 애플리케이션을 실행합니다. 이 문서에서는 비대화형 인증 .NET 애플리케이션을 만들어 Azure에 연결하고 HDInsight를 관리하는 방법을 보여줍니다. 대화형 애플리케이션의 샘플은 Azure HDInsight에 연결을 참조하세요.

귀하의 대화형이 아닌 .NET 애플리케이션에는 다음 사항이 필요합니다.

필수 조건

HDInsight 클러스터. 시작 자습서를 참조하세요.

Microsoft Entra 애플리케이션에 역할 할당

Microsoft Entra 애플리케이션에 역할을 할당하여 작업을 수행할 수 있는 권한을 부여합니다. 구독, 리소스 그룹 또는 리소스 수준에서 범위를 설정할 수 있습니다. 사용 권한은 하위 수준의 범위로 상속됩니다. 예를 들어, 리소스 그룹에 대한 Reader 역할에 애플리케이션을 추가하면 애플리케이션이 리소스 그룹 및 그 안의 모든 리소스를 액세스할 수 있습니다. 이 문서에서는 리소스 그룹 수준에서 범위를 설정합니다. 자세한 내용은 Azure 구독 리소스에 대한 액세스를 관리하는 Azure 역할 할당을 참조하세요.

Microsoft Entra 애플리케이션에 소유자 역할을 추가하려면

  1. Azure Portal에 로그인합니다.
  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);
            }
        }
    }
    

다음 단계