JTouchLogic
JTouchLogic

Reputation: 57

Azure storage SAS failed to authenticate

Hi I am trying to create SAS token for my file on Azure. I am getting "Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature" error. Following is my code.

    public async Task<IActionResult> TestBlobClient()
{
    try
    {
        string storageAccount = "myaccount";
        string password = "mykey";

        var sharedKeyCredential = new StorageSharedKeyCredential(storageAccount, password);

        var shareClient = new ShareClient(new Uri("https://aaa.file.core.windows.net/zttsox20201027154501"), sharedKeyCredential);

        ShareDirectoryClient directory = shareClient.GetDirectoryClient("Output/14");
        ShareFileClient file = directory.GetFileClient("637655759841727494_main.wav");

        var shareSasBuilder = new ShareSasBuilder
        {
            ShareName = "aaa",
            Protocol = SasProtocol.None,
            ExpiresOn = DateTime.UtcNow.AddYears(50),
            Resource = "b"
        };

        shareSasBuilder.SetPermissions(ShareFileSasPermissions.Read);
        var url = new Uri(file.Uri + "?" + shareSasBuilder.ToSasQueryParameters(sharedKeyCredential).ToString());

        return Ok(url);
    }
    catch (Exception ex)
    {

        return StatusCode(StatusCodes.Status400BadRequest, ex.Message);
    }

Upvotes: 0

Views: 1303

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136136

The issue is with Resource = "b". When you set the resource as b, that means you are getting a SAS token for a blob. Considering you're getting a SAS token for a file share, the value of this parameter should be s.

Please try something like:

        var shareSasBuilder = new ShareSasBuilder
        {
            ShareName = "zttsox20201027154501",
            Protocol = SasProtocol.None,
            ExpiresOn = DateTime.UtcNow.AddYears(50),
            Resource = "s"
        };

For more details, please see this link: https://learn.microsoft.com/en-us/dotnet/api/azure.storage.sas.sharesasbuilder.resource?view=azure-dotnet#Azure_Storage_Sas_ShareSasBuilder_Resource.

Upvotes: 1

Related Questions