Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ASP.NET Core 2.0-如何使用auth从Azure存储转发文件_Azure_Asp.net Core Mvc_Azure Storage Blobs_Asp.net Core 2.0 - Fatal编程技术网

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

ASP.NET Core 2.0-如何使用auth从Azure存储转发文件,azure,asp.net-core-mvc,azure-storage-blobs,asp.net-core-2.0,Azure,Asp.net Core Mvc,Azure Storage Blobs,Asp.net Core 2.0,我需要一个建议,然后是技术细节 我的应用程序生成PDF并将其存储在Azure存储中的一个专用容器中。但当用户进行身份验证(我使用Azure AD B2C)并转到他的个人页面时,我需要显示这些PDF的链接。现在,这些链接不能公开,因此我认为我需要: 1) 某种中间件在用户访问这些链接时对其进行身份验证 2) 从存储器中请求文件并将其传递给响应 最好的方法是什么?(也考虑性能) 我的第一个想法是使用SAS令牌,将时间限制在5-10分钟左右。但如果用户打开页面,让浏览器打开一个小时,然后回来点击PDF

我需要一个建议,然后是技术细节

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

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

2) 从存储器中请求文件并将其传递给响应

最好的方法是什么?(也考虑性能)


我的第一个想法是使用SAS令牌,将时间限制在5-10分钟左右。但如果用户打开页面,让浏览器打开一个小时,然后回来点击PDF链接会怎么样?

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

出于性能原因,使用SAS令牌是最佳选择。不要在页面加载时生成blob URL,只需在操作中创建指向API的链接,生成令牌,然后将重定向响应返回到azure存储的完整路径(uri+令牌)。这样,用户就不可能获得指向blob.Right的陈旧链接。我在这里可能遇到的唯一问题是当用户获得PDF的实际链接并通过电子邮件发送时。如果令牌过期,则链接断开。如果没有,它基本上是公开的。。。所以我想也许还有另一种方法。不清楚你的预期行为是什么,不管你用什么方法,你介绍的都是一个问题。为什么用户应该通过电子邮件发送指向私人文件的链接?因为客户端是。。。嗯,用户。用户可以很容易地向任何人发送指向文档的链接(您确实在电子邮件中发送链接,对吗?)。