Microsoft 信息保护 SDK - 文件 SDK 配置文件概念

配置文件是所有 MIP SDK 操作的根类。 在使用任何文件 SDK 功能之前,必须创建 FileProfile,并且所有进一步操作将由配置文件执行,或由添加到配置文件的其他对象执行。

在尝试实例化配置文件之前,需要满足一些代码先决条件:

  • MipContext 已创建并存储在可供 mip::FileProfile 对象访问的对象中。
  • ConsentDelegateImpl 可实现 mip::ConsentDelegate
  • 应用程序已经注册到 Microsoft Entra ID,并且客户端 ID 已硬编码到应用程序或配置文件中。
  • 已正确实现类继承 mip::FileProfile::Observer

加载配置文件

定义 ProfileObserverConsentDelegateImpl 后,现在可以实例化 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 实现的共享指针(在 PolicyProfileProtectionProfileFileProfile 中)。

以下示例演示了如何使用状态存储的本地存储以及仅在内存中创建 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

加载配置文件

使用上述任意一种方法详细信息,然后使用承诺/未来模式加载 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 的指针。 该函数只是将承诺的值设置为作为第一个参数传入的 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 的对象中。

后续步骤

现在已经添加好配置文件,下一步将需要向配置文件添加引擎。