アプリケーションの開発

重要

2020 年 3 月より前にリリースされた Microsoft Rights Management Service SDK のバージョンは非推奨です。以前のバージョンを使用するアプリケーションは、2020 年 3 月のリリースを使用するように更新する必要があります。 詳細については、 非推奨の通知を参照してください。

Microsoft Rights Management Service SDK の追加の機能強化は計画されていません。 分類、ラベル付け、保護サービスにMicrosoft Information Protection SDK を導入することを強くお勧めします。

この例では、Azure Information Protection サービス (AIP) と連携する簡単なコンソール アプリケーションを作成します。 保護するドキュメントのパスを入力し、アドホック ポリシーまたは Azure テンプレートを使用してドキュメントを保護する手順を説明します。 アプリケーションは入力に従って正しいポリシーを適用し、情報が保護されたドキュメントを作成します。 使用するサンプル コードは、Azure IP テスト アプリケーションおよび Github を参照してください。

サンプル アプリの前提条件

  • オペレーティング システム: Windows 10、Windows 8、Windows 7、Windows Server 2008、Windows Server 2008 R2、Windows Server 2012 のいずれか
  • プログラミング言語: C# (.NET Framework 3.0 以降)
  • 開発環境: Visual Studio 2015 (およびそれ以降)

Azure 構成のセットアップ

このアプリ用に Azure をセットアップするには、テナント ID、対称キー、およびアプリケーションのプリンシパル ID を作成する必要があります。

Azure AD テナントの構成

Azure Information Protection 用に Azure AD 環境を構成するには、「Azure Information Protection から保護サービスをアクティブ化する」のガイダンスに従います。

サービスをアクティブにすると、次の手順で PowerShell コンポーネントが必要になります。 これを実現するには、PowerShell を使用して Azure Information Protection からの保護の管理に従います。

テナント ID を取得する

  • 管理者として PowerShell を実行します。
  • RMS モジュールをインポートします: Import-Module AIPService
  • 割り当てられたユーザーの資格情報でサービスに接続します: Connect-AipService –Verbose
  • RMS が有効になっていることを確認します: enable-aipservice
  • Get-AipServiceConfiguration を実行してテナント ID を取得します。

BPOS ID (テナント ID) 値を記録します。 後の手順で必要になります。

出力の例Get-AadrmConfiguration コマンドレットの出力

  • サービスから切断します: Disconnect-AipServiceService

サービス プリンシパルの作成

サービス プリンシパルを作成するには、次の手順に従います。

サービス プリンシパルは、アクセス制御のためにグローバルに構成された資格情報です。サービス プリンシパルを使用するサービスでは、Microsoft Azure AD による認証および Microsoft Azure AD Rights Management を使用した情報の保護が可能になります。

  • 管理者として PowerShell を実行します。
  • Import-Module MSOnline を使用して Microsoft Azure AD モジュールをインポートします。
  • 割り当てられたユーザーの資格情報でオンライン サービスに接続します: Connect-MsolService
  • New-MsolServicePrincipal を実行して、新しいサービス プリンシパルを作成します。
  • サービス プリンシパルの名前を入力します。

    後で使用するために、対称キーとアプリケーションのプリンシパル ID を記録します。

出力の例NewMsolServicePrincipal コマンドレットの出力

  • アプリケーションのプリンシパル ID、対称キー、およびテナント ID をアプリケーションの App.config ファイルに追加します。

App.config ファイルの例 App.config ファイルの例

  • アプリケーションを Azure に登録した時点から、 ClientIDRedirectUri を使用できるようになります。 Azure にアプリケーションを登録する方法、および ClientIDRedirectUri の取得方法の詳細については、「Azure RMS の ADAL 認証を構成する」を参照してください。

設計の概要

次の図は、作成するアプリのアーキテクチャとプロセス フローを示しています。図の下には手順を示しています。 デザインの概要

  1. ユーザーは次を入力します。
    • 保護されるファイルのパス。
    • テンプレートを選択するか、またはアドホック ポリシーを作成します。
  2. アプリケーションが AIP による認証を要求します。
  3. AIP が認証を確認します。
  4. アプリケーションが AIP のテンプレートを要求します。
  5. AIP が事前に定義されたテンプレートを返します。
  6. アプリケーションは、指定された場所の指定されたファイルを検索します。
  7. アプリケーションは、そのファイルに AIP 保護ポリシーを適用します。

コードの動作

サンプルでは、Iprotect.cs ファイルを使用してソリューションの Azure IP テストを開始します。 Azure IP テストは C# コンソール アプリケーションであり、他の AIP 対応アプリケーションと同様に、main() メソッドで示されるように MSIPC.dll の読み込みで開始します。

//Loads MSIPC.dll
SafeNativeMethods.IpcInitialize();
SafeNativeMethods.IpcSetAPIMode(APIMode.Server);

Azure への接続に必要なパラメーターを読み込みます。

//Loads credentials for the service principal from App.Config
SymmetricKeyCredential symmetricKeyCred = new SymmetricKeyCredential();
symmetricKeyCred.AppPrincipalId = ConfigurationManager.AppSettings["AppPrincipalId"];
symmetricKeyCred.Base64Key = ConfigurationManager.AppSettings["Base64Key"];
symmetricKeyCred.BposTenantId = ConfigurationManager.AppSettings["BposTenantId"];

コンソール アプリケーションにファイルのパスを入力すると、アプリケーションはドキュメントが既に暗号化されているかどうかを確認します。 このメソッドは SafeFileApiNativeMethods クラスです。

var checkEncryptionStatus = SafeFileApiNativeMethods.IpcfIsFileEncrypted(filePath);

ドキュメントが暗号化されていない場合は、プロンプトで入力された選択に従ってドキュメントの暗号化処理が行われます。

if (!checkEncryptionStatus.ToString().ToLower().Contains(alreadyEncrypted))
{
  if (method == EncryptionMethod1)
  {
    //Encrypt a file via AIP template
    ProtectWithTemplate(symmetricKeyCred, filePath);

  }
  else if (method == EncryptionMethod2)
  {
    //Encrypt a file using ad-hoc policy
    ProtectWithAdHocPolicy(symmetricKeyCred, filePath);
  }
}

テンプレート オプションによる保護では、サーバーからテンプレートの一覧が取得され、選択するオプションがユーザーに提供されます。

テンプレートを変更していない場合は、AIP から既定のテンプレートを取得します。

public static void ProtectWithTemplate(SymmetricKeyCredential symmetricKeyCredential, string filePath)
{
  // Gets the available templates for this tenant
  Collection<TemplateInfo> templates = SafeNativeMethods.IpcGetTemplateList(null, false, true,
      false, true, null, null, symmetricKeyCredential);

  //Requests tenant template to use for encryption
  Console.WriteLine("Please select the template you would like to use to encrypt the file.");

  //Outputs templates available for selection
  int counter = 0;
  for (int i = 0; i < templates.Count; i++)
  {
    counter++;
    Console.WriteLine(counter + ". " + templates.ElementAt(i).Name + "\n" +
        templates.ElementAt(i).Description);
  }

  //Parses template selection
  string input = Console.ReadLine();
  int templateSelection;
  bool parseResult = Int32.TryParse(input, out templateSelection);

  //Returns error if no template selection is entered
  if (parseResult)
  {
    //Ensures template value entered is valid
    if (0 < templateSelection && templateSelection <= counter)
    {
      templateSelection -= templateSelection;

      // Encrypts the file using the selected template
      TemplateInfo selectedTemplateInfo = templates.ElementAt(templateSelection);

      string encryptedFilePath = SafeFileApiNativeMethods.IpcfEncryptFile(filePath,
          selectedTemplateInfo.TemplateId,
          SafeFileApiNativeMethods.EncryptFlags.IPCF_EF_FLAG_KEY_NO_PERSIST, true, false, true, null,
          symmetricKeyCredential);
    }
  }
}

アドホック ポリシーを選択した場合、アプリケーションのユーザーは権限を持つユーザーのメール アドレスを入力する必要があります。 このセクションでは、IpcCreateLicenseFromScratch() メソッドを使用し、テンプレートに新しいポリシーを適用してライセンスが作成されます。

if (issuerDisplayName.Trim() != "")
{
  // Gets the available issuers of rights policy templates.
  // The available issuers is a list of RMS servers that this user has already contacted.
  try
  {
    Collection<TemplateIssuer> templateIssuers = SafeNativeMethods.IpcGetTemplateIssuerList(
                                                    null,
                                                    true,
                                                    false,
                                                    false, true, null, symmetricKeyCredential);

    // Creates the policy and associates the chosen user rights with it
    SafeInformationProtectionLicenseHandle handle = SafeNativeMethods.IpcCreateLicenseFromScratch(
                                                        templateIssuers.ElementAt(0));
    SafeNativeMethods.IpcSetLicenseOwner(handle, owner);
    SafeNativeMethods.IpcSetLicenseUserRightsList(handle, userRights);
    SafeNativeMethods.IpcSetLicenseDescriptor(handle, new TemplateInfo(null, CultureInfo.CurrentCulture,
                                                            policyName,
                                                            policyDescription,
                                                            issuerDisplayName,
                                                            false));

    //Encrypts the file using the ad hoc policy
    string encryptedFilePath = SafeFileApiNativeMethods.IpcfEncryptFile(
                                    filePath,
                                    handle,
                                    SafeFileApiNativeMethods.EncryptFlags.IPCF_EF_FLAG_KEY_NO_PERSIST,
                                    true,
                                    false,
                                    true,
                                    null,
                                    symmetricKeyCredential);
    }
}

ユーザー操作の例

すべて作成して実行すると、アプリケーションの出力は次のようになります。

  1. 暗号化方法を選択するように求められます。 アプリの出力 - 手順 1

  2. 保護されるファイルのパスを入力するよう求められます。 アプリの出力 - 手順 2

  3. ライセンス所有者のメール アドレスを入力するよう求められます (この所有者は、Azure AD テナントでグローバル管理者の権限を持つ必要があります)。 アプリの出力 - 手順 3

  4. ファイルへのアクセス権を持つユーザーのメール アドレスを入力します (メール アドレスはスペースで区切る必要があります)。 アプリの出力 - 手順 4

  5. 承認されたユーザーに与えられる権限の一覧から選択します。 アプリの出力 - 手順 5

  6. 最後に、ポリシー名、説明、発行者 (Azure AD テナント) の表示名アプリの出力というポリシー メタデータを入力します - 手順 6