首页 > 解决方案 > ASP.Net Core 2.2 - Azure SAS 令牌?

问题描述

我试图弄清楚如何为用户生成基于时间的令牌,以访问存储在 Azure 存储帐户 Blob 容器中的数据。用户上传各种数据(PDF、图像),但我不希望这些数据的链接公开。推荐的策略是使用 SAS 令牌,我能够使用大约一年前在 MS 网站上找到的以下功能在 .Net 下使其工作:

        //Function for getting a temporary Azure SAS 
        public static string GetAzureSASToken(string userhashid, int minutes)
        {

            //Get Azure SAS Token so we can allow them to temporarily view the photos 
            UploadedFileInfo uploadedfileinfo = new UploadedFileInfo();

            //Azure User containter must be all lowercase!!
            uploadedfileinfo.usercontainer = "user-" + userhashid;
            uploadedfileinfo.azureurl = ConfigurationManager.AppSettings["AzureURL"].ToString() + uploadedfileinfo.usercontainer + "/";


            // Retrieve storage account from connection string.
            string azureconnection = CloudConfigurationManager.GetSetting("StorageConnectionString");

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(azureconnection);

            CloudBlobClient client = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer blobContainer = client.GetContainerReference(uploadedfileinfo.usercontainer);

            //Set the expiry time and permissions for the container.
            //In this case no start time is specified, so the shared access signature becomes valid immediately.
            SharedAccessBlobPolicy sasConstraints = new SharedAccessBlobPolicy();
            sasConstraints.SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(minutes);
            sasConstraints.Permissions = SharedAccessBlobPermissions.Read;

            //Generate the shared access signature on the container, setting the constraints directly on the signature.
            string sasContainerToken = blobContainer.GetSharedAccessSignature(sasConstraints);

            return sasContainerToken;

        }

问题是我现在需要从Asp.Net Core 2.2应用程序访问这些文件,我似乎无法弄清楚如何复制代码来获取令牌(核心库不同)

有关如何在 .Net Core 2.2 中完成此任务的任何建议?

谢谢!

标签: azureasp.net-coreazure-storageazure-blob-storage

解决方案


只需安装最新的 nuget 包Microsoft.Azure.Storage.Blob -Version 11.1.0

然后您的 .net 核心代码(与您帖子中的 .net 框架代码相同)可以像 .net 框架代码一样工作。

这是 .net core 2.2 的示例代码。我没有从配置文件中读取设置,所以有点不同:

using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.Auth;
using Microsoft.Azure.Storage.Blob;
using System;

namespace ConsoleApp5
{
    class Program
    {
        static void Main(string[] args)
        {
            string sas = GetAzureSASToken();

            Console.WriteLine(sas);
            Console.ReadLine();
        }

        public static string GetAzureSASToken()
        {

            string accountName = "xxx";
            string accountKey = "xxx";

            CloudStorageAccount storageAccount = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);

            CloudBlobClient client = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer blobContainer = client.GetContainerReference("test1");

            //Set the expiry time and permissions for the container.
            //In this case no start time is specified, so the shared access signature becomes valid immediately.
            SharedAccessBlobPolicy sasConstraints = new SharedAccessBlobPolicy();
            sasConstraints.SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(5);
            sasConstraints.Permissions = SharedAccessBlobPermissions.Read;

            //Generate the shared access signature on the container, setting the constraints directly on the signature.
            string sasContainerToken = blobContainer.GetSharedAccessSignature(sasConstraints);

            return sasContainerToken;
        }
    }
}

和测试结果:

在此处输入图像描述


推荐阅读