Share via


方法: ACS 管理サービスを使用してルールとルール グループを構成する

更新日: 2015 年 6 月 19 日

適用先:Azure

適用対象

  • Microsoft Azure Active Directory アクセス制御 (アクセス制御サービスまたは ACS)

概要

ACS 管理ポータル (詳細については、ルール グループとルールを参照) または ACS 管理サービスを使用して、ACS ルールとルール グループを構成できます。 ACS 管理サービスの操作は、ACS を管理するためのカスタム ユーザー インターフェイスを構築する場合や、マルチテナント のサービスとしてのソフトウェア (SaaS) ソリューションの新しいテナントのオンボードを自動化する場合に、より効率的になります。

ACS 管理サービスを使用するルールおよびルール グループの構成手順

重要

次の手順を実行する前に、 システムが ACS の前提条件に要約されているすべての .NET フレームワークとプラットフォームの要件を満たしていることを確認します。

ACS 管理サービスを使用してルールとルール グループを構成するには、次の手順を実行します。

  • 手順 1 – ACS 構成情報を収集する

  • 手順 2 – サンプル コンソール アプリケーションを作成する

  • 手順 3 – 必要なサービスとアセンブリへの参照を追加する

  • 手順 4 – 管理サービス クライアントを実装する

  • 手順 5 - ルール グループを追加する

  • 手順 6 - ルールを追加する

手順 1 – ACS 構成情報を収集する

ACS 管理ポータルを使用して、必要な構成情報を収集できます。 ACS 管理ポータルを起動する方法の詳細については、 ACS 管理ポータルを参照してください。

ACS 構成情報を収集するには

  1. ACS 管理ポータルを起動します。 ACS 管理ポータルを起動する方法の詳細については、 ACS 管理ポータルを参照してください。

  2. ACS 管理サービス アカウントの値を取得します。 既定の ManagementClient アカウントを使用できます。 この値を表示するには、ACS 管理ポータルで、ページの左側にあるツリーの [管理] セクションの下にある [管理サービス] をクリックします。

  3. ACS 管理サービス アカウントのパスワードの値を取得します。 この値を表示するには、次の操作を実行します。

    1. ACS 管理ポータルで、ページの左側にあるツリーの [管理] セクションの下にある [管理サービス] をクリックします。

    2. [管理サービス] ページの [管理サービス アカウント] で [ManagementClient] をクリックします。

    3. [管理サービス アカウントの編集] ページの [資格情報] で [パスワード] をクリックします。

    4. [管理資格情報の編集] ページで、[パスワード] フィールドの値をコピーします。

  4. Azure portalまたは ACS 管理ポータルの URL から、Azure 名前空間の名前を取得します。 たとえば、名前 http://contoso.accesscontrol.windows.netは contoso です。

  5. ACS ホスト名を取得します。 通常、これは accesscontrol.windows.net です。

手順 2 – サンプル コンソール アプリケーションを作成する

この手順では、ACS ルール グループとルールを追加するためのコードを実行できるサンプル コンソール アプリケーションを作成します。

サンプル コンソール アプリケーションを作成するには

  1. Visual Studio 2012 を開き、インストールされているテンプレートの下に新しいコンソール アプリケーション プロジェクトWindows作成します。

  2. 次のコードを 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 – 必要なサービスとアセンブリへの参照を追加する

この手順では、サービスとアセンブリに必要な依存関係を特定して追加します。

サービスとアセンブリに必要な依存関係を追加するには

  1. [参照] を右クリックし、[参照の追加] をクリックして、参照を System.Web.Extensions に追加します。

    注意

    ソリューション エクスプローラーでサンプルのコンソール アプリケーション名を右クリックし、[プロパティ] を選択して、サンプル アプリケーションのターゲット フレームワークを .NET Framework 4 Client Profile (新しいコンソール アプリケーションの作成時に既定で割り当てられる) から .NET Framework 4 に変更しなければならない場合があります。

  2. [サービス参照] を右クリックし、[サービス参照の追加] をクリックして、管理サービスにサービス参照を追加します。 管理サービスの URL は名前空間に対して一意であり、次のような形式です。

    https:// YOURNAMESPACE.accesscontrol.windows.net/v2/mgmt/service

  3. 次の宣言を追加します。ここで、MyConsoleApplication はコンソール アプリケーションの名前で、MyServiceReference はサービス参照の名前です。

    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.Runtime.Serialization.Json; 
    using MyConsoleApplication.MyServiceReference;
    

手順 4 – 管理サービス クライアントを実装する

この手順では、管理サービス クライアントを実装します。

管理サービス クライアントを実装するには

  1. 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;
            }
    
  2. 次のコードを Program クラスに追加して、GetTokenWithWritePermission メソッドとそのヘルパー メソッドを作成します。 GetTokenWithWritePermission とそのヘルパーにより、HTTP 要求の承認ヘッダーに SWT OAuth トークンが追加されます。

    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 - ルール グループを追加する

この手順では、上記の手順で作成した管理サービス クライアントを使用して、ルール グループを追加します。

ルール グループを追加するには

  1. 次のコードを Program クラスの Main メソッドに追加して、管理サービス クライアントを初期化します。

    ManagementService svc = CreateManagementServiceClient();
    
  2. 新しいルール グループ (以下のコードに示されているように "mygroup" という名前を付けることができます) を追加し、以下のコードを Program クラスの Main メソッドに追加して変更を保存します。

    RuleGroup rg = new RuleGroup();
                rg.Name = "mygroup";
                svc.AddToRuleGroups(rg);
                svc.SaveChanges(SaveChangesOptions.Batch);
    

手順 6 - ルールを追加する

この手順では、ACS 管理サービスを使用して、前の手順で作成したルール グループにルールを追加します。

ルールを追加するには

  1. Program クラスの Main メソッドに次のコードを追加して、Access Control名前空間名前空間を表す組み込みの発行者名である "LOCAL AUTHORITY" の変数を確立します。

    // "LOCAL AUTHORITY" is a built-in IDP name that represents the Access Control namespace. 
    Issuer localAuthority = svc.Issuers.Where(m => m.Name == "LOCAL AUTHORITY").FirstOrDefault();
    
  2. 次のいずれかの操作を行います。

    1. 基本ルールを追加するには、Program クラスの Main メソッドに以下のコードを追加します。

                  //EXAMPLE #1 - BASIC RULE
                  Rule basicRule = new Rule()
                  {
                      InputClaimType = "https://acs/your-input-type",
                      InputClaimValue = "inputValue",
                      OutputClaimType = "https://acs/your-output-type",
                      OutputClaimValue = "outputValue",
                  };
      
                  basicRule.Description = string.Format(CultureInfo.InvariantCulture,
                      "Transforms claim from {0} with type: {1}, value: {2}, into a new claim with type: {3}, value:{4}",
                      "ACS",
                      basicRule.InputClaimType,
                      basicRule.InputClaimValue,
                      basicRule.OutputClaimType,
                      basicRule.OutputClaimValue);
      
                  svc.AddToRules(basicRule);
                  svc.SetLink(basicRule, "RuleGroup", rg);
                  svc.SetLink(basicRule, "Issuer", localAuthority);                                              
                    svc.SaveChanges(SaveChangesOptions.Batch);
      
    2. 特定の入力方向の要求と値を変更せずにアプリケーションに渡すルールを追加するには、Program クラスの Main メソッドに以下のコードを追加します。

      //EXAMPLE #2 - PASS TYPE AND VALUE RULE
                  Rule passSpecificClaimRule = new Rule()
                  {
                      InputClaimType = "https://acs/your-input-type2",
                      InputClaimValue = "inputValue2",
                  };
      
                  passSpecificClaimRule.Description = string.Format(CultureInfo.InvariantCulture,
                      "Passthough claim from {0} with type: {1}, value: {2}",
                      "ACS",
                      passSpecificClaimRule.InputClaimType,
                      passSpecificClaimRule.InputClaimValue);
      
                  svc.AddToRules(passSpecificClaimRule);
                  svc.SetLink(passSpecificClaimRule, "RuleGroup", rg);
                  svc.SetLink(passSpecificClaimRule, "Issuer", localAuthority); 
      svc.SaveChanges(SaveChangesOptions.Batch);
      
    3. 指定した種類の要求を渡すルールを追加するには、Program クラスの Main メソッドに以下のコードを追加します。

      //EXAMPLE #3 PASS SPECIFIC TYPE RULE
                  Rule passAnyClaimSpecificTypeRule = new Rule()
                  {
                      InputClaimType = "https://acs/your-input-type3",
                  };
      
                  passAnyClaimSpecificTypeRule.Description = string.Format(CultureInfo.InvariantCulture,
                      "Pass claim from {0} with type: {1}, and any value",
                      "ACS",
                      passSpecificClaimRule.InputClaimType);
      
                  svc.AddToRules(passAnyClaimSpecificTypeRule);
                  svc.SetLink(passAnyClaimSpecificTypeRule, "RuleGroup", rg);
                  svc.SetLink(passAnyClaimSpecificTypeRule, "Issuer", localAuthority); 
      svc.SaveChanges(SaveChangesOptions.Batch);
      
    4. 指定した値の入力方向の要求を渡すルールを追加するには、Program クラスの Main メソッドに以下のコードを追加します。

      //EXAMPLE #4 PASS ANY CLAIM W/SPECIFIC VALUE RULE
                  Rule passAnyClaimSpecificValueRule = new Rule()
                  {
                      InputClaimValue = "inputValue3",
                  };
      
                  passAnyClaimSpecificValueRule.Description = string.Format(CultureInfo.InvariantCulture,
                      "Pass claim from {0} with any type, and specific value {1}",
                      "ACS",
                      passSpecificClaimRule.InputClaimValue);
      
                  svc.AddToRules(passAnyClaimSpecificValueRule);
                  svc.SetLink(passAnyClaimSpecificValueRule, "RuleGroup", rg);
                  svc.SetLink(passAnyClaimSpecificValueRule, "Issuer", localAuthority); 
      svc.SaveChanges(SaveChangesOptions.Batch);
      
    5. 指定した入力方向の要求の種類を別の出力方向の要求の種類に変換しますが、要求の値を変更しないルールを追加するには、Program クラスの Main メソッドに以下のコードを追加します。

      //EXAMPLE #5 COMPLEX RULE
                  Rule complexTransformationRule = new Rule()
                  {
                      InputClaimType = "https://acs/your-input-type4",
                      OutputClaimType = "https://acs/your-output-type2",
                  };
      
                  complexTransformationRule.Description = string.Format(CultureInfo.InvariantCulture,
                      "Transforms claim from {0} with type: {1}, and any value, into a new claim with type: {2}, keeping(passingthough) old value",
                      "ACS",
                      complexTransformationRule.InputClaimType,
                      complexTransformationRule.OutputClaimType);
      
                  svc.AddToRules(complexTransformationRule);
                  svc.SetLink(complexTransformationRule, "RuleGroup", rg);
                  svc.SetLink(complexTransformationRule, "Issuer", localAuthority);
      
                  svc.SaveChanges(SaveChangesOptions.Batch);
      

参照

概念

ACS の利用方法