简体   繁体   中英

Check if Download Link Works in Email

I am creating an email that will send out a link to a PDF in an Azure Blob Container. I am also playing a time limit on the link so it will no longer work after 30 days. Here is what my code that creates the azure link and email message looks like:

// 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>");

Is there a way to redirect the user to a designed error page if they click the link after the time limit?

You can set static page in Azure Blobs.

You can refer the offical document to create your download page which contains the logic of time limit. If the link does not expire, download it, and if it expires, jump to your custom error page.

在此处输入图像描述

You can upload your DownloadPage.html ,and modify code. The value of uri set like 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 />");

In the DownloadPage.html , you can check the link does not expire by parameter expirationDate .

This is just a suggestion, of course, you can also use your own application website for better logic processing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM