title: Problems generating a SAS token in C# for Blob paths with special characters
body: <p>We are implementing a file store in our application and we store all the files in private containers in Azure Blob Storage. We have a virtual folder system which we replicate in our Blob storage.</p>
  <p>For example, Let's say i work for <code>Company A</code>, and i upload <code>file_1.txt</code> to <code>Folder #1</code>, it will reside in <code>/vault/Company A/Folder #1/file_1.txt</code> in the Blob storage.</p>
  <p>We generate SAS tokens using the following code:</p>
  <pre><code>public  static Uri GetServiceSasUriForCloudBlockBlob(CloudBlockBlob cloudBlockBlob, string permissions = &quot;r&quot;)
  {
              
      var sasBuilder = new SharedAccessBlobPolicy()
      {
          SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5),
          SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(5),
          Permissions = SharedAccessBlobPolicy.PermissionsFromString(permissions)
      };
              
      var sasUri = cloudBlockBlob.GetSharedAccessSignature(sasBuilder);
  
      return new Uri(cloudBlockBlob.Uri + sasUri);
  
  }
  </code></pre>
  <p>However, this does not work. The error we get is:</p>
  <pre><code>&lt;Error&gt;
      &lt;script type=&quot;text/javascript&quot;/&gt;
      &lt;Code&gt;AuthenticationFailed&lt;/Code&gt;
      &lt;Message&gt;Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. RequestId:f82118d1-101e-002a-1381-97ac16000000 Time:2022-07-14T12:55:34.6370028Z&lt;/Message&gt;
      &lt;AuthenticationErrorDetail&gt;Signature did not match. String to sign used was r 2022-07-14T12:50:27Z 2022-07-14T13:00:27Z /blob/[blobname]/vault/Company A/Folder #1/file_1.txt 2019-07-07 b &lt;/AuthenticationErrorDetail&gt;
  &lt;/Error&gt;
  </code></pre>
  <p>When generating a SAS token from the Azure Portal or the Azure Storage Explorer there is no problem</p>
  <p>It seems to be an issue with the special characters in the path to the file in the Blob. So we tried escaping all spaces and special characters manually to fix this issue, however when doing this the CloudBlockBlob encodes it again (e.g.: it escapes <code>My%20File.txt</code> to <code>My%2520File.txt</code>).</p>
  <p>Currently the only operation we use is Read on Objects, but this may be expanded in the future.</p>
  <p>We could disallow spaces and special character in folders/files but this doesn't feel like solving the issue but working around it. How can we fix this without implementing naming policies?</p>
  <p><strong>EDIT:</strong> Turns out this was a design issue, and while the SDK docs never explicitly disencourages the use unescaped blob paths it does disallow container names with anything other than alphanumerics and dashes.</p>