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

Profile 是所有 MIP SDK 操作的根类。 在使用任何文件 SDK 功能之前,请创建一个 FileProfile。 该配置文件或添加到该配置文件的其他对象将执行后续所有操作。

在实例化配置文件之前,请先满足以下代码前提条件:

  • MipContext:创建 MipContext 对象并将其存储在对象 mip::FileProfile 可访问的对象中。
  • 同意代理:ConsentDelegateImpl实现mip::ConsentDelegate。
  • 应用程序注册:在Microsoft Entra ID中注册应用程序,并将客户端 ID 硬编码到应用程序或配置文件中。
  • 文件配置观察器:实现一个继承自 mip::FileProfile::Observer 的类。

加载配置文件

定义ProfileObserverConsentDelegateImpl后,可以实例化mip::FileProfile。 创建 mip::FileProfile 对象需要 mip::MipContext 和 mip::FileProfile::Settings,其中 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 。 该函数将 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 的对象中。

后续步骤

现在已添加配置文件,下一步是为该配置文件添加引擎。