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

时间:2020-04-21 14:51:48

标签: javascript c# html azure-blob-storage

我正在创建一封电子邮件,该电子邮件将发送指向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 />");
            }
            messageBody.Append("<a href=\"" + uri + "\" download=\"MyGoogleLogo\">Download Quote PDF</a><br />");
            messageBody.Append("<b>*** Link will expire after 30 days (" + expirationDate.ToString("MM/dd/yyyy") + ") ***</b><br /><br />");
            messageBody.Append("<b>Please do not reply back to this email.</b>");

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

1 个答案:

答案 0 :(得分:1)

您可以在Azure Blob中设置静态页面。

您可以参考offical document来创建包含时间限制逻辑的下载页面。如果链接没有过期,请下载它,如果链接过期,请跳至您的自定义错误页面。

enter image description here

您可以上传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:30

messageBody.Append("<a href=\"" + uri + "\" download=\"MyGoogleLogo\">Download Quote PDF</a><br />");

DownloadPage.html中,您可以通过参数expirationDate检查链接是否未过期。

这只是一个建议,当然,您也可以使用自己的应用程序网站进行更好的逻辑处理。