本快速入門將示範如何在運行時間實作 MIP SDK .NET 包裝函式所使用的用戶端初始化模式。
備註
使用 MIP .NET 包裝函式檔案、原則或保護 SDK 的任何用戶端應用程式都需要本快速入門中所述的步驟。 雖然本快速入門示範檔案 SDK 的使用方式,但這個相同的模式適用於使用原則和保護 SDK 的用戶端。 未來的快速開始指南應該循序漸進完成,因為每個指南都是以上一個為基礎而建置,因為這是第一個快速開始指南。 此程式代碼旨在示範如何開始使用 MIP SDK, 不適用於生產環境。
先決條件
如果您尚未這麼做,請務必:
- 完成 Microsoft 資訊保護 (MIP) SDK 設定和配置中的步驟。 本「用戶端應用程式初始化」快速入門依賴適當的 SDK 安裝和設定。
- 選擇:
建立 Visual Studio 方案和專案
首先,我們會建立並設定初始的 Visual Studio 方案和專案,其他的快速入門指南將以此為基礎進行建置。
開啟 Visual Studio 2019,選取 [ 檔案 ] 功能表 [ 新增]、[ 專案]。 在 [ 新增專案] 對話框中:
將 MIP 檔案 SDK 的 NuGet 套件新增至您的專案:
- 在 [方案總管] 中,以滑鼠右鍵按兩下項目節點(直接在頂端/方案節點底下),然後選取 [ 管理 NuGet 套件...] :
- 當 NuGet 套件管理員 索引標籤在編輯器群組的索引標籤區域中開啟時:
- 選擇瀏覽。
- 在搜尋方塊中輸入 「Microsoft.InformationProtection」。。
- 選取 「Microsoft.InformationProtection.File」 套件。
- 按兩下 [安裝],然後在 [預覽變更 確認] 對話框顯示時按兩下 [確定]。
重複上述步驟以新增 MIP 檔案 SDK 套件,但改為將 「Microsoft.Identity.Client」 新增至應用程式。
實作驗證代理
MIP SDK 會使用類別擴充性來實作驗證,以提供與用戶端應用程式共用驗證工作的機制。 客戶端必須取得適當的 OAuth2 存取令牌,並在運行時間提供給 MIP SDK。
現在,透過擴充 SDK 的 Microsoft.InformationProtection.IAuthDelegate 介面並覆寫/實作 IAuthDelegate.AcquireToken() 虛擬函式來建立驗證委派的實作。 驗證委派會被實例化,稍後由 FileProfile 和 FileEngine 物件使用。
以滑鼠右鍵按兩下 Visual Studio 中的項目名稱,選取 [ 新增 ] 和 [ 類別]。
在 [ 名稱 ] 字段中輸入 “AuthDelegateImplementation”。 按下 新增。
新增 Microsoft 驗證程式庫 (MSAL) 和 MIP 程式庫的 using 指令:
using Microsoft.InformationProtection; using Microsoft.Identity.Client;將
AuthDelegateImplementation設為繼承Microsoft.InformationProtection.IAuthDelegate,並實作Microsoft.InformationProtection.ApplicationInfo類型的私有變數,以及接受相同類型的建構函式。public class AuthDelegateImplementation : IAuthDelegate { private ApplicationInfo _appInfo; // Microsoft Authentication Library IPublicClientApplication private IPublicClientApplication _app; public AuthDelegateImplementation(ApplicationInfo appInfo) { _appInfo = appInfo; } }物件
ApplicationInfo包含三個屬性。_appInfo.ApplicationId將在AuthDelegateImplementation類別中使用,以將用戶端識別碼提供給身份驗證庫。ApplicationName並將ApplicationVersion出現在 Microsoft Purview 稽核報告中。新增
public string AcquireToken()方法。 如果必要,此方法應該接受Microsoft.InformationProtection.Identity和三個字串:授權單位 URL、資源 URI 和宣告。 這些字串變數會由 API 傳入驗證庫,不應加以修改。 請從 Azure 入口網站為您的租使用者輸入租使用者 GUID。 編輯租使用者 GUID 以外的字串可能會導致驗證失敗。public string AcquireToken(Identity identity, string authority, string resource, string claims) { var authorityUri = new Uri(authority); authority = String.Format("https://{0}/{1}", authorityUri.Host, "<Tenant-GUID>"); _app = PublicClientApplicationBuilder.Create(_appInfo.ApplicationId).WithAuthority(authority).WithDefaultRedirectUri().Build(); var accounts = (_app.GetAccountsAsync()).GetAwaiter().GetResult(); // Append .default to the resource passed in to AcquireToken(). string[] scopes = new string[] { resource[resource.Length - 1].Equals('/') ? $"{resource}.default" : $"{resource}/.default" }; var result = _app.AcquireTokenInteractive(scopes).WithAccount(accounts.FirstOrDefault()).WithPrompt(Prompt.SelectAccount) .ExecuteAsync().ConfigureAwait(false).GetAwaiter().GetResult(); return result.AccessToken; }
實作同意委派
現在,藉由擴充 SDK 的Microsoft.InformationProtection.IConsentDelegate介面並覆寫/實作GetUserConsent(),建立同意委派物件的實作。 檔案配置檔和檔案引擎物件稍後會具現化並使用同意委派。 同意委派者會獲得包含在 url 參數中的服務位址,此服務是用戶必須同意使用的。 委派通常應該提供一些流程,讓使用者接受或拒絕同意存取服務。 在本快速入門中,硬式程式碼 Consent.Accept。
使用我們先前使用的相同 Visual Studio「新增類別」功能,將另一個類別新增至您的專案。 這次,在 [類別名稱 ] 字段中輸入 「ConsentDelegateImplementation」。。
現在更新 ConsentDelegateImpl.cs 以實作新的同意委派類別。 新增 using 語句
Microsoft.InformationProtection,並將 類別設定為繼承IConsentDelegate。class ConsentDelegateImplementation : IConsentDelegate { public Consent GetUserConsent(string url) { return Consent.Accept; } }選擇性地嘗試建置解決方案,以確保其編譯時不會發生任何錯誤。
初始化 MIP SDK 受管理的包裝器
從 [方案總管] 中,開啟專案中包含 方法實作
Main()的 .cs 檔案。 它預設為與包含它的專案相同的名稱,此專案名稱是在您建立專案時指定的。移除生成的實作
main()。Managed 包裝函式包含靜態類別,
Microsoft.InformationProtection.MIP用於初始化、建立MipContext、載入配置檔,以及釋放資源。 若要初始化檔案 SDK 作業的包裝函式,請呼叫MIP.Initialize(),傳入MipComponent.File以載入檔案作業所需的連結庫。在
Main()Program.cs 中新增下列內容,將 <application-id> 替換為先前建立之 Microsoft Entra Application Registration 的 ID。
using System;
using System.Threading.Tasks;
using Microsoft.InformationProtection;
using Microsoft.InformationProtection.Exceptions;
using Microsoft.InformationProtection.File;
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 File SDK operations
MIP.Initialize(MipComponent.File);
}
}
}
建構檔案設定檔和引擎
如前所述,使用 MIP API 的 SDK 用戶端需要設定檔和引擎物件。 藉由新增程式代碼來完成本快速入門的程式碼部分。首先載入原生 DLL,然後初始化設定檔和引擎物件。
using System;
using System.Threading.Tasks;
using Microsoft.InformationProtection;
using Microsoft.InformationProtection.File;
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 File SDK operations.
MIP.Initialize(MipComponent.File);
// Create ApplicationInfo, setting the clientID from Microsoft Entra 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 mipContext = MIP.CreateMipContext(mipConfiguration);
// Initialize and instantiate the File Profile.
// Create the FileProfileSettings object.
// Initialize file profile settings to create/use local state.
var profileSettings = new FileProfileSettings(mipContext,
CacheStorageType.OnDiskEncrypted,
new ConsentDelegateImplementation());
// Load the Profile async and wait for the result.
var fileProfile = Task.Run(async () => await MIP.LoadFileProfileAsync(profileSettings)).Result;
// Create a FileEngineSettings object, then use that to add an engine to the profile.
// This pattern sets the engine ID to user1@tenant.com, then sets the identity used to create the engine.
var engineSettings = new FileEngineSettings("user1@tenant.com", authDelegate, "", "en-US");
engineSettings.Identity = new Identity("user1@tenant.com");
var fileEngine = Task.Run(async () => await fileProfile.AddEngineAsync(engineSettings)).Result;
// Application Shutdown
// handler = null; // This will be used in later quick starts.
fileEngine = null;
fileProfile = null;
mipContext.ShutDown();
mipContext = null;
}
}
}
請使用下列值替換您在原始碼中貼上的佔位元值:
佔位符 價值 範例 <應用程式識別碼> 指派給「MIP SDK 設定和設定」中註冊之應用程式的Microsoft Entra 應用程式識別碼(2 個實例)。 0edbblll-8773-44de-b87c-b8c6276d41eb <友好的名稱> 應用程式的使用者自訂友善名稱。 應用程式初始化 <租戶-GUID> Microsoft Entra 租戶的 Tenant-ID TenantID 現在請執行應用程式的最終組建,並解決任何錯誤。 您的程式碼應該能建置成功。
後續步驟
現在您的初始化程式代碼已完成,您已準備好進行下一個快速入門,您將開始體驗 MIP 檔案 SDK。