猿问

检查电子邮件中的下载链接是否有效

我正在创建一封电子邮件,该电子邮件将发送指向 Azure Blob 容器中的 PDF 的链接。我还对链接设置了时间限制,因此 30 天后它将不再有效。这是创建天蓝色链接和电子邮件的代码:


// Establishes a connection with Azure.

            string storageConnection = CloudConfigurationManager.GetSetting("AzureBlobConnectionString");

            CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(storageConnection);


            // Gets access to the quote blob container.

            CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();

            CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference("quotes");

            cloudBlobContainer.CreateIfNotExists(BlobContainerPublicAccessType.Blob);


            // Inserts the pdf into Azure Blob.

            CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference("Quote_" + orderId + "_" + DateTime.Now.ToString("yyyy_MM_dd") + ".pdf"); ;

            await cloudBlockBlob.UploadFromStreamAsync(report.ExportToStream(ExportFormatType.PortableDocFormat));


            // Creates the 30 day time limit to access the pdf.

            DateTime expirationDate = DateTime.UtcNow.Add(new TimeSpan(30, 0, 0, 0));

            SharedAccessBlobPolicy sharedAccessBlobPolicy = new SharedAccessBlobPolicy();

            sharedAccessBlobPolicy.SharedAccessExpiryTime = expirationDate;

            sharedAccessBlobPolicy.Permissions = SharedAccessBlobPermissions.Read;


            // Creates the uri with the time limit.

            string sharedAccesSignature = cloudBlockBlob.GetSharedAccessSignature(sharedAccessBlobPolicy);

            string uri = cloudBlockBlob.Uri.AbsoluteUri + sharedAccesSignature;


            StringBuilder messageBody = new StringBuilder();

            messageBody.Append("Your Quote is attached.<br /><br />");

            if (additionalNotes.Replace(" ", "").Length > 0)

            {

                messageBody.Append("Addtional Notes:<br />");

                messageBody.Append(additionalNotes + "<br /><br />");

            }


如果用户在时间限制后单击链接,是否可以将用户重定向到设计的错误页面?


扬帆大鱼
浏览 72回答 1
1回答

海绵宝宝撒

您可以在 Azure Blob 中设置静态页面。您可以参考官方文档来创建您的下载页面,其中包含时间限制的逻辑。如果链接未过期,请下载它,如果过期,请跳转到您的自定义错误页面。您可以上传您的代码DownloadPage.html并修改代码。的值uri设置为 https://pan**storage.blob.core.windows.net/asset-*****-4baf-48a5-9ea1-6cb09319e679/DownloadPage.html?downloadurl=XXXXXXXX&expirationDate=2020-05-22 15:40:30messageBody.Append("<a href=\"" + uri + "\" download=\"MyGoogleLogo\">Download Quote PDF</a><br />");中DownloadPage.html,您可以通过参数检查链接是否过期expirationDate。这只是一个建议,当然你也可以使用自己的应用网站进行更好的逻辑处理。
随时随地看视频慕课网APP

相关分类

Html5
我要回答