Cannot generate SAS token for Blob using GetSharedAccessSignature(policy) and Azure Managed Identity.
See original GitHub issueWhich service(blob, file, queue, table) does this issue concern?
Microsoft.Azure.Storage.Blob;
Which version of the SDK was used?
Azure Tools 2.9 Microsoft.Azure.Storage.Blob 10.0.3 Microsoft.Azure.Services.App.Authentication 1.2.0-preview3
Which platform are you using? (ex: .NET Core 2.1)
.NET Core 2.2
What problem was encountered?
Cannot generate SAS token when using Managed Identity. I have App Service on Azure trying to generate SAS token using the RBAC role Assignment. For the time being, I even assigned the identity as “Owner” role but still it cannot generate SAS token. It says, I need a Account Key Credentials. If I have to provide Account Key in the code, then doesn’t it defeat the purpose of Managed Identity. We want to avoid using Storage Key in our solution and use Managed Identity.
Below is the error.
2019-05-22 15:15:19.283 +00:00 [Error] Microsoft.AspNetCore.Server.IIS.Core.IISHttpServer: Connection ID "16861477006485750114", Request ID "80000165-0000-ea00-b63f-84710c7967bb": An unhandled exception was thrown by the application.System.InvalidOperationException: Cannot create Shared Access Signature unless Account Key credentials are used.at Microsoft.Azure.Storage.Blob.CloudBlob.GetSharedAccessSignature(SharedAccessBlobPolicy policy,
How can we reproduce the problem in the simplest way?
const string blobName = “https://yourcontainer.blob.core.windows.net/images/image1.jpg”;
var azureServiceTokenProvider = new AzureServiceTokenProvider();
string accessToken = (azureServiceTokenProvider.GetAccessTokenAsync("https://storage.azure.com/")).Result;
AccountKeyCredentials accountKeyCredentials;
TokenCredential tokenCredential = new TokenCredential(accessToken);
StorageCredentials storageCredentials = new StorageCredentials(tokenCredential);
CloudBlockBlob blob = new CloudBlockBlob(new Uri(blobName),
storageCredentials);
SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy()
{
Permissions = SharedAccessBlobPermissions.Read,
SharedAccessExpiryTime = DateTime.UtcNow.AddDays(24),
};
var sasToken = blob.GetSharedAccessSignature(policy);
Have you found a mitigation/solution?
No.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:21
- Comments:17
Top GitHub Comments
Yes- before you start accessing containers you need to instantiate a blob client passing in a set of TokenCredentials- something like t his:
@Xiaoxin4396 Its been a long time since I probably fixed it, so I don’t remember exactly the cause of the exception. It probably was related to usin
GetSharedAccessSignature
method, rather I usedBlobSasBuilder
in my final code. I am sharing the code, which is running fine currently, hope it helps you in some way