Skip to content

Instantly share code, notes, and snippets.

Last active July 21, 2022 08:44
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
using System;
using Azure.Storage;
using Azure.Storage.Sas;
using Azure.Storage.Blobs;
using Azure.Storage;
using System.Collections.Generic;
using System.Web;
using Azure.Storage.Files.Shares;
namespace ConsoleApp1
class Program
private string GetKeyValueFromConnectionString(string key)
var connectionString = "CONNECTIONSTRING";
IDictionary<string, string> settings = new Dictionary<string, string>();
var splitted = connectionString.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var nameValue in splitted)
var splittedNameValue = nameValue.Split(new char[] { '=' }, 2);
settings.Add(splittedNameValue[0], splittedNameValue[1]);
return settings[key];
public void GetAdHocContainerSasToken()
var containerName = "CONTAINERNAME";
var connectionString = "CONNECTIONSTRING";
var sasBuilder = new ShareSasBuilder()
ShareName = containerName,
Resource = "s", //Value b is for generating token for a Blob and c is for container
StartsOn = DateTime.UtcNow.AddMinutes(-2),
ExpiresOn = DateTime.UtcNow.AddMinutes(10),
sasBuilder.SetPermissions(ShareSasPermissions.Read | ShareSasPermissions.Write| ShareSasPermissions.List); //multiple permissions can be added by using | symbol
var sasToken = sasBuilder.ToSasQueryParameters(new StorageSharedKeyCredential(GetKeyValueFromConnectionString("AccountName"), GetKeyValueFromConnectionString("AccountKey")));
//Console.WriteLine($"{new BlobContainerClient(connectionString, containerName).Uri}?{sasToken}");
Console.WriteLine($"{new ShareClient(connectionString, containerName).Uri}?{sasToken}&restype=directory&comp=list");
/* Note : If you want to list the items inside container and view those details in a browser based on the generated SAS token
* then two additional query parameters has to be appended to the token
* the Query parameters are "restype=container&comp=list"
class Run
static void Main(string[] args)
var class1 = new Program();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment