這個快速入門會教你如何實作 MIP SDK .NET 包裝器在執行時使用的客戶端初始化模式。
備註
任何使用 MIP .NET 封裝器的 Protection SDK 的用戶端應用程式,都必須完成此快速入門中的步驟。 在應用程式初始化及驗證代理與同意代理類別實作後,依序完成這些快速啟動。
先決條件
如果您尚未這麼做,請務必:
- 完成 Microsoft 資訊保護 (MIP) SDK 設定和配置中的步驟。 這個「保護設定檔與引擎設定」快速入門有賴於正確的 SDK 安裝與設定。
- 選擇:
建立 Visual Studio 方案和專案
首先,建立並設定其他快速入門工具所建立的初始 Visual Studio 解決方案與專案。
開啟 Visual Studio 2022 或更新版本,選擇 檔案 選單, 新增, 專案。 在 [ 新增專案] 對話框中:
將 MIP 保護 SDK 的 NuGet 套件新增至您的專案:
- 在 方案總管 中,右鍵點擊專案節點,直接位於頂端解決方案節點下方,然後選擇「管理 NuGet 套件...」:
- 當 NuGet 套件管理員 索引標籤在編輯器群組的索引標籤區域中開啟時:
- 選擇[瀏覽]。
- 在搜尋方塊中輸入
Microsoft.InformationProtection。 - 選取
Microsoft.InformationProtection.Protection套件。 - 選擇安裝,當預覽變更確認對話框出現時,再選擇確定。
重複前述步驟,但改為在應用程式中新增
Microsoft.Identity.Client(MSAL)。
實作身份驗證代理和同意代理
如果尚未實作,請依照 File SDK 應用程式初始化 中的步驟實作認證與同意授權。
初始化 MIP SDK 的受控包裝函式
從 [方案總管] 中,開啟專案中包含 方法實作
Main()的 .cs 檔案。 它預設為與包含它的專案相同的名稱,此專案名稱是在您建立專案時指定的。移除生成的實作
main()。受控包裝函式包含一個用於初始化、建立
MipContext、載入設定檔,以及釋放資源的靜態類別Microsoft.InformationProtection.MIP。 若要初始化用於 Protection SDK 作業的包裝器,請呼叫MIP.Initialize(),並傳入MipComponent.Protection以載入保護作業所需的函式庫。在
Main()Program.cs中,新增以下代碼,將應用程式 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, CacheStorageType.OnDiskEncrypted);
// Create MipContext using Configuration
MipContext 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;
}
}
}
請使用以下值來替換你貼上的原始碼中的佔位值:
佔位符 價值 範例 <應用程式識別碼> 指派給「MIP SDK 設定和設定」中註冊之應用程式的Microsoft Entra 應用程式識別碼(2 個實例)。 00001111-aaaa-2222-bbbb-3333cccc4444 <友好的名稱> 為您的應用程式設定的使用者友好名稱。 應用程式初始化 現在請執行應用程式的最終組建,並解決任何錯誤。 您的程式碼應該能建置成功。
下一步
現在你的初始化程式碼完成,你就可以進入下一個快速入門,屆時你會開始體驗 MIP Protection SDK。
- 請在 GitHub 上探索 MIP Protection SDK .NET 範例。