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

以下两个示例演示了如何使用状态存储的本地存储以及仅在内存中创建 profileSettings 对象。

加载配置文件

现在,已经定义了 ProtectionProfileObserverImpl,我们将使用它来实例化 mip::ProtectionProfile。 创建 mip::ProtectionProfile 对象需要 mip::ProtectionProfile::Settings

ProtectionProfile::Settings 参数

  • std::shared_ptr<MipContext>:已初始化为存储应用程序信息、状态路径等的 mip::MipContext 对象。
  • mip::CacheStorageType:定义如何存储状态:在内存中、在磁盘上或在磁盘上并加密。
  • std::shared_ptr<mip::ConsentDelegate>mip::ConsentDelegate 类的共享指针。
  • std::shared_ptr<mip::ProtectionProfile::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);

ProtectionProfile::Settings profileSettings(
    mMipContext,                                        // mipContext object
    mip::CacheStorageType::InMemory,                   // use in memory storage    
    std::make_shared<ConsentDelegateImpl>(),           // new consent delegate
    std::make_shared<ProtectionProfileObserverImpl>()); // new protection profile observer

从磁盘上的存储路径读/写配置文件设置

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);

ProtectionProfile::Settings profileSettings(
    mMipContext,                                         // mipContext object
    mip::CacheStorageType::OnDisk,                      // use on disk storage    
    std::make_shared<ConsentDelegateImpl>(),            // new consent delegate
    std::make_shared<ProtectionProfileObserverImpl>()); // new protection profile

然后使用承诺/未来模式加载 ProtectionProfile

auto profilePromise = std::make_shared<std::promise<std::shared_ptr<ProtectionProfile>>>();
auto profileFuture = profilePromise->get_future();
ProtectionProfile::LoadAsync(profileSettings, profilePromise);

如果已经加载了配置文件,并且该操作成功 (ProtectionProfileObserverImpl::OnLoadSuccess),则会调用我们的 mip::ProtectionProfile::Observer::OnLoadSuccess 实现。 生成的对象或异常指针以及上下文将作为参数传递到函数。 上下文是指向我们为了处理异步操作而创建的 std::promise 的指针。 该函数只是将承诺的值设置为该 ProtectionProfile 对象(上下文)。 当主函数使用 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);

    ProtectionProfile::Settings profileSettings(
        mMipContext,                                    // mipContext object
        mip::CacheStorageType::OnDisk,                 // use on disk storage        
        std::make_shared<ConsentDelegateImpl>(),       // new consent delegate
        std::make_shared<ProfileObserver>());          // new protection profile observer

    auto profilePromise = std::make_shared<promise<shared_ptr<ProtectionProfile>>>();
    auto profileFuture = profilePromise->get_future();
    ProtectionProfile::LoadAsync(profileSettings, profilePromise);
    auto profile = profileFuture.get();
}

最终结果是,我们已成功加载配置文件并将其存储在名为 profile 的对象中。

后续步骤

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

保护引擎概念