Microsoft 信息保护 SDK 支持两种主要类型的标签驱动权限:基于模板和用户定义的权限。
基于模板的权限:标签管理员在Microsoft Purview 门户中定义这些权限。 这些标签是集中管理的,配置更改会影响已有文件副本的用户。 例如,如果管理员从授权用户列表中删除用户,该用户下次尝试提取许可证时不再有权访问受保护数据。
用户定义的权限:最终用户或应用程序 在标记时定义这些权限。 以用户到角色或用户到权限的映射集合形式将权限传递给 MIP SDK。 MIP SDK 将这些权限写入受保护文档的发布许可证中。 与基于模板的权限不同,在共享后无法集中管理或修改这些权限,而无需直接访问和修改文档。
用户、权限和角色
由于用户在标记时定义权限,因此应用程序必须提供一个界面供用户或服务输入电子邮件地址和权限或角色。 若要配置访问权限,请传入一个集合 UserRoles 或 UserRights 对象,这些对象定义谁具有对文档的访问权限级别。
// Create a List<string> of the first set of permissions.
List<string> users = new List<string>()
{
"alice@contoso.com",
"bob@contoso.com"
};
// Create a List<string> of the Rights the above users should have.
List<string> rights = new List<string>()
{
Rights.View,
Rights.Edit
};
// Create a UserRights object containing the defined users and rights.
UserRights userRights = new UserRights(users, rights);
// Add them to a new List<UserRights>
List<UserRights> userRightsList = new List<UserRights>()
{
userRights
};
结果是一个 List<UserRights> 集合,该集合指定 Alice 和 Bob 对受保护的文件具有 VIEW 和 EDIT。 若要添加具有不同权限集的更多用户,请重复此过程以创建第二UserRights个对象,传入新用户和权限,然后通过调用userRightsList.Add(userRights2)将其添加到List<UserRights>集合中。
相同的模式适用于 UserRoles。 若要实现它,请将 权限 替换为 角色 并创建 List<UserRoles> 集合。
保护域名
如果要为域应用用户定义的权限,则需要一个已知的邮件前缀和目标域来作为邮件地址。 该地址如下所示 AllStaff-7184AB3F-CCD1-46F3-8233-3E09E9CF0E66@contoso.com。
在应用程序中,用户应能够指定域,例如 contoso.com 或 fabrikam.com。 当应用程序创建保护描述符时,它会将 AllStaff-7184AB3F-CCD1-46F3-8233-3E09E9CF0E66@ 追加到域后缀。
此已知组也是向 组织中所有经过身份验证的用户授予权限的方式。 该AllStaff-7184AB3F-CCD1-46F3-8233-3E09E9CF0E66@组包含指定Microsoft Entra租户中的每个用户,因此它与来自 Active Directory Rights Management Services (AD RMS) 的 ANYONE 组最接近。 范围始终为单个租户:不存在可在所有位置向任何经过身份验证的用户授予权限的跨租户标识,因此请为要包含的每个组织添加单独的 AllStaff-...@domain 条目。 有关详细信息,请参阅 为 Azure 信息保护配置使用权限。
在以下示例中,用户将 alice@contoso.com 和 Fabrikam.com 中的所有人指定为有效收件人。
// Create a List<string> of the first set of permissions.
List<string> users = new List<string>()
{
"alice@contoso.com",
"AllStaff-7184AB3F-CCD1-46F3-8233-3E09E9CF0E66@fabrikam.com"
};
// Create a List<string> of the Rights the above users should have.
List<string> rights = new List<string>()
{
Rights.View,
Rights.Edit
};
// Create a UserRights object containing the defined users and rights.
UserRights userRights = new UserRights(users, rights);
// Add them to a new List<UserRights>
List<UserRights> userRightsList = new List<UserRights>()
{
userRights
};
启用保护
若要设置保护,请从 FileHandler.SetProtection() 或 List<UserRoles> 对象创建 ProtectionDescriptor,然后将该描述符传递给 List<UserRights>。 最后,提交对文件所做的更改以写入新文件。
何时对文件应用保护
使用 FileHandler.SetLabel() 设置标签后,MIP SDK 就具备了采取操作并应用相应保护所需的一切条件。 当标签使用用户定义的权限(UDP)时,应用程序无法提前知道标签是 UDP 标签。 MIP SDK 通过引发类型的 Microsoft.InformationProtection.Exceptions.AdhocProtectionRequiredException异常来显示此信息。 代码 FileHandler 应捕获此异常,然后触发用户或服务界面来定义自定义权限。 完成此过程后,可以设置保护。 下面的示例演示端到端模式,但假设你已经实现了一个函数来生成 List<UserRights> 对象。
try
{
// Attempt to set the label. If it's a UDP label, this will throw.
handler.SetLabel(engine.GetLabelById(options.LabelId), labelingOptions, new ProtectionSettings());
}
catch (Microsoft.InformationProtection.Exceptions.AdhocProtectionRequiredException)
{
// Assumes you've create a function that returns the List<UserRights> as previously detailed.
List<UserRights> userRightsList = GetUserRights();
// Create a ProtectionDescriptor using the set of UserRights.
ProtectionDescriptor protectionDescriptor = new ProtectionDescriptor(userRightsList);
// Apply protection to the file using the new ProtectionDescriptor.
handler.SetProtection(protectionDescriptor, new ProtectionSettings());
// Set the label. This will now succeed as protection has been defined.
handler.SetLabel(engine.GetLabelById(options.LabelId), labelingOptions, new ProtectionSettings());
// Commit the change.
var result = Task.Run(async () => await handler.CommitAsync("myFileOutput.xlsx")).Result;
}
自定义保护
还可以使用此过程通过设置保护并跳过步骤 SetLabel() 来仅设置保护。 如果应用程序不需要应用标签,则不需要异常处理程序。 若要设置保护,请遵循 ProtectionDescriptor ->SetProtection() 模式>CommitAsync() 。