次の方法で共有


クイック スタート: 保護 SDK のクライアント アプリケーションの初期化 (C#)

このクイック スタートでは、実行時に MIP SDK .NET ラッパーによって使用されるクライアント初期化パターンを実装する方法について説明します。

このクイック スタートで説明する手順は、MIP .NET ラッパーの Protection SDK を使用するすべてのクライアント アプリケーションに必要です。 これらのクイック スタートは、アプリケーションの初期化と認証デリゲートクラスと同意デリゲート クラスの実装後に順次実行する必要があります。

[前提条件]

まだ行っていない場合は、次の手順を実行してください。

Visual Studio ソリューションとプロジェクトを作成する

最初に、Visual Studio のソリューションとプロジェクトを作成して構成します。このソリューションを基に、他のクイックスタートガイドが構築されます。

  1. Visual Studio 2019 以降を開き、[ ファイル ] メニューの [ 新規作成]、[ プロジェクト] の順に選択します。 [ 新しいプロジェクト ] ダイアログで、次の手順を実行します。

    • 左側のウィンドウの [ インストール済みVisual C#] で、[ Windows デスクトップ] を選択します。

    • 中央のウィンドウで、コンソール アプリ (.NET Framework) を選択します。

    • 下部のウィンドウで、プロジェクト 場所、および含まれている ソリューション名 を適宜更新します。

    • 完了したら、右下にある [OK] ボタンをクリックします。

      Visual Studio ソリューションの作成

  2. MIP ファイル SDK の NuGet パッケージをプロジェクトに追加します。

    • ソリューション エクスプローラーで、プロジェクト ノード (上部またはソリューション ノードのすぐ下) を右クリックし、[NuGet パッケージの管理]を選択します。..:
    • [エディター グループ] タブ領域で [NuGet パッケージ マネージャー ] タブが開いたとき:
      • を選択し、を参照します。
      • 検索ボックスに「Microsoft.InformationProtection」と入力します。
      • "Microsoft.InformationProtection.File" パッケージを選択します。
      • [インストール] をクリックし、[ プレビューの変更 の確認] ダイアログが表示されたら [OK] をクリックします。
  3. MIP Protection SDK パッケージを追加するために上記の手順を繰り返しますが、代わりに "Microsoft.IdentityModel.Clients.ActiveDirectory" をアプリケーションに追加します。

まだ実装されていない場合は、認証と同意デリゲートを実装するための File SDK アプリケーションの初期化 に関するページに記載されている手順に従います。

MIP SDK マネージド ラッパーを初期化する

  1. ソリューション エクスプローラーでMain() メソッドの実装を含むプロジェクト内の.cs ファイルを開きます。 既定の名前は、それが含まれるプロジェクトと同じであり、プロジェクトの作成時に指定したものです。

  2. 生成された main()の実装を削除します。

  3. マネージド ラッパーには静的クラスが含まれています。 Microsoft.InformationProtection.MIP 初期化、 MipContextの作成、プロファイルの読み込み、リソースの解放に使用されます。 File SDK 操作のラッパーを初期化するには、 MIP.Initialize()を呼び出し、 MipComponent.Protection を渡して保護操作に必要なライブラリを読み込みます。

  4. Main()内のProgram.csに以下を追加し、<application-id>を、前に作成したMicrosoft Entraアプリケーション登録のIDに置き換えてください。

using System;
using System.Threading.Tasks;
using Microsoft.InformationProtection;
using Microsoft.InformationProtection.Exceptions;
using Microsoft.InformationProtection.Protection;

namespace mip_sdk_dotnet_quickstart
{
    class Program
    {
        private const string clientId = "<application-id>";
        private const string appName = "<friendly-name>";

        static void Main(string[] args)
        {
            //Initialize Wrapper for Protection SDK operations
            MIP.Initialize(MipComponent.Protection);
        }
    }
}

保護プロファイルとエンジンの構築

前述のように、MIP API を使用する SDK クライアントにはプロファイル オブジェクトとエンジン オブジェクトが必要です。 ネイティブ DLL を読み込むコードを追加し、プロファイルとエンジン オブジェクトをインスタンス化して、このクイック スタートのコーディング部分を完了します。

using System;
using System.Threading.Tasks;
using Microsoft.InformationProtection;
using Microsoft.InformationProtection.Exceptions;
using Microsoft.InformationProtection.Protection;

namespace mip_sdk_dotnet_quickstart
{
     class Program
     {
          private const string clientId = "<application-id>";
          private const string appName = "<friendly-name>";

          static void Main(string[] args)
          {
               // Initialize Wrapper for Protection SDK operations.
               MIP.Initialize(MipComponent.Protection);

               // Create ApplicationInfo, setting the clientID from Azure AD App Registration as the ApplicationId.
               ApplicationInfo appInfo = new ApplicationInfo()
               {
                    ApplicationId = clientId,
                    ApplicationName = appName,
                    ApplicationVersion = "1.0.0"
               };

               // Instantiate the AuthDelegateImpl object, passing in AppInfo.
               AuthDelegateImplementation authDelegate = new AuthDelegateImplementation(appInfo);

               // Create MipConfiguration Object
               MipConfiguration mipConfiguration = new MipConfiguration(appInfo, "mip_data", LogLevel.Trace, false);

               // Create MipContext using Configuration
               mipContext = MIP.CreateMipContext(mipConfiguration);
                
               // Initialize and instantiate the ProtectionProfile.
               // Create the ProtectionProfileSettings object.
               // Initialize protection profile settings to create/use local state.
               var profileSettings = new ProtectionProfileSettings(mipContext,
                                        CacheStorageType.OnDiskEncrypted,                                        
                                        new ConsentDelegateImplementation());

               // Load the Profile async and wait for the result.
               var protectionProfile = Task.Run(async () => await MIP.LoadProtectionProfileAsync(profileSettings)).Result;

               // Create a ProtectionEngineSettings object, then use that to add an engine to the profile.
               var engineSettings = new ProtectionEngineSettings("user1@tenant.com", authDelegate, "", "en-US");
               engineSettings.Identity = new Identity("user1@tenant.com");
               var protectionEngine = Task.Run(async () => await protectionProfile.AddEngineAsync(engineSettings)).Result;

               // Application Shutdown
               // handler = null; // This will be used in later quick starts.
               protectionEngine = null;
               protectionProfile = null;
               mipContext = null;
          }
     }
}
  1. 次の値を使用して、貼り付けたソース コードのプレースホルダーの値を置き換えます。

    プレースホルダー 価値
    <application-id> "MIP SDK のセットアップと構成" に登録されているアプリケーションに割り当てられた Microsoft Entra アプリケーション ID (2 つのインスタンス)。 0edbblll-8773-44de-b87c-b8c6276d41eb
    <フレンドリーネーム> アプリケーションのユーザー定義のフレンドリ名。 アプリ初期化
  2. 次に、アプリケーションの最終的なビルドを行い、エラーを解決します。 コードが正常にビルドされます。

次のステップ

初期化コードが完了したら、次のクイック スタートに向けて準備が整いました。ここでは、MIP Protection SDK の使用を開始します。