配置檔是 MIP SDK 中所有作業的根類別。 在使用任何檔案 SDK 功能之前,先建立一個 FileProfile. 設定檔或其他新增至該設定檔中的物件,會執行後續所有作業。
在建立個人檔案前,請先符合以下程式碼的先決條件:
-
MipContext:建立
MipContext並儲存在物件mip::FileProfile可存取的物件中。 -
同意委派:
ConsentDelegateImpl實作mip::ConsentDelegate。 - 應用程式註冊:將應用程式註冊為 Microsoft Entra ID,並將用戶端 ID 硬編碼到應用程式或設定檔中。
-
檔案設定檔觀察器:實作一個繼承
mip::FileProfile::Observer的類別。
載入設定檔
在定義 ProfileObserver 和 ConsentDelegateImpl後,你可以實例化 mip::FileProfile。 建立物件 mip::FileProfile 需要 mip::MipContext 和 mip::FileProfile::Settings,這會儲存所有關於 的 FileProfile設定資訊。
FileProfile::Settings 參數
FileProfile::Settings建構子接受以下五個參數:
-
std::shared_ptr<MipContext>mip::MipContext:初始化以儲存應用程式資訊、狀態路徑等的物件。 -
mip::CacheStorageType:定義如何儲存狀態:在記憶體、磁碟或磁碟上,以及加密。 -
std::shared_ptr<mip::ConsentDelegate>:類別mip::ConsentDelegate的共享指標。 -
std::shared_ptr<mip::FileProfile::Observer> observer:配置文件Observer實作的共享指標(在PolicyProfile、ProtectionProfile和FileProfile中)。
以下範例說明如何利用本地儲存作為狀態儲存或僅在記憶體中儲存來建立物件 profileSettings 。
僅將狀態儲存在記憶體中
mip::ApplicationInfo appInfo {clientId, "APP NAME", "1.2.3" };
std::shared_ptr<mip::MipConfiguration> mipConfiguration = std::make_shared<mip::MipConfiguration>(mAppInfo,
"mip_data",
mip::LogLevel::Trace,
false);
std::shared_ptr<mip::MipContext> mMipContext = mip::MipContext::Create(mipConfiguration);
FileProfile::Settings profileSettings(
mMipContext, // mipContext object
mip::CacheStorageType::InMemory, // use in memory storage
std::make_shared<ConsentDelegateImpl>(), // new consent delegate
std::make_shared<FileProfileObserverImpl>()); // new protection profile observer
從磁碟上的記憶體路徑讀取/寫入設定檔設定
以下程式碼片段指示 將 FileProfile 所有應用程式狀態資料儲存在 ./mip_app_data。
mip::ApplicationInfo appInfo {clientId, "APP NAME", "1.2.3" };
std::shared_ptr<mip::MipConfiguration> mipConfiguration = std::make_shared<mip::MipConfiguration>(mAppInfo,
"mip_data",
mip::LogLevel::Trace,
false);
std::shared_ptr<mip::MipContext> mMipContext = mip::MipContext::Create(mipConfiguration);
FileProfile::Settings profileSettings(
mMipContext, // mipContext object
mip::CacheStorageType::OnDisk, // use on disk storage
std::make_shared<ConsentDelegateImpl>(), // new consent delegate
std::make_shared<FileProfileObserverImpl>()); // new protection profile observer
載入設定檔
在使用任一前一種方法後,使用 promise/future 模式來載入 FileProfile。
auto profilePromise = std::make_shared<std::promise<std::shared_ptr<FileProfile>>>();
auto profileFuture = profilePromise->get_future();
FileProfile::LoadAsync(profileSettings, profilePromise);
若應用程式成功載入設定檔,則會呼叫 ProfileObserver::OnLoadSuccess、mip::FileProfile::Observer::OnLoadSuccess 的實作。 SDK 會將產生的物件或例外指標,以及上下文作為參數傳遞給函式。 上下文是指向為處理非同步作業而建立的 std::promise 的指標。 函式會將承諾的值設定給第一個參數所傳遞的 FileProfile 物件。 當主函式使用 Future.get()時,可以將結果儲存在新的物件中。
//get the future value and store in profile.
auto profile = profileFuture.get();
整合
在實作觀察者和認證代理後,你可以完整載入設定檔。 以下程式碼片段假設所有必要的標頭都已包含。
int main()
{
const string userName = "MyTestUser@contoso.com";
const string password = "P@ssw0rd!";
const string clientId = "MyClientId";
mip::ApplicationInfo appInfo {clientId, "APP NAME", "1.2.3" };
std::shared_ptr<mip::MipConfiguration> mipConfiguration = std::make_shared<mip::MipConfiguration>(mAppInfo,
"mip_data",
mip::LogLevel::Trace,
false);
std::shared_ptr<mip::MipContext> mMipContext = mip::MipContext::Create(mipConfiguration);
FileProfile::Settings profileSettings(
mMipContext, // MipContext object
mip::CacheStorageType::OnDisk, // use on disk storage
std::make_shared<ConsentDelegateImpl>(), // new consent delegate
std::make_shared<FileProfileObserverImpl>()); // new file profile observer
auto profilePromise = std::make_shared<promise<shared_ptr<FileProfile>>>();
auto profileFuture = profilePromise->get_future();
FileProfile::LoadAsync(profileSettings, profilePromise);
auto profile = profileFuture.get();
}
程式碼載入設定檔並將其儲存在名為 profile的物件中。
下一步
現在已新增設定檔,下一步是將引擎新增至設定檔。