How to set Shared Access Signatures for Storage that takes immediate effect

Issue 

While working on a recent issue for one of our customers we came across the following error. The code was trying to set shared access signature for the blob storage and would later download it from the application. However it was failing intermittently with below error

Server failed to Authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.

Reason

Customer was using below code and wanted to set the shared access signature with immediate effect using the SharedAccessStartTime property. This was actually causing clock differences sometime leading the error.

 blobPermissions.SharedAccessPolicies.Add("mypolicy", new SharedAccessPolicy()

   {

     SharedAccessStartTime = DateTime.UtcNow();

     SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(30);

     Permissions = SharedAccessPermissions.Read

   };

 

Resolution 

It is advised that if you want shared access signature policy to take effect immediately then don’t set the SharedAccessStartTime property. We did the same to get this issue resolved.