0

首先,我需要一個建議,然後是技術細節。ASP.NET Core 2.0 - 如何使用身份驗證從Azure存儲轉發文件

我的應用程序生成PDF並將它們存儲在私有容器中的Azure存儲中。但是,當用戶認證(我使用Azure AD B2C)並轉到他的個人頁面時,我需要顯示這些PDF的鏈接。現在,這些鏈接必須不公開的,所以我想我需要:

1)某種中間件權威性的用戶,當他訪問這些類型的鏈接

2)要求從存儲和傳遞文件它對響應

這樣做的最好方法是什麼? (也考慮性能)

我的第一個想法是使用SAS令牌,只需要約5-10分鐘的時間。但是如果用戶打開頁面並讓他的瀏覽器打開一個小時,然後返回並點擊PDF鏈接?

+1

使用SAS令牌是出於性能原因的最佳選擇。不要在頁面加載時生成blob URL,只需創建指向API的鏈接,在操作內部生成令牌,然後將完整路徑(uri +令牌)的重定向響應返回到azure存儲。這樣用戶就不可能獲得到blob的陳舊鏈接。 –

+0

沒錯。我可能在這裏遇到的唯一問題是用戶獲取到PDF的實際鏈接並通過電子郵件發送。如果令牌過期,則鏈接中斷。如果沒有,它基本上是公開的...所以我想也許還有另一種方法。 – alvipeo

+0

目前尚不清楚您的預期行爲是什麼,無論您使用何種方法,您所介紹的內容都是問題。爲什麼用戶應該通過電子郵件發送一個**私人**文件的鏈接? –

回答

1

我同意@Federico Dipuma。我在我的一個項目中使用了這種方式。我在這裏分享我的代碼。

生成SAS URL的代碼。

public string GetBlobSasUri(string containerName, string blobName, string connectionstring) 
{ 
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionstring); 
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); 
    var container = blobClient.GetContainerReference(containerName); 
    CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName); 

    //Set the expiry time and permissions for the blob. 
    //In this case no start time is specified, so the shared access signature becomes valid immediately. 
    SharedAccessBlobPolicy sasConstraints = new SharedAccessBlobPolicy(); 
    sasConstraints.SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(10); 
    sasConstraints.Permissions = SharedAccessBlobPermissions.Read; 

    //Generate the shared access signature on the blob, setting the constraints directly on the signature. 
    string sasContainerToken = blockBlob.GetSharedAccessSignature(sasConstraints); 

    //Return the URI string for the blob, including the SAS token. 
    return blockBlob.Uri + sasContainerToken; 
} 

重定向到您的Web應用程序中的URL。

public ActionResult FileDownload() 
{ 
    string blobURL = GetBlobSasUri("blob name","container name", "connection string"); 
    return Redirect(blobURL); 
} 
相關問題