练习 - 确保使用访问控制列表进行安全访问
在本练习中,你将更新负责导入本地 markdowns 文件的代码,并在所选项上配置 ACL。
准备工作
在执行本练习之前,请务必完成本模块中的上一个练习。
导入可供组织中的每个人使用的内容
在上一练习中实现用于导入外部内容的代码时,已将其配置为可供组织中的每个人使用。 下面是你使用的代码:
static IEnumerable<ExternalItem> Transform(IEnumerable<DocsArticle> content)
{
var baseUrl = new Uri("https://learn.microsoft.com/graph/");
return content.Select(a =>
{
var docId = GetDocId(a.RelativePath ?? "");
return new ExternalItem
{
Id = docId,
Properties = new()
{
AdditionalData = new Dictionary<string, object> {
{ "title", a.Title ?? "" },
{ "description", a.Description ?? "" },
{ "url", new Uri(baseUrl, a.RelativePath!.Replace(".md", "")).ToString() }
}
},
Content = new()
{
Value = a.Content ?? "",
Type = ExternalItemContentType.Html
},
Acl = new()
{
new()
{
Type = AclType.Everyone,
Value = "everyone",
AccessType = AccessType.Grant
}
}
};
});
}
你配置了 ACL 以向所有人授予访问权限。 让我们针对要导入的所选 Markdown 页面调整它。
导入可供选定用户使用的内容
首先,将要导入的页面之一配置为仅供特定用户访问。
在 Web 浏览器中:
导航到 的 Azure 门户 https://portal.azure.com ,并使用工作或学校帐户登录。
从边栏中,选择“ Microsoft Entra ID”。
在导航中,选择“ 用户”。
从用户列表中,通过选择其中一个用户的名称来打开其中一个用户。
复制 Object ID 属性的值。
使用此值为特定 markdown 页定义新的 ACL。
在代码编辑器中:
打开 ContentService.cs 文件,找到
Transform
方法。在委托中
Select
,定义适用于所有导入项的默认 ACL:var acl = new Acl { Type = AclType.Everyone, Value = "everyone", AccessType = AccessType.Grant };
接下来,重写 markdown 文件的默认 ACL,其名称以 结尾
use-the-api.md
:if (a.RelativePath!.EndsWith("use-the-api.md")) { acl = new() { Type = AclType.User, // AdeleV Value = "6de8ec04-6376-4939-ab47-83a2c85ab5f5", AccessType = AccessType.Grant }; }
最后,更新返回外部项的代码以使用定义的 ACL:
return new ExternalItem { Id = docId, Properties = new() { AdditionalData = new Dictionary<string, object> { { "title", a.Title ?? "" }, { "description", a.Description ?? "" }, { "url", new Uri(baseUrl, a.RelativePath!.Replace(".md", "")). ToString() } } }, Content = new() { Value = a.Content ?? "", Type = ExternalItemContentType.Html }, Acl = new() { acl } };
更新
Transform
的方法如下所示:static IEnumerable<ExternalItem> Transform(IEnumerable<DocsArticle> content) { var baseUrl = new Uri("https://learn.microsoft.com/graph/"); return content.Select(a => { var acl = new Acl { Type = AclType.Everyone, Value = "everyone", AccessType = AccessType.Grant }; if (a.RelativePath!.EndsWith("use-the-api.md")) { acl = new() { Type = AclType.User, // AdeleV Value = "6de8ec04-6376-4939-ab47-83a2c85ab5f5", AccessType = AccessType.Grant }; } var docId = GetDocId(a.RelativePath ?? ""); return new ExternalItem { Id = docId, Properties = new() { AdditionalData = new Dictionary<string, object> { { "title", a.Title ?? "" }, { "description", a.Description ?? "" }, { "url", new Uri(baseUrl, a.RelativePath!.Replace(".md", "")). ToString() } } }, Content = new() { Value = a.Content ?? "", Type = ExternalItemContentType.Html }, Acl = new() { acl } }; }); }
保存所做的更改。
导入可供选择组使用的内容
现在,让我们扩展代码,以便只有一组选定用户才能访问另一个页面。
在 Web 浏览器中:
- 导航到 的 Azure 门户 https://portal.azure.com ,并使用工作或学校帐户登录。
- 从边栏中,选择“ Microsoft Entra ID”。
- 在导航中,选择“ 组”。
- 从组列表中,通过选择组名称打开其中一个组。
- 复制 “对象 ID” 属性的值。
使用此值为特定 markdown 页定义新的 ACL。
在代码编辑器中:
打开 ContentService.cs 文件,并找到
Transform
方法扩展以前定义的
if
子句,使用额外的条件来定义 markdown 文件的 ACL,其名称以 结尾traverse-the-graph.md
:if (a.RelativePath!.EndsWith("use-the-api.md")) { acl = new() { Type = AclType.User, // AdeleV Value = "6de8ec04-6376-4939-ab47-83a2c85ab5f5", AccessType = AccessType.Grant }; } else if (a.RelativePath.EndsWith("traverse-the-graph.md")) { acl = new() { Type = AclType.Group, // Sales and marketing Value = "a9fd282f-4634-4cba-9dd4-631a2ee83cd3", AccessType = AccessType.Grant }; }
更新
Transform
的方法如下所示:static IEnumerable<ExternalItem> Transform(IEnumerable<DocsArticle> content) { var baseUrl = new Uri("https://learn.microsoft.com/graph/"); return content.Select(a => { var acl = new Acl { Type = AclType.Everyone, Value = "everyone", AccessType = AccessType.Grant }; if (a.RelativePath!.EndsWith("use-the-api.md")) { acl = new() { Type = AclType.User, // AdeleV Value = "6de8ec04-6376-4939-ab47-83a2c85ab5f5", AccessType = AccessType.Grant }; } else if (a.RelativePath.EndsWith("traverse-the-graph.md")) { acl = new() { Type = AclType.Group, // Sales and marketing Value = "a9fd282f-4634-4cba-9dd4-631a2ee83cd3", AccessType = AccessType.Grant }; } var docId = GetDocId(a.RelativePath ?? ""); return new ExternalItem { Id = docId, Properties = new() { AdditionalData = new Dictionary<string, object> { { "title", a.Title ?? "" }, { "description", a.Description ?? "" }, { "url", new Uri(baseUrl, a.RelativePath!.Replace(".md", "")).ToString() } } }, Content = new() { Value = a.Content ?? "", Type = ExternalItemContentType.Html }, Acl = new() { acl } }; }); }
保存所做的更改。
应用新的 ACL
最后一步是应用新配置的 ACL。
- 打开终端并将工作目录更改为项目。
- 通过运行
dotnet build
命令生成项目。 - 通过运行
dotnet run -- load-content
命令开始加载内容。