練習 - 將雲端原生應用程式中的敏感資料分類
在此練習中,您會將範例 eShopLite 應用程式中的敏感數據類型分類。
應用程式處於使用中開發狀態,目前有兩個數據類別, Product 以及 Order。 他們正在建置排序程式,並想要新增程式代碼來分類數據類型。
在此練習中,請參閱如何:
- 探索目前的應用程式及其數據類型。
- 新增程式代碼以分類數據類型。
- 執行及測試應用程式。
開啟開發環境
您可以選擇使用裝載本練習的 GitHub Codespace,或在 Visual Studio Code 本機上完成該練習。
若要使用 Codespace,請使用 此 Codespace 建立連結 來建立預先設定的 GitHub Codespace。
GitHub 需要幾分鐘的時間才能建立及設定 Codespace。 程式完成時,您會看到練習的程式代碼檔案。 此課程模組其餘部分所使用的程式代碼位於 /dotnet-compliance 目錄中。
若要使用 Visual Studio Code,請將存放 https://github.com/MicrosoftDocs/mslearn-dotnet-cloudnative 庫派生到您自己的 GitHub 帳戶。 然後:
- 請確保 Docker 正在運行。 在新的 Visual Studio Code 視窗中,按 Ctrl+Shift+P 以開啟命令選擇區。
- 搜尋並選取 [開發容器:在容器磁碟區中複製存放庫]。
- 選取分支存放庫。 Visual Studio Code 會在本機上建立您的開發容器。
eShopLite測試應用程式
在 Visual Studio Code 視窗底部,選取 [終端機 ] 索引標籤。
移至練習資料夾:
cd dotnet-compliance/eShopLite建置應用程式容器。
dotnet publish /p:PublishProfile=DefaultContainer使用 docker 執行應用程式:
cd .. docker compose up選取 [埠] 索引標籤,然後選取 [在瀏覽器中開啟] 地球圖示以開啟 前端 (32000) 埠。
選取 [產品] 連結。 應用程式會顯示產品清單。
選取 [終端機] 索引標籤,然後按 Ctrl+C 以停止應用程式。
建立分類法和屬性
在這項工作中,新增程式代碼以建立兩個新的分類法。 Product然後使用適當的屬性標註和 Order 數據類型。
在 [終端機] 索引標籤中 eShopLite/DataEntities ,移至資料夾:
cd eShopLite/DataEntities/新增合規性套件:
dotnet add package Microsoft.Extensions.Compliance.Redaction在 [總管] 窗格中,以滑鼠右鍵按兩下 DataEntities 資料夾,然後選取 [新增檔案]。
在檔名中,輸入 Compliance.cs。
在編輯器中,輸入下列程式代碼:
using Microsoft.Extensions.Compliance.Classification; using Microsoft.Extensions.Compliance.Redaction; public static class DataClassifications { // End User Identifiable Information public static DataClassification EUIIDataClassification {get;} = new DataClassification("EUIIDataTaxonomy", "EUIIData"); // End User Pseudonymous Information public static DataClassification EUPDataClassification {get;} = new DataClassification("EUPDataTaxonomy", "EUPData"); } public class EUIIDataAttribute : DataClassificationAttribute { public EUIIDataAttribute() : base(DataClassifications.EUIIDataClassification) { } } public class EUPDataAttribute : DataClassificationAttribute { public EUPDataAttribute() : base(DataClassifications.EUPDataClassification) { } }此程式代碼會建立兩個分類法和 EUIIEUPI。 它也會建立兩個屬性和 EUIIDataAttributeEUPDataAttribute。 這些屬性是用來標註數據類型。
分類數據類型
使用這些分類法和屬性來分類應用程式中的 eShopLite 數據類型。
在 [ 總管] 窗格中,展開 [DataEntities ] 資料夾,然後選取 Product.cs 檔案。
此類別中沒有特定的客戶敏感數據,但如果產品標識符屬性已連線到記錄中的客戶,應用程式可能會洩漏假名數據。
將 EUPData 屬性新增至 ProductId 屬性:
[EUPData] [Key] [JsonPropertyName("id")] public int Id { get; set; }上述程式代碼會告訴修訂引擎 Id 屬性是假名數據。
在 [ 總管] 窗格中,展開 [DataEntities ] 資料夾,然後選取 Order.cs 檔案。
類別 Order 包含敏感數據。 CustomerName和 CustomerAddress 屬性是用戶可識別的資訊。 屬性 Id 是用戶假名資訊。
將 EUIIData 屬性新增至 CustomerName 和 CustomerAddress 屬性:
[EUIIData] [JsonPropertyName("customerName")] public string? CustomerName { get; set; } [EUIIData] [JsonPropertyName("customerAddress")] public string? CustomerAddress { get; set; }上述程式代碼會告訴修訂引擎, CustomerName 和 CustomerAddress 屬性是用戶可識別的資訊。
將 EUPData 屬性新增至 Id 屬性:
[Key] [EUPData] [JsonPropertyName("id")] public int Id { get; set; }上述程式代碼會告訴修訂引擎 Id ,屬性是使用者假名資訊。
測試您對應用程式的變更eShopLite
在底部的 [終端機 ] 窗格中,移至 dotnet-compliance/eShopLite 資料夾。
cd ..更新應用程式容器。
dotnet publish /p:PublishProfile=DefaultContainer移至 dotnet-compliance 資料夾,並使用 Docker 啟動應用程式:
cd .. docker compose up如果您在瀏覽器中關閉索引標籤,請選取 [埠] 索引標籤,然後選取前端 (32000) 埠的 [在瀏覽器中開啟] 圖示。
請注意, eShopLite 應用程式不會變更。
請嘗試將某些產品新增至購物籃,然後選取 [購買購物籃]。
在 [終端機 ] 視窗中,按 Ctrl+C 以停止應用程式。
您將在下一個練習中新增修訂的記錄。