練習 - 使用存取控制清單確保安全存取

已完成

在此練習中,您會更新負責匯入本機 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 頁面進行調整。

匯入可供選取使用者使用的內容

首先,將您要匯入的其中一個頁面設定為僅供特定使用者存取。

在網頁瀏覽器中:

  1. 流覽至 Azure 入口網站, https://portal.azure.com 並使用您的公司或學校帳戶登入。

  2. 從提要欄位中,選取 [Microsoft Entra ID]

  3. 從導覽中,選取 [ 使用者]

  4. 從使用者清單中,選取其中一個使用者的名稱來開啟其中一個使用者。

  5. 複製 [物件識別碼 ] 屬性的值。

    已開啟使用者配置檔的 Azure 入口網站螢幕快照。

使用此值來定義特定 Markdown 頁面的新 ACL。

在程式代碼編輯器中:

  1. 開啟 ContentService.cs 檔案,然後找出 Transform 方法。

  2. 在委 Select 派內,定義套用至所有匯入項目的預設 ACL:

    var acl = new Acl
    {
      Type = AclType.Everyone,
      Value = "everyone",
      AccessType = AccessType.Grant
    };
    
  3. 接下來,覆寫名稱結尾 use-the-api.md為 之 Markdown 檔案的預設 ACL:

    if (a.RelativePath!.EndsWith("use-the-api.md"))
    {
      acl = new()
      {
        Type = AclType.User,
        // AdeleV
        Value = "6de8ec04-6376-4939-ab47-83a2c85ab5f5",
        AccessType = AccessType.Grant
      };
    }
    
  4. 最後,更新傳回外部項目的程式代碼,以使用定義的 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
      }
    };
    
  5. 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
          }
        };
      });
    }
    
  6. 儲存變更。

匯入可供選取群組使用的內容

現在,讓我們擴充程式代碼,讓另一個頁面只能由選取的使用者群組存取。

在網頁瀏覽器中:

  1. 流覽至 Azure 入口網站, https://portal.azure.com 並使用您的公司或學校帳戶登入。
  2. 從提要欄位中,選取 [Microsoft Entra ID]
  3. 從導覽中,選取 [群組]
  4. 從群組清單中,選取其中一個群組的名稱來開啟其中一個群組。
  5. 複製 [物件識別碼 ] 屬性的值。

Azure 入口網站的螢幕快照,其中已開啟群組頁面。

使用此值來定義特定 Markdown 頁面的新 ACL。

在程式代碼編輯器中:

  1. 開啟 ContentService.cs 檔案,然後找出 Transform 方法

  2. 擴充先前定義 if 的 子句,並使用額外的條件來定義名稱結尾為 之 markdown 檔案的 traverse-the-graph.mdACL:

    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
      };
    }
    
  3. 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
          }
        };
      });
    }
    
  4. 儲存變更。

套用新的 ACL

最後一個步驟是套用新設定的 ACL。

  1. 開啟終端機,並將工作目錄變更為您的專案。
  2. 執行 命令來建置 dotnet build 專案。
  3. 執行 命令開始 dotnet run -- load-content 載入內容。