Microsoft 資訊保護 SDK - 檔案 SDK 設定檔概念
設定檔是 MIP SDK 中所有作業的根類別。 在使用任何檔案 SDK 功能之前, FileProfile
必須先建立 ,而且所有未來的作業都會由設定檔或新增 至設定檔的其他物件 執行。
在嘗試具現化設定檔之前,應該符合幾個程式碼必要條件:
MipContext
已建立並儲存在物件可存取的物件中mip::FileProfile
。ConsentDelegateImpl
會實作mip::ConsentDelegate
。- 應用程式已在 Microsoft Entra ID 中註冊,且用戶端識別碼會硬式編碼至應用程式或組態檔。
- 已適當實作繼承
mip::FileProfile::Observer
類別。
載入設定檔
ProfileObserver
使用 、 和 ConsentDelegateImpl
定義後, mip::FileProfile
現在可以具現化。 mip::FileProfile
建立物件需要 [ mip::MipContext
] 具有 和 mip::FileProfile::Settings
來儲存 有關 FileProfile
的所有設定資訊。
FileProfile::設定 參數
建 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
作。 產生的物件或例外狀況指標以及內容會以參數的形式傳入函式。 內容是我們建立來處理非同步作業的指標 std::promise
。 函式只會將 promise 的值設定為針對第一個參數傳入的 FileProfile 物件。 當 main 函式使用 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
的 物件中。
後續步驟
現在已新增設定檔,下一個步驟是將引擎新增至設定檔。
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應