更新日: 2015 年 6 月 19 日
適用対象: Azure
適用対象
- Microsoft Azure Active Directory アクセス制御 (アクセス制御サービスまたは ACS とも呼ばれます)
概要
ACS 証明書利用者アプリケーションは、ACS 管理ポータル ( 詳細については、「証明書利用者アプリケーション」を参照) または ACS 管理サービスを使用して構成できます。 ACS を管理するためのカスタム ユーザー インターフェイスを構築している場合、またはマルチテナントの SaaS (Software as a Service) ソリューションの新しいテナントのオンボードを自動化する場合は、ACS 管理サービスの使用をより効率的に行うことができます。
ACS 管理サービスを使用して証明書利用者アプリケーションを構成する手順
Von Bedeutung
次の手順を実行する前に、 システムが ACS の前提条件にまとめられている .NET Framework とプラットフォームのすべての要件を満たしていることを確認してください。
ACS 管理サービスを使用して証明書利用者アプリケーションを構成するには、次の手順を実行します。
ステップ 1 – ACS 設定情報の収集
ステップ 2 – サンプルコンソールアプリケーションを作成する
手順 3 – 必要なサービスとアセンブリへの参照を追加する
手順 4 – 管理サービス クライアントを実装する
手順 5 – 証明書利用者アプリケーションを追加する
ステップ 1 – ACS 設定情報の収集
ACS 管理ポータルを使用して、必要な構成情報を収集できます。 ACS 管理ポータルを起動する方法の詳細については、「 ACS 管理ポータル」を参照してください。
ACS 設定情報を収集するには
ACS 管理ポータルを起動します。 ACS 管理ポータルを起動する方法の詳細については、「 ACS 管理ポータル」を参照してください。
ACS 管理サービス アカウントの値を取得します。 デフォルトの ManagementClient アカウントを使用できます。 この値を表示するには、ACS 管理ポータルで、ページの左側にあるツリーの [管理] セクションの下にある [管理サービス] をクリックします。
ACS 管理サービス アカウントのパスワードの値を取得します。 この値を表示するには、次の手順を実行します。
ACS 管理ポータルで、ページの左側にあるツリーの [管理] セクションの下にある [管理サービス] をクリックします。
[管理サービス] ページで、[管理サービス アカウント] の下の [管理クライアント] をクリックします。
[ 管理サービス アカウントの編集 ] ページの [資格情報] で、[ パスワード] をクリックします。
[ Edit Management Credential ] ページで、[ Password ] フィールドの値をコピーします。
Azure 名前空間の名前を取得します。 この値は、Azure ポータルまたは ACS 管理ポータルの URL から取得できます。 たとえば、
http://contoso.accesscontrol.windows.netでは、名前空間名は contoso です。ACS ホスト名を取得します。 通常は accesscontrol.windows.net です。
ステップ 2 – サンプルコンソールアプリケーションを作成する
この手順では、ACS ルール グループとルールを追加するためのコードを実行できるサンプル コンソール アプリケーションを作成します。
サンプルのコンソールアプリケーションを作成するには
Visual Studio 2012を開き、 Windows のインストール済みテンプレートの下に新しいコンソールアプリケーションプロジェクトを作成します。
次のコードを Program クラスに追加し、前の手順で収集した適切な構成情報に serviceIdentityPasswordForManagement、serviceNamespace、acsHostName 変数を割り当てます。
public const string serviceIdentityUsernameForManagement = "ManagementClient"; public const string serviceIdentityPasswordForManagement = "My Password/Key for ManagementClient"; public const string serviceNamespace = "MyNameSpaceNoDots"; public const string acsHostName = "accesscontrol.windows.net"; public const string acsManagementServicesRelativeUrl = "v2/mgmt/service/"; static string cachedSwtToken;
手順 3 – 必要なサービスとアセンブリへの参照を追加する
この手順では、必要な依存関係を特定し、サービスとアセンブリに追加します。
必要な依存関係をサービスとアセンブリに追加するには
[参照] を右クリックし、[参照の追加] をクリックして、System.Web.Extensions への参照を追加します。
注
ソリューション エクスプローラーでサンプル コンソール アプリケーション名を右クリックし、[プロパティ] を選択して、サンプル アプリケーションのターゲット フレームワークを .NET Framework 4 クライアント プロファイル (新しいコンソール アプリケーションを作成するときに既定で割り当てられる) から .NET Framework 4 に変更することが必要な場合があります。
[ サービス参照] を右クリックし、[ サービス参照の追加] をクリックして、管理サービスにサービス参照を追加します。 管理サービスの URL は名前空間に固有で、次のようになります。
https:// YOURNAMESPACE.accesscontrol.windows.net/v2/mgmt/service
次の宣言を追加します ( MyConsoleApplication はコンソール アプリケーションの名前です)。
using System; using System.IO; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.Net; using System.Data.Services.Client; using System.Collections.Specialized; using System.Web.Script.Serialization; using System.Globalization; using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; using MyConsoleApplication.MyServiceReference;
手順 4 – 管理サービス クライアントを実装する
この手順では、管理サービス クライアントを実装します。
管理サービス クライアントを実装するには
Program クラスに次のメソッドを追加します。
public static ManagementService CreateManagementServiceClient() { string managementServiceEndpoint = String.Format(CultureInfo.InvariantCulture, "https://{0}.{1}/{2}", serviceNamespace, acsHostName, acsManagementServicesRelativeUrl); ManagementService managementService = new ManagementService(new Uri(managementServiceEndpoint)); managementService.SendingRequest += GetTokenWithWritePermission; return managementService; }GetTokenWithWritePermission メソッドとそのヘルパー メソッドを Program クラスに追加します。 GetTokenWithWritePermission とそのヘルパーは、SWT OAuth トークンを HTTP 要求の Authorization ヘッダーに追加します。
public static void GetTokenWithWritePermission(object sender, SendingRequestEventArgs args) { GetTokenWithWritePermission((HttpWebRequest)args.Request); } public static void GetTokenWithWritePermission(HttpWebRequest args) { if (cachedSwtToken == null) { cachedSwtToken = GetTokenFromACS(); } args.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + cachedSwtToken); } private static string GetTokenFromACS() { // // Request a token from ACS // WebClient client = new WebClient(); client.BaseAddress = string.Format(CultureInfo.CurrentCulture, "https://{0}.{1}", serviceNamespace, acsHostName); NameValueCollection values = new NameValueCollection(); values.Add("grant_type", "client_credentials"); values.Add("client_id", serviceIdentityUsernameForManagement); values.Add("client_secret", serviceIdentityPasswordForManagement); values.Add("scope", client.BaseAddress + acsManagementServicesRelativeUrl); byte[] responseBytes = client.UploadValues("/v2/OAuth2-13", "POST", values); string response = Encoding.UTF8.GetString(responseBytes); // Parse the JSON response and return the access token JavaScriptSerializer serializer = new JavaScriptSerializer(); Dictionary<string, object> decodedDictionary = serializer.DeserializeObject(response) as Dictionary<string, object>; return decodedDictionary["access_token"] as string; }
手順 5 – 証明書利用者アプリケーションを追加する
この手順では、SAML 2.0 トークン形式 (既定のオプション)、トークン暗号化ポリシーなし (既定のオプション)、Windows Live ID (Microsoft アカウント) ID プロバイダー (既定のオプション)、トークンの有効期間 600 秒 (既定のオプション)、および証明書利用者アプリケーション用のカスタム X.509 トークン署名証明書または Access Control 名前空間用のトークン署名証明書を使用して、サンプル証明書利用者アプリケーションを作成します。
Access Control 名前空間トークン署名証明書を持つ証明書利用者アプリケーションを追加するには
管理サービス クライアントを初期化するには、Program クラスの Main メソッドに次のコードを追加します。
ManagementService svc = CreateManagementServiceClient();新しい証明書利用者アプリケーション (次のコードに示すように、"MyRelyingPartyApplication" と呼ぶことができます) を追加し、Program クラスの Main メソッドに次のコードを追加して変更を保存します。
注
「あなたの.PFX ファイル」と、X.509 証明書への有効なフルパスを含めてください。 たとえば、ACS2ClientCertificate.cer という証明書が C: で保存されている場合、正しい値は "C:\ ACS2ClientCertificate.cer" です。
次のコードの "MyCertificatePassword" を X.509 証明書の正しいパスワードに置き換えます。//Create Relying Party Application RelyingParty relyingParty = new RelyingParty() { Name = "MyRelyingPartyApplication", AsymmetricTokenEncryptionRequired = false, TokenType = "SAML_2_0", TokenLifetime = 3600 }; svc.AddToRelyingParties(relyingParty); //Create the Realm Address RelyingPartyAddress realmAddress = new RelyingPartyAddress() { Address = "http://TestRelyingParty.com/Realm", EndpointType = "Realm" }; svc.AddRelatedObject(relyingParty, "RelyingPartyAddresses", realmAddress); //Create the Return URL Address RelyingPartyAddress replyAddress = new RelyingPartyAddress() { Address = "http://TestRelyingParty.com/Reply", EndpointType = "Reply" }; svc.AddRelatedObject(relyingParty, "RelyingPartyAddresses", replyAddress); // Create a Rule Group for This Relying Party Application RuleGroup rg = new RuleGroup(); rg.Name = "SampleRuleGroup For " + relyingParty.Name; svc.AddToRuleGroups(rg); // Assign This New Rule Group to Your New Relying Party Application RelyingPartyRuleGroup relyingPartyRuleGroup = new RelyingPartyRuleGroup(); svc.AddToRelyingPartyRuleGroups(relyingPartyRuleGroup); svc.AddLink(relyingParty, "RelyingPartyRuleGroups", relyingPartyRuleGroup); svc.AddLink(rg, "RelyingPartyRuleGroups", relyingPartyRuleGroup); //Save Your New Relying Party Application svc.SaveChanges(SaveChangesOptions.Batch);
専用のトークン署名証明書を持つ証明書利用者アプリケーションを追加するには
管理サービス クライアントを初期化するには、Program クラスの Main メソッドに次のコードを追加します。
ManagementService svc = CreateManagementServiceClient();ヘルパー関数 ReadBytesFromPfxFile を作成して、X.509 証明書からバイトを読み取るには、Program クラスに次のコードを追加します。
//Helper Function to Read Bytes from Your .pfx file public static byte[] ReadBytesFromPfxFile(string pfxFileName, string protectionPassword) { byte[] signingCertificate; using (FileStream stream = File.OpenRead(pfxFileName)) { using (BinaryReader br = new BinaryReader(stream)) { signingCertificate = br.ReadBytes((int)stream.Length); } } return signingCertificate; }新しい証明書利用者アプリケーション (次のコードに示すように、"MyRelyingPartyApplication" と呼ぶことができます) を追加し、Program クラスの Main メソッドに次のコードを追加して変更を保存します。
注
「あなたの.PFX ファイル」と、X.509 証明書への有効なフルパスを含めてください。 たとえば、ACS2ClientCertificate.cer という証明書が C: で保存されている場合、正しい値は "C:\ ACS2ClientCertificate.cer" です。
次のコードの "MyCertificatePassword" を X.509 証明書の正しいパスワードに置き換えます。//Create Relying Party Application RelyingParty relyingParty = new RelyingParty() { Name = "MyRelyingPartyApplication", AsymmetricTokenEncryptionRequired = false, TokenType = "SAML_2_0", TokenLifetime = 3600 }; svc.AddToRelyingParties(relyingParty); //Create the Realm Address RelyingPartyAddress realmAddress = new RelyingPartyAddress() { Address = "http://TestRelyingParty.com/Realm", EndpointType = "Realm" }; svc.AddRelatedObject(relyingParty, "RelyingPartyAddresses", realmAddress); //Create the Return URL Address RelyingPartyAddress replyAddress = new RelyingPartyAddress() { Address = "http://TestRelyingParty.com/Reply", EndpointType = "Reply" }; svc.AddRelatedObject(relyingParty, "RelyingPartyAddresses", replyAddress); //Create a Signing Certificate X509Certificate2 cert = new X509Certificate2(@"Full path to your .PFX file", "MyCertificatePassword"); DateTime startDate, endDate; startDate = cert.NotBefore.ToUniversalTime(); endDate = cert.NotAfter.ToUniversalTime(); string pfxFileName = @"Full path to your .PFX file"; string pfxPassword = @"MyCertificatePassword"; byte[] signingCertificate = ReadBytesFromPfxFile(pfxFileName, pfxPassword); RelyingPartyKey relyingPartyKey = new RelyingPartyKey() { StartDate = startDate.ToUniversalTime(), EndDate = endDate.ToUniversalTime(), Type = "X509Certificate", Usage = "Signing", IsPrimary = true, Value = signingCertificate, Password = Encoding.UTF8.GetBytes("MyCertificatePassword") }; svc.AddRelatedObject(relyingParty, "RelyingPartyKeys", relyingPartyKey); // Create a Rule Group for This Relying Party Application RuleGroup rg = new RuleGroup(); rg.Name = "SampleRuleGroup For " + relyingParty.Name; svc.AddToRuleGroups(rg); // Assign This New Rule Group to Your New Relying Party Application RelyingPartyRuleGroup relyingPartyRuleGroup = new RelyingPartyRuleGroup(); svc.AddToRelyingPartyRuleGroups(relyingPartyRuleGroup); svc.AddLink(relyingParty, "RelyingPartyRuleGroups", relyingPartyRuleGroup); svc.AddLink(rg, "RelyingPartyRuleGroups", relyingPartyRuleGroup); //Save Your New Relying Party Application svc.SaveChanges(SaveChangesOptions.Batch);