none
SAS Token Expired- How to generate it programmatically?

    Question

  • I have a storage account that contains blob and i'm using this blob to save the images uploaded from UWP application consuming Azure Mobile Service. The code block I wrote to upload - shown below- throws an exception stating that the SAS token is expired. how can I generate a new SAS token programmatically?

    CloudBlobContainer container = new CloudBlobContainer(
                        new Uri("URL"),cred);
    
                    // Upload the new image as a BLOB from the stream.
                    CloudBlockBlob blobFromSASCredential =
                        container.GetBlockBlobReference(App.ViewModel.CurrentUser.ResourceName);
                  
                     selectedImage.Seek(0, SeekOrigin.Begin);
                    await blobFromSASCredential.UploadFromStreamAsync(selectedImage);

    Saturday, December 12, 2015 11:46 AM

Answers

  • You can create it programmatically using the code segment below. the SAS token generated in the sample below expires every 4 hours, you can change it based on your app scenario.

      StorageCredentials credss=new StorageCredentials("<Storage Account Name?","<Primary Access Key>
                    CloudBlobContainer tempContainer=new CloudBlobContainer(
                        new Uri("<Blob URL>"),credss);
    
                        SharedAccessBlobPolicy sasConstraints = new SharedAccessBlobPolicy();
        sasConstraints.SharedAccessExpiryTime = DateTime.UtcNow.AddHours(4);
        sasConstraints.Permissions = SharedAccessBlobPermissions.Write | SharedAccessBlobPermissions.List;
    
        //Generate the shared access signature on the container, setting the constraints directly on the signature.
        string sasContainerToken = tempContainer.GetSharedAccessSignature(sasConstraints);

    • Marked as answer by Yasmine Gamal Saturday, December 12, 2015 11:49 AM
    Saturday, December 12, 2015 11:49 AM