練習 - 使用預存存取原則來委派 Azure 儲存體的存取權
不是個別建立具有各自存取權限和到期日的 SAS,而是可以建立其與預存存取原則的關聯。 變更原則會影響與其關聯的所有 SAS。
現在您知道有一種更好的方法可以建立及管理貴公司的 SAS。 您可以更新新的測試頁面,以使用預存存取原則。
在此練習中,您會更新 Web 應用程式以建立具有預存存取原則的 SAS。 接著,使用 Azure CLI 命令變更原則並測試存取權是否已撤銷。
新增方法以建立預存存取原則
在 Azure Cloud Shell 中,編輯 PatientRecordController.cs 檔案。
code ~/sas/Controllers/PatientRecordController.cs
在課程底部的
GetBlobSas
方法下,撰寫建立預存存取原則的方法。// Use a stored access policy for the SAS private void CreateStoredAccessPolicy() { // Create a stored access policy for our blobs BlobSignedIdentifier identifier = new BlobSignedIdentifier { Id = _storedPolicyID, AccessPolicy = new BlobAccessPolicy { ExpiresOn = DateTimeOffset.UtcNow.AddHours(1), Permissions = "r" } }; _container.SetAccessPolicy(permissions: new BlobSignedIdentifier[] { identifier }); }
這個方法會使用全域變數作為存取原則識別碼。 將此變數新增至位在名為
_container
之BlobContainerClient
變數宣告底下的類別上方。private String _storedPolicyID = "patient-images-policy";
預存存取原則會用於所產生的每個 SAS 權杖,因此,請在類別具現化上呼叫新的方法。 在方法的底部新增呼叫。
public PatientRecordsController(ILogger<PatientRecordsController> logger, IConfiguration iconfiguration) { _logger = logger; _iconfiguration = iconfiguration; _container = new BlobContainerClient( _iconfiguration.GetValue<string>("StorageAccount:ConnectionString"), _iconfiguration.GetValue<string>("StorageAccount:Container") ); CreateStoredAccessPolicy(); }
現在
GetBlobSas
可以簡化成使用存取原則。 變更方法來加以使用。// Build a SAS token for the given blob private string GetBlobSas() { // Create a user SAS that only allows reading for a minute BlobSasBuilder sas = new BlobSasBuilder { Identifier = _storedPolicyID, BlobContainerName = _iconfiguration.GetValue<string>("StorageAccount:Container") }; // Use the shared key to access the blob var storageSharedKeyCredential = new StorageSharedKeyCredential( _iconfiguration.GetValue<string>("StorageAccount:AccountName"), _iconfiguration.GetValue<string>("StorageAccount:AccountKey") ); return '?' + sas.ToSasQueryParameters(storageSharedKeyCredential).ToString(); }
處理 SAS 權杖要求的程式碼需要略加修正,才能呼叫已更新的方法。
// GET PatientRecord/patient-nnnnnn/secure [HttpGet("{Name}/{secure}")] public PatientRecord Get(string name, string flag) { BlobClient blob = _container.GetBlobClient(name); return new PatientRecord { name=blob.Name, imageURI=blob.Uri.AbsoluteUri, sasToken=GetBlobSas() }; }
選取 Ctrl+S,然後選取 Ctrl+Q,以儲存您的程式碼變更。
測試新的程式碼
在 Cloud Shell 中,建置應用程式。
cd ~/sas/ dotnet build
如果在完成前一個練習後,連接埠已關閉,請執行
curl
命令以重新開啟。curl -X POST http://localhost:8888/openPort/8000;
執行更新的 Web 應用程式。
dotnet run
移至 Web 應用程式的 URL,並確定其結尾是正斜線 (/)。
在首頁中選取 [Get all patients] (取得所有病人資料)。
複製影像檔案名稱。 例如 patient-32589.jpg。
選取頁面頂端的 [外部公司] 功能表連結。
將影像檔案名稱貼入 [Patient image filename] (患者影像檔案名稱) 欄位。
選取 [Get Key] (取得金鑰) 填入 SAS 權杖。
選取 [View scan] (檢視掃描) 檢視影像。
編輯預存存取原則
以您啟用沙箱時所使用的相同認證登入 Azure 入口網站。
在 Azure 入口網站資源功能表中,選取 [所有資源]。
在資源清單中,選取 medicalrecords 儲存體帳戶。
在 [概觀] 窗格上,選取 [容器],然後選取 [patient-images (病患影像)]。
在病患影像功能表上的 [設定] 底下,選取[存取原則]。
請注意,Web 應用程式已建立 patient-images-policy 預存存取原則。
在右側,選取 [...] 功能表,然後從快顯功能表中選取 [編輯]。
在 [編輯原則] 中,將 [權限] 從 [讀取] 變更為 [列出],並選取 [確定] 以確認。
在病患影像上選取 [儲存] | 存取原則窗格。
測試新的 SAS
返回您的 Web 應用程式。 在 [外部公司] 頁面上,透過選取 [取得金鑰] 以建立新的 SAS 權杖。
選取 [檢視掃描]。
系統不會從 Azure 儲存體返回影像,且您會收到 403 驗證錯誤。