This is my first time working with SAS. I am trying to access the azure queues using Service Sas Token and trying to insert a message but it is not working. When I try to access the generated URI from browser then I am getting below error
"The resource doesn't support specified Http Verb. RequestId:cc1f2356-f003-0015-78b6-15f337000000 Time:2023-11-12T22:23:52.3942771Z". What could be the problem? Below is my sample code
Note: I will replace queueName, AccountName and AccountKey with original values.
public async Task<ApiResponse<BlobServiceClient>> GetServiceQueueSasToken()
{
ApiResponse<BlobServiceClient> result = new ApiResponse<BlobServiceClient>();
try
{
string queueName = "queueName";
string accountName = "accountName";
string accountKey = "accountKey";
StorageSharedKeyCredential storageSharedKeyCredential = new(accountName, accountKey);
var serviceQueueUri = new Uri($"https://{accountName}.queue.core.windows.net/{queueName}");
string sasToken = await CreateServiceQueueSASToken(storageSharedKeyCredential, queueName);
BlobServiceClient blobServiceClientAccountSAS = new BlobServiceClient(new Uri($"{serviceQueueUri}? {sasToken}"));
SendMessageUsingSasToken(blobServiceClientAccountSAS.Uri queueName, sasToken);
result.Data = blobServiceClientAccountSAS;
result.IsSucceed = true;
}
catch (Exception ex)
{
result.IsSucceed = false;
result.Exception = ex;
result.ErrorMessages = new List<string>();
result.ErrorMessages.Add(ex.Message);
}
return result;
}
public static async Task<string> CreateServiceQueueSASToken(StorageSharedKeyCredential sharedKey, string queueName)
{
var queueSasBuilder = new QueueSasBuilder()
{
QueueName = queueName,
ExpiresOn = DateTimeOffset.UtcNow.AddDays(1),
Protocol = SasProtocol.Https
};
queueSasBuilder.SetPermissions(QueueSasPermissions.Add | QueueSasPermissions.Read);
string sasToken = queueSasBuilder.ToSasQueryParameters(sharedKey).ToString();
return sasToken;
}
static void SendMessageUsingSasToken(Uri serviceUri, string queueName, string sasToken)
{
var queueClient = new QueueClient(serviceUri, queueName, sasToken);
queueClient.SendMessage("Your message content", sasToken);
Console.WriteLine("Message sent successfully.");
}
The issue with using the SAS token in your
SendMessageUsingSasToken
method. TheQueueClient
constructor already expects the SAS token to be included in the URI, so you don't need to pass it again as a parameter when creating theQueueClient
.The code below generates a Shared Access Signature (SAS) token for an Azure Storage Queue and uses it to send a message to the specified queue.
Output: