Model view controller 如何在MVC中从私有Azure存储在.cshtml页面中显示图像?

Model view controller 如何在MVC中从私有Azure存储在.cshtml页面中显示图像?,model-view-controller,azure-storage-blobs,Model View Controller,Azure Storage Blobs,检查html页面时出现以下错误: <Error> <Code>AuthorizationFailure</Code> <Message>This request is not authorized to perform this operation. RequestId:7b853423-101e-0015-51e5-905acd000000 Time:2020-09-22T13:38:0

检查html页面时出现以下错误:

<Error>
    <Code>AuthorizationFailure</Code>
    <Message>This request is not authorized to perform this operation.
           RequestId:7b853423-101e-0015-51e5-905acd000000
           Time:2020-09-22T13:38:05.2479851Z
    </Message>
</Error>
我已在web.config文件中为Azure connectionstring创建了密钥,该文件是访问Azure存储blob的书面方法,使之成为私有到公共:

public ActionResult BlobManager(string ContainerName = "images")
{
    string ConnectionString = ConfigurationManager.AppSettings["AzureStorageConnectionString"].ToString();
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConnectionString);
    CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
    blobContainer = cloudBlobClient.GetContainerReference(ContainerName);

    //Make available to everyone
    blobContainer.SetPermissions(new BlobContainerPermissions{
        PublicAccess = BlobContainerPublicAccessType.Blob
        });
    }
这是我的控制器方法:

[HttpGet]
public JsonResult GetImageList(int TaskID)
{
    var data = (from d in _context.ImgTable
                where (d.Taskid == TaskID )
                select new
                {
                    d.ImageID,
                    d.Taskid,
                    d.ImagePath,
                }).ToList();
    return Json(data, JsonRequestBehavior.AllowGet);
}
请帮助我如何走得更远。我想在html页面中显示图像。 我正在从dropdownlist中选择Taskid,单击它会在html表中显示与该Taskid相关的图像。下面是js函数:

$.ajax({
        type: 'GET',
        url: '@Url.Action("GetImageList")',
        datatype: JSON,
        data: {
                'TaskID': $("#tasklist").val()                //getting selected TaskID from dropdown
        },
        success: function (data) {
                 var row_no = 1;
                 $.each(data, function (i, item) {
             
                 var path = "https://abcdatastorage.blob.core.windows.net/task/";     //azure storage url
                 var img = item.ImagePath;                                            //getting imagepath from local db
                     img = img.replace(/\s+/g, '%20');
                 var img1 = img.split('\\');
                     img = (path + img1[5]);                                          //joining above url to get complete url to bind with <img src>

            
                + "<td id='ItemId" + row_no + "' >" + item.ID + "</td>"               // displaying image id in html table

                "<td>"
                + "<div><img id='Image" + row_no + "' src=" + img + " class='img-fluid img-thumbnail' alt='' style='width:500px; height:350px;' />"  // display image for that imageid
                + "</div></td > "
     });
$.ajax({
键入:“GET”,
url:“@url.Action”(“GetImageList”),
数据类型:JSON,
数据:{
'TaskID':$(“#tasklist”).val()//从下拉列表中获取选定的TaskID
},
成功:功能(数据){
var row_no=1;
$。每个(数据、功能(i、项){
变量路径=”https://abcdatastorage.blob.core.windows.net/task/“;//azure存储url
var img=item.ImagePath;//从本地数据库获取ImagePath
img=img.replace(/\s+/g,'%20');
var img1=img.split('\\');
img=(path+img1[5]);//加入上述url以获得要绑定的完整url
+“”+item.ID+“”//在html表中显示图像ID
""
+“”//显示该imageid的图像
+ " "
});
我可以在html中直接传递azure存储url吗?我可以访问和显示图像吗? 如果azure存储是私有的,图像将如何显示

下面是我用js编写的链接url得到的src。但没有看到图像

<div>
    <img src='https://abcimages.blob.core.windows.net/images/418;1488163937486;d%20V%20Phase%205.0%20kV.jpg' class='img-fluid img-thumbnail' alt='' style='width:500px; height:350px;' />
</div>


请帮助解决此问题。我如何通过代码访问专用azure存储并显示图像。

我有一个简单的方法来实现此功能,您的代码不需要做很多修改

我的建议是编写一个通用的
GetAccountSASToken
方法。如果需要安全验证,建议在通过验证后调用此方法,以便生成的
sas令牌
可以被生成并附加到img URL中

你可以阅读官方文件


我有一个简单的方法来实现这个函数,您的代码不需要做很多修改

我的建议是编写一个通用的
GetAccountSASToken
方法。如果需要安全验证,建议在通过验证后调用此方法,以便生成的
sas令牌
可以被生成并附加到img URL中

你可以阅读官方文件


你的问题解决了吗?我的方法对你有帮助吗?是的,你的方法对你有帮助吗?我提出的这个问题与这个问题有点类似,但需要新的方法:你使用azure web app吗?如果是,请修改标签,我稍后会检查。你的问题解决了吗?我的方法对你有帮助吗?是的,你的方法对你有帮助吗我提出的这个问题与这个问题有点类似,但需要新的方法:你使用azure web app吗?如果是,请修改标签,我稍后会检查。
static void Main(string[] args)
{
    Console.WriteLine("Hello World!");
    Console.WriteLine(GetAccountSASToken());
}
public static string GetAccountSASToken()
{
    // To create the account SAS, you need to use Shared Key credentials. Modify for your account.
    const string ConnectionString = "DefaultEndpointsProtocol=**fix=core.windows.net";
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConnectionString);

    // Create a new access policy for the account.
    SharedAccessAccountPolicy policy = new SharedAccessAccountPolicy()
    {
        Permissions = SharedAccessAccountPermissions.Read | SharedAccessAccountPermissions.Write | SharedAccessAccountPermissions.List,
        Services = SharedAccessAccountServices.Blob | SharedAccessAccountServices.File,
        ResourceTypes = SharedAccessAccountResourceTypes.Service | SharedAccessAccountResourceTypes.Container | SharedAccessAccountResourceTypes.Object,
        SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24),
        Protocols = SharedAccessProtocol.HttpsOnly
    };

    // Return the SAS token.
    return storageAccount.GetSharedAccessSignature(policy);
}