azure-storage-net: Cannot generate SAS token for Blob using GetSharedAccessSignature(policy) and Azure Managed Identity.
Which 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.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 21
- Comments: 17
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@tomgallard I can’t get it to work. GetUserDelegationKey throws “Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.” in GetUserDelegationKey. Note that I’m using a SAS to a container, not the account.
Code:
Any ideas?
@mikeblakeuk Did you figure the cause of this exception?
I am facing same exception with message “Only authentication scheme Bearer is supported” on my local development environment, while creating the BlobServiceClient object, below is the code.
BlobServiceClientntials. blobClient = new BlobServiceClient(connectionString); UserDelegationKey key = blobClient.GetUserDelegationKey(DateTimeOffset.UtcNow, DateTimeOffset.UtcNow.AddDays(7));
My local machine’s timezone/time is not off, I have checked thoroughly
@arkiaconsulting I had the same requirement for container only access. I followed this blog to work with setting up a service-principal with roles. You don’t need to do the custom roles, but the blog is a good reference is walking through setting up the needed resources.
My first run was to assign the “Contributor” and “Storage Blob Data Contributor” to the container-id, but later had to assign those roles to the storage-account-id so that the could use the service-principal login way to generate the SAS.
given the same service-principal-id/secret