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 应用访问这些文件,而且我似乎无法弄清楚如何复制代码以获取令牌(Core库不同)

关于如何在.Net Core 2.2中实现此目标的任何建议?

谢谢!

milleralong 回答:ASP.Net Core 2.2-Azure SAS令牌

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

然后,您的.net核心代码(与您帖子中的.net框架代码相同)可以与.net框架代码一起很好地工作。

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

<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/colorWhite"/>
<corners android:radius="@dimen/margin_4dp"/>
<stroke
    android:width="2dp"
    android:color="#ff0000"/>
<padding
    android:left="8dp"
    android:top="10dp"
    android:bottom="10dp"
    android:right="8dp"/>

测试结果:

enter image description here

本文链接:https://www.f2er.com/3140572.html

大家都在问