'How to validate Azure Blob Storage Shared Access Signature (SAS) URL?

My application allows users to enter an Azure Blob Storage SAS URL. How would I go about validating it? I'm using the Azure Storage Blobs client library, and there doesn't seem to be any way of validating SAS URLs without actually performing a blob operation (which I don't want to do).

The validation operation can be asynchronous and involve an API call if necessary (ie it can be triggered with a button).

public class SASURLValidator
{
    public async Task<bool> ValidateSASURL(string sasURL)
    {
        // What goes here?
    }

    public async Task Test()
    {
        var result = await ValidateSASURL("https://blobstorageaccountname.blob.core.windows.net/containerName?sp=w&st=2022-02-15T02:07:49Z&se=2022-03-15T10:07:49Z&spr=https&sv=2020-08-04&sr=c&sig=JDFJEF342JDSFJIERJsdjfkajiwSKDFJIQWJIFJSKDFJWE%3D")
        // result should be true if the above is a valid SAS
    }
}


Solution 1:[1]

You man test the list or write and delete access. Depending on your scenario you can use on of both. It would be also possible to modify the sample for testing. read access to a singe file.

private async Task TestSasAsync(String uri, bool testWriteAndDelete, bool testList)
        {
            try
            {
                var cloudBlobContainer = new CloudBlobContainer(new Uri(uri));

                if (testList)
                {
                    foreach (var blob in cloudBlobContainer.ListBlobs())
                    {
                        Console.WriteLine(blob.Uri);
                    }
                }

                if (testWriteAndDelete)
                {
                    var blockBlob = cloudBlobContainer.GetBlockBlobReference("testBlob.txt");
                    await blockBlob.UploadTextAsync("Hello world");
                    await blockBlob.DeleteAsync();
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Failed to validate SAS Uri: " + ex.Message, ex);
            }
        }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Daniel W.