【发布时间】:2022-01-12 11:43:22
【问题描述】:
我在尝试使用从策略生成的 Sas 访问 blob(图像)时收到以下错误。 MS学习项目的一部分:https://docs.microsoft.com/en-us/learn/modules/control-access-to-azure-storage-with-sas/6-exercise-use-stored-access-policies
练习两次,结果仍然相同。任何想法为什么签名是一个问题?
AuthenticationFailed
服务器未能验证请求。确保 Authorization 标头的值正确形成,包括签名。 RequestId:328fa4c1-401e-004c-49d2-06e232000000 时间:2022-01-11T10:02:52.3736100Z
签名不匹配。使用的签名字符串是 /blob/medicalrecords10150/patient-images patient-images-policy 2020-08-04 c
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Configuration;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using Azure.Storage.Sas;
using Azure.Storage;
namespace patientrecords.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class PatientRecordsController : ControllerBase
{
private readonly ILogger<PatientRecordsController> _logger;
private IConfiguration _iconfiguration;
private BlobContainerClient _container;
private String _storedPolicyID = "patient-images-policy";
public PatientRecordsController(ILogger<PatientRecordsController> logger, IConfiguration iconfiguration)
{
_logger = logger;
_iconfiguration = iconfiguration;
_container = new BlobContainerClient(
_iconfiguration.GetValue<string>("StorageAccount:ConnectionString"),
_iconfiguration.GetValue<string>("StorageAccount:Container")
);
CreateStoredAccessPolicy();
}
// GET PatientRecord
[HttpGet]
public IEnumerable<PatientRecord> Get()
{
List<PatientRecord> records = new List<PatientRecord>();
foreach (BlobItem blobItem in _container.GetBlobs())
{
BlobClient blob = _container.GetBlobClient(blobItem.Name);
var patient = new PatientRecord { name=blob.Name, imageURI=blob.Uri.ToString() };
records.Add(patient);
}
return records;
}
// GET PatientRecord/patient-nnnnnn
[HttpGet("{Name}")]
public PatientRecord Get(string name)
{
BlobClient blob = _container.GetBlobClient(name);
return new PatientRecord { name=blob.Name, imageURI=blob.Uri.AbsoluteUri };
}
// GET PatientRecord/patient-nnnnnn/secure
[HttpGet("{Name}/{secure}")]
public PatientRecord Get(string name, string flag)
{
BlobClient blob = _container.GetBlobClient(name);
return new PatientRecord { name=blob.Name, imageURI=blob.Uri.AbsoluteUri, sasToken=GetBlobSas() };
}
// Build a SAS token for the given blob
private string GetBlobSas(BlobClient blob)
{
// Create a user SAS that only allows reading for a minute
BlobSasBuilder sas = new BlobSasBuilder
{
BlobContainerName = blob.BlobContainerName,
BlobName = blob.Name,
Resource = "b",
ExpiresOn = DateTimeOffset.UtcNow.AddMinutes(1)
};
// Allow read access
sas.SetPermissions(BlobSasPermissions.Read);
// Use the shared key to access the blob
var storageSharedKeyCredential = new StorageSharedKeyCredential(
_iconfiguration.GetValue<string>("StorageAccount:AccountName"),
_iconfiguration.GetValue<string>("StorageAccount:AccountKey")
);
return '?' + sas.ToSasQueryParameters(storageSharedKeyCredential).ToString();
}
// Use a stored access policy for the SAS
private void CreateStoredAccessPolicy()
{
// Create a stored access policy for our blobs
BlobSignedIdentifier identifier = new BlobSignedIdentifier
{
Id = _storedPolicyID,
AccessPolicy = new BlobAccessPolicy
{
//ExpiresOn = DateTimeOffset.UtcNow.AddHours(1),
Permissions = "r"
}
};
_container.SetAccessPolicy(permissions: new BlobSignedIdentifier[] { identifier });
}
// Build a SAS token for the given blob
private string GetBlobSas()
{
// Create a user SAS that only allows reading for a minute
BlobSasBuilder sas = new BlobSasBuilder
{
Identifier = _storedPolicyID
};
// Use the shared key to access the blob
var storageSharedKeyCredential = new StorageSharedKeyCredential(
_iconfiguration.GetValue<string>("StorageAccount:AccountName"),
_iconfiguration.GetValue<string>("StorageAccount:AccountKey")
);
return '?' + sas.ToSasQueryParameters(storageSharedKeyCredential).ToString();
}
}
}
【问题讨论】:
-
请分享您的 SAS 令牌。在共享之前,请混淆 SAS 令牌的
sig部分。
标签: c# azure azure-storage