代码之家  ›  专栏  ›  技术社区  ›  AlexB

ASP。NET Core 2.0-如何使用auth从Azure存储转发文件

  •  0
  • AlexB  · 技术社区  · 8 年前

    我的应用程序生成PDF并将其存储在Azure存储中的一个私人容器中。但是,当用户进行身份验证(我使用Azure AD B2C)并转到他的个人页面时,我需要显示这些PDF的链接。现在,这些链接不能公开,所以我认为我需要:

    1) 当用户访问这些链接时,使用某种中间件对其进行身份验证

    1 回复  |  直到 8 年前
        1
  •  1
  •   Amor    8 年前

    我同意“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);
    }