Microsoft .NET Azure HDInsight アプリケーションを、アプリケーションの独自の ID (非対話型) またはアプリケーションのサインイン ユーザーの ID (対話型) で実行します。 この記事では、非対話型認証 .NET アプリケーションを作成して Azure に接続し、HDInsight を管理する方法について説明します。 対話型アプリケーションのサンプルについては、「 Azure HDInsight への接続」を参照してください。
非対話型の .NET アプリケーションでは、次のものが必要です。
- Azure サブスクリプション テナント ID ( ディレクトリ ID とも呼ばれます)。 「 テナント ID を取得する」を参照してください。
- Microsoft Entra アプリケーション クライアント ID。 「 Microsoft Entra アプリケーションを作成する 」と「 アプリケーション ID を取得する」を参照してください。
- Microsoft Entra アプリケーションの秘密キー。 アプリケーション 認証キーの取得を参照してください。
[前提条件]
HDInsight クラスター。 開始 チュートリアルを参照してください。
Microsoft Entra アプリケーションにロールを割り当てる
Microsoft Entra アプリケーションに ロールを割り当てて、アクションを実行するアクセス許可を付与します。 スコープは、サブスクリプション、リソース グループ、またはリソースのレベルで設定できます。 アクセス許可は、下位レベルのスコープに継承されます。 たとえば、リソース グループの閲覧者ロールにアプリケーションを追加すると、アプリケーションはリソース グループとその中のリソースを読み取ることができます。 この記事では、リソース グループ レベルでスコープを設定します。 詳細については、「 Azure ロールを割り当てて Azure サブスクリプション リソースへのアクセスを管理する」を参照してください。
Microsoft Entra アプリケーションに所有者ロールを追加するには
- Azure portal にサインインします。
- この記事の後半で Hive クエリを実行する HDInsight クラスターがあるリソース グループに移動します。 リソース グループの数が多い場合は、フィルターを使用して目的のリソース グループを見つけることができます。
- リソース グループ メニューで [アクセス制御 (IAM)] を選択します。
- [ ロールの割り当て ] タブを選択して、現在のロールの割り当てを表示します。
- ページの上部にある [ + 追加] を選択します。
- 指示に従って、Microsoft Entra アプリケーションに所有者ロールを追加します。 ロールを正常に追加すると、アプリケーションが所有者ロールの下に一覧表示されます。
HDInsight クライアント アプリケーションを開発する
C# コンソール アプリケーションを作成します。
次の NuGet パッケージを追加します。
Install-Package Microsoft.Azure.Common.Authentication -PreInstall-Package Microsoft.Azure.Management.HDInsight -PreInstall-Package Microsoft.Azure.Management.Resources -Pre
次のコードを実行します。
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); } } }