Search code examples
azure-storagedynamics-business-central

Azure File Storage API access using Shared Access Signature


I'm currently trying to access files & directories on an Azure Storage Account Fileshare (let's call it rootdirectory ) from a Dynamics Business Central page action.

The method to access files stored in it is to build and send an HTTP GET request (with a client) to https://myaccount.file.core.windows.net/myfileshare/rootdirectory to retrieve a filelist.

I'm trying to use a Shared Access Signature to authenticate the request.

The blocking point that I can't get through is the following error message :

Authentication information is not given in the correct format. Check the value of Authorization header.

I'm setting my request headers like this :

client.DefaultRequestHeaders().Clear();
client.DefaultRequestHeaders().Add('x-msdate', '2019-02-20');
client.DefaultRequestHeaders().Add('Authorization', '...');

My main problem is that I can't seem to find the proper format (to use in the '...' part) asked in the error message in any tutorial.

I've tried the following formats (and minor variations inside it)

'SharedAccessSignature myaccount:signature'
'SharedAccessSignature sv=2018-03-28&ss=f&srt=sco&sp=rwdlc&se=2019-02-20T18:12:27Z&st=2019-02-20T10:12:27Z&spr=https&sig=signature'
'SharedAccessSignature sr=https%3A%2F%2Fmyaccount.file.core.windows.net%2Fmyfileshare%2Frootdirectory&sig=signature&se=2019-02-21T22:36:05Z&skn=key1'

I also tried putting it in the service URI, nothing better.

Has anyone already encountered this issue ?

Any help would be appreciated.

Thanks for your time.


Solution

  • I would highly recommend checking the following post. They ended up using the below code:

    public AzureSASToken GetSASFromShare(string shareName)
            {
                var share = _fileclient.GetShareReference(shareName);
                share.CreateIfNotExists();
                string policyName = "UPARSharePolicy";
    
                // Create a new shared access policy and define its constraints.
                var sharedPolicy = new SharedAccessFilePolicy()
                {
                    SharedAccessExpiryTime = DateTime.UtcNow.AddDays(15),
                    Permissions = SharedAccessFilePermissions.Read | SharedAccessFilePermissions.Write
                };
    
                // Get existing permissions for the share.
                var permissions = share.GetPermissions();
    
                // Add the shared access policy to the share's policies. 
                // Note that each policy must have a unique name.
                // Maximum 5 policies for each share!
                if (!permissions.SharedAccessPolicies.Keys.Contains(policyName))
                {
                    if (permissions.SharedAccessPolicies.Count > 4)
                    {
                        var lastAddedPolicyName = permissions.SharedAccessPolicies.Keys.Last();
                        permissions.SharedAccessPolicies.Remove(lastAddedPolicyName);
                    }
                    permissions.SharedAccessPolicies.Add(policyName, sharedPolicy);
                    share.SetPermissions(permissions);
                }            
    
                var sasToken = share.GetSharedAccessSignature(sharedPolicy);
                //fileSasUri = new Uri(share.StorageUri.PrimaryUri.ToString() + sasToken);
                return new AzureSASToken ()
                {
                   Name = shareName,
                   Url = share.StorageUri.PrimaryUri.ToString() + "/",
                   SASToken = sasToken        
                };
            }