Microsoft 信息保护 SDK - 文件 SDK 重新发布快速入门 (C#)

概述

有关此方案及其使用位置的概述,请参阅在 MIP SDK 中重新发布

先决条件

如果尚未完成,请确保先完成以下先决条件,然后再继续:

添加逻辑以编辑和重新发布受保护的文件

  1. 打开在前文“快速入门:设置/获取敏感度标签 (C#)”中创建的 Visual Studio 解决方案。

  2. 使用解决方案资源管理器,打开项目中包含 Main() 方法实现的 .cs 文件。 该文件默认与包含它的项目同名,该名称在项目创建期间指定。

  3. Main() 主体的末尾,在 Console.ReadKey() 下方和应用程序关闭块上方(你在上一个快速入门中离开的位置),插入以下代码。

string protectedFilePath = "<protected-file-path>" // Originally protected file's path from previous quickstart.

//Create a fileHandler for consumption for the Protected File.
var protectedFileHandler = Task.Run(async () => 
                            await fileEngine.CreateFileHandlerAsync(protectedFilePath,// inputFilePath
                                                                    protectedFilePath,// actualFilePath
                                                                    false, //isAuditDiscoveryEnabled
                                                                    null)).Result; // fileExecutionState

// Store protection handler from file
var protectionHandler = protectedFileHandler.Protection;

//Check if the user has the 'Edit' right to the file
if (protectionHandler.AccessCheck("Edit"))
{
    // Decrypt file to temp path
    var tempPath = Task.Run(async () => await protectedFileHandler.GetDecryptedTemporaryFileAsync()).Result;

    /*
        Your own application code to edit the decrypted file belongs here. 
    */

    /// Follow steps below for re-protecting the edited file. ///
    // Create a new file handler using the temporary file path.
    var republishHandler = Task.Run(async () => await fileEngine.CreateFileHandlerAsync(tempPath, tempPath, false)).Result;

    // Set protection using the ProtectionHandler from the original consumption operation.
    republishHandler.SetProtection(protectionHandler);

    // New file path to save the edited file
    string reprotectedFilePath = "<reprotected-file-path>" // New file path for saving reprotected file.

    // Write changes
    var reprotectedResult = Task.Run(async () => await republishHandler.CommitAsync(reprotectedFilePath)).Result;

    var protectedLabel = protectedFileHandler.Label;
    Console.WriteLine(string.Format("Originally protected file: {0}", protectedFilePath));
    Console.WriteLine(string.Format("File LabelID: {0} \r\nProtectionOwner: {1} \r\nIsProtected: {2}", 
                        protectedLabel.Label.Id, 
                        protectedFileHandler.Protection.Owner, 
                        protectedLabel.IsProtectionAppliedFromLabel.ToString()));
    var reprotectedLabel = republishHandler.Label;
    Console.WriteLine(string.Format("Reprotected file: {0}", reprotectedFilePath));
    Console.WriteLine(string.Format("File LabelID: {0} \r\nProtectionOwner: {1} \r\nIsProtected: {2}", 
                        reprotectedLabel.Label.Id, 
                        republishHandler.Protection.Owner, 
                        reprotectedLabel.IsProtectionAppliedFromLabel.ToString()));
    Console.WriteLine("Press a key to continue.");
    Console.ReadKey();
}
  1. 在 Main() 的末尾,找到在上一个快速入门中创建的应用程序关闭块,并添加下面的处理程序行以释放资源。

        protectedFileHandler = null;
        protectionHandler = null;
    
  2. 将源代码中的占位符值替换为以下值:

    占位符
    <protected-file-path> 上一快速入门中的受保护文件。
    <reprotected-file-path> 要重新发布的已修改文件的输出文件路径。

生成并测试应用

生成和测试客户端应用程序。

  1. 使用 CTRL-SHIFT-B(生成解决方案)生成客户端应用程序。 如果没有生成错误,请使用 F5(启动调试)运行应用程序。

  2. 如果项目成功生成并运行,则应用程序可能在 SDK 每次调用 AcquireToken() 方法时都会提示使用 Microsoft 身份验证库 (MSAL) 进行身份验证。 如果缓存的凭据已存在,系统不会提示你登录并查看标签列表,以及应用的标签和已修改文件的信息。

  Personal : 73c47c6a-eb00-4a6a-8e19-efaada66dee6
  Public : 73254501-3d5b-4426-979a-657881dfcb1e
  General : da480625-e536-430a-9a9e-028d16a29c59
  Confidential : 569af77e-61ea-4deb-b7e6-79dc73653959
  Highly Confidential : 905845d6-b548-439c-9ce5-73b2e06be157
  Press a key to continue.

  Getting the label committed to file: C:\Test\Test_protected.docx
  File Label: Confidential
  IsProtected: True
  Press a key to continue.
  Originally protected file: C:\Test\Test_protected.docx
  File LabelID: 569af77e-61ea-4deb-b7e6-79dc73653959
  ProtectionOwner: User1@Contoso.OnMicrosoft.com
  IsProtected: True
  Reprotected file: C:\Test\Test_reprotected.docx
  File LabelID: 569af77e-61ea-4deb-b7e6-79dc73653959
  ProtectionOwner: User1@Contoso.OnMicrosoft.com
  IsProtected: True
  Press a key to continue.