SasQueryParameters
A SasQueryParameters object represents the components
that make up an Azure Storage Shared Access Signature's query
parameters. It includes components used by all Azure Storage resources
(Blob Containers, Blobs, Files, and Queues). You can construct a new instance
using the service specific SAS builder types.
For more information,
Create a service SAS.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Text;
namespace Azure.Storage.Sas
{
public class SasQueryParameters
{
public const string DefaultSasVersion = "2025-11-05";
private string _version;
private (AccountSasServices? Parsed, string Raw) _services;
private (AccountSasResourceTypes? Parsed, string Raw) _resourceTypes;
private (SasProtocol? Parsed, string Raw) _protocol;
private DateTimeOffset _startTime;
private string _startTimeString;
private DateTimeOffset _expiryTime;
private string _expiryTimeString;
private SasIPRange _ipRange;
private string _identifier;
private string _resource;
private string _permissions;
private string _signature;
private string ;
private string _agentObjectId;
private string _correlationId;
private int? _directoryDepth;
private string _encryptionScope;
private string _cacheControl;
private string _contentDisposition;
private string _contentEncoding;
private string _contentLanguage;
private string _contentType;
private static readonly string[] s_sasTimeFormats = new string[4] {
"yyyy-MM-ddTHH:mm:ssZ",
"yyyy-MM-ddTHH:mm:ss.fffffffZ",
"yyyy-MM-ddTHH:mmZ",
"yyyy-MM-dd"
};
public string Version => _version ?? SasQueryParametersInternals.DefaultSasVersionInternal;
public AccountSasServices? Services => _services.Parsed;
public AccountSasResourceTypes? ResourceTypes => _resourceTypes.Parsed;
public SasProtocol Protocol => _protocol.Parsed ?? SasExtensions.ParseProtocol(_protocol.Raw);
private string ProtocolAsString {
get {
object text = _protocol.Raw;
if (text == null) {
ref SasProtocol? item = ref _protocol.Parsed;
if (!item.HasValue)
return null;
text = item.GetValueOrDefault().ToProtocolString();
}
return (string)text;
}
}
public DateTimeOffset StartsOn => _startTime;
internal string StartsOnString => _startTimeString;
public DateTimeOffset ExpiresOn => _expiryTime;
internal string ExpiresOnString => _expiryTimeString;
public SasIPRange IPRange => _ipRange;
public string Identifier => _identifier ?? string.Empty;
public string Resource => _resource ?? string.Empty;
public string Permissions => _permissions ?? string.Empty;
public string CacheControl => _cacheControl ?? string.Empty;
public string ContentDisposition => _contentDisposition ?? string.Empty;
public string ContentEncoding => _contentEncoding ?? string.Empty;
public string ContentLanguage => _contentLanguage ?? string.Empty;
public string ContentType => _contentType ?? string.Empty;
public string => _preauthorizedAgentObjectId ?? string.Empty;
public string AgentObjectId => _agentObjectId ?? string.Empty;
public string CorrelationId => _correlationId ?? string.Empty;
public int? DirectoryDepth => _directoryDepth ?? null;
public string EncryptionScope => _encryptionScope ?? string.Empty;
public string Signature => _signature ?? string.Empty;
public static SasQueryParameters Empty => new SasQueryParameters();
protected SasQueryParameters()
{
}
protected SasQueryParameters(IDictionary<string, string> values)
{
foreach (KeyValuePair<string, string> item in (IEnumerable<KeyValuePair<string, string>>)values.ToArray()) {
bool flag = true;
string text = item.Key.ToUpperInvariant();
if (text != null) {
switch (text.Length) {
case 2:
break;
case 3:
goto IL_009f;
case 4:
goto IL_00dd;
case 5:
goto IL_0113;
default:
goto IL_04c4;
}
switch (text[1]) {
case 'V':
break;
case 'S':
goto IL_0148;
case 'T':
goto IL_015d;
case 'E':
goto IL_0172;
case 'I':
goto IL_0187;
case 'R':
goto IL_019c;
case 'P':
goto IL_01b1;
default:
goto IL_04c4;
}
if (text == "SV") {
_version = item.Value;
goto IL_04c6;
}
}
goto IL_04c4;
IL_022f:
if (!(text == "SES"))
goto IL_04c4;
_encryptionScope = item.Value;
goto IL_04c6;
IL_04c6:
if (flag)
values.Remove(item.Key);
continue;
IL_0113:
switch (text[1]) {
case 'A':
break;
case 'U':
goto IL_02d2;
default:
goto IL_04c4;
}
if (!(text == "SAOID"))
goto IL_04c4;
_preauthorizedAgentObjectId = item.Value;
goto IL_04c6;
IL_02d2:
if (!(text == "SUOID"))
goto IL_04c4;
_agentObjectId = item.Value;
goto IL_04c6;
IL_01b1:
if (!(text == "SP"))
goto IL_04c4;
_permissions = item.Value;
goto IL_04c6;
IL_0148:
if (!(text == "SS"))
goto IL_04c4;
_services = (SasExtensions.ParseAccountServices(item.Value), item.Value);
goto IL_04c6;
IL_021a:
if (!(text == "SDD"))
goto IL_04c4;
_directoryDepth = int.Parse(item.Value, NumberStyles.Integer, CultureInfo.InvariantCulture);
goto IL_04c6;
IL_00dd:
switch (text[3]) {
case 'C':
break;
case 'D':
goto IL_0259;
case 'E':
goto IL_027e;
case 'L':
goto IL_0293;
case 'T':
goto IL_02a8;
default:
goto IL_04c4;
}
if (!(text == "RSCC"))
goto IL_04c4;
_cacheControl = item.Value;
goto IL_04c6;
IL_02a8:
if (!(text == "RSCT"))
goto IL_04c4;
_contentType = item.Value;
goto IL_04c6;
IL_01f0:
if (!(text == "SIP"))
goto IL_04c4;
_ipRange = SasIPRange.Parse(item.Value);
goto IL_04c6;
IL_0293:
if (!(text == "RSCL"))
goto IL_04c4;
_contentLanguage = item.Value;
goto IL_04c6;
IL_0187:
if (!(text == "SI"))
goto IL_04c4;
_identifier = item.Value;
goto IL_04c6;
IL_027e:
if (!(text == "RSCE"))
goto IL_04c4;
_contentEncoding = item.Value;
goto IL_04c6;
IL_04c4:
flag = false;
goto IL_04c6;
IL_0259:
if (!(text == "RSCD")) {
if (!(text == "SCID"))
goto IL_04c4;
_correlationId = item.Value;
} else
_contentDisposition = item.Value;
goto IL_04c6;
IL_0172:
if (!(text == "SE"))
goto IL_04c4;
_expiryTimeString = item.Value;
_expiryTime = ParseSasTime(item.Value);
goto IL_04c6;
IL_015d:
if (!(text == "ST"))
goto IL_04c4;
_startTimeString = item.Value;
_startTime = ParseSasTime(item.Value);
goto IL_04c6;
IL_01db:
if (!(text == "SPR"))
goto IL_04c4;
_protocol = (SasExtensions.ParseProtocol(item.Value), item.Value);
goto IL_04c6;
IL_0205:
if (!(text == "SIG"))
goto IL_04c4;
_signature = item.Value;
goto IL_04c6;
IL_019c:
if (!(text == "SR"))
goto IL_04c4;
_resource = item.Value;
goto IL_04c6;
IL_009f:
switch (text[2]) {
case 'T':
break;
case 'R':
goto IL_01db;
case 'P':
goto IL_01f0;
case 'G':
goto IL_0205;
case 'D':
goto IL_021a;
case 'S':
goto IL_022f;
default:
goto IL_04c4;
}
if (!(text == "SRT"))
goto IL_04c4;
_resourceTypes = (SasExtensions.ParseResourceTypes(item.Value), item.Value);
goto IL_04c6;
}
}
protected SasQueryParameters(string version, AccountSasServices? services, AccountSasResourceTypes? resourceTypes, SasProtocol protocol, DateTimeOffset startsOn, DateTimeOffset expiresOn, SasIPRange ipRange, string identifier, string resource, string permissions, string signature, string cacheControl = null, string contentDisposition = null, string contentEncoding = null, string contentLanguage = null, string contentType = null, string authorizedAadObjectId = null, string unauthorizedAadObjectId = null, string correlationId = null, int? directoryDepth = default(int?), string encryptionScope = null)
{
_version = version;
_services = (services, services?.ToPermissionsString());
_resourceTypes = (resourceTypes, resourceTypes?.ToPermissionsString());
_protocol = (protocol, protocol.ToProtocolString());
_startTime = startsOn;
_startTimeString = startsOn.ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture);
_expiryTime = expiresOn;
_expiryTimeString = expiresOn.ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture);
_ipRange = ipRange;
_identifier = identifier;
_resource = resource;
_permissions = permissions;
_signature = signature;
_cacheControl = cacheControl;
_contentDisposition = contentDisposition;
_contentEncoding = contentEncoding;
_contentLanguage = contentLanguage;
_contentType = contentType;
_preauthorizedAgentObjectId = authorizedAadObjectId;
_agentObjectId = unauthorizedAadObjectId;
_correlationId = correlationId;
_directoryDepth = directoryDepth;
_encryptionScope = encryptionScope;
}
[EditorBrowsable(EditorBrowsableState.Never)]
protected SasQueryParameters(string version, AccountSasServices? services, AccountSasResourceTypes? resourceTypes, SasProtocol protocol, DateTimeOffset startsOn, DateTimeOffset expiresOn, SasIPRange ipRange, string identifier, string resource, string permissions, string signature, string cacheControl = null, string contentDisposition = null, string contentEncoding = null, string contentLanguage = null, string contentType = null)
{
_version = version;
_services = (services, services?.ToPermissionsString());
_resourceTypes = (resourceTypes, resourceTypes?.ToPermissionsString());
_protocol = (protocol, protocol.ToProtocolString());
_startTime = startsOn;
_startTimeString = startsOn.ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture);
_expiryTime = expiresOn;
_expiryTimeString = expiresOn.ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture);
_ipRange = ipRange;
_identifier = identifier;
_resource = resource;
_permissions = permissions;
_signature = signature;
_cacheControl = cacheControl;
_contentDisposition = contentDisposition;
_contentEncoding = contentEncoding;
_contentLanguage = contentLanguage;
_contentType = contentType;
_preauthorizedAgentObjectId = null;
_agentObjectId = null;
_correlationId = null;
_directoryDepth = null;
}
[EditorBrowsable(EditorBrowsableState.Never)]
protected SasQueryParameters(string version, AccountSasServices? services, AccountSasResourceTypes? resourceTypes, SasProtocol protocol, DateTimeOffset startsOn, DateTimeOffset expiresOn, SasIPRange ipRange, string identifier, string resource, string permissions, string signature, string cacheControl = null, string contentDisposition = null, string contentEncoding = null, string contentLanguage = null, string contentType = null, string authorizedAadObjectId = null, string unauthorizedAadObjectId = null, string correlationId = null, int? directoryDepth = default(int?))
{
_version = version;
_services = (services, services?.ToPermissionsString());
_resourceTypes = (resourceTypes, resourceTypes?.ToPermissionsString());
_protocol = (protocol, protocol.ToProtocolString());
_startTime = startsOn;
_startTimeString = startsOn.ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture);
_expiryTime = expiresOn;
_expiryTimeString = expiresOn.ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture);
_ipRange = ipRange;
_identifier = identifier;
_resource = resource;
_permissions = permissions;
_signature = signature;
_cacheControl = cacheControl;
_contentDisposition = contentDisposition;
_contentEncoding = contentEncoding;
_contentLanguage = contentLanguage;
_contentType = contentType;
_preauthorizedAgentObjectId = authorizedAadObjectId;
_agentObjectId = unauthorizedAadObjectId;
_correlationId = correlationId;
_directoryDepth = directoryDepth;
}
protected static SasQueryParameters Create(IDictionary<string, string> values)
{
return new SasQueryParameters(values);
}
protected static SasQueryParameters Create(string version, AccountSasServices? services, AccountSasResourceTypes? resourceTypes, SasProtocol protocol, DateTimeOffset startsOn, DateTimeOffset expiresOn, SasIPRange ipRange, string identifier, string resource, string permissions, string signature, string cacheControl = null, string contentDisposition = null, string contentEncoding = null, string contentLanguage = null, string contentType = null, string authorizedAadObjectId = null, string unauthorizedAadObjectId = null, string correlationId = null, int? directoryDepth = default(int?), string encryptionScope = null)
{
return new SasQueryParameters(version, services, resourceTypes, protocol, startsOn, expiresOn, ipRange, identifier, resource, permissions, signature, cacheControl, contentDisposition, contentEncoding, contentLanguage, contentType, authorizedAadObjectId, unauthorizedAadObjectId, correlationId, directoryDepth, encryptionScope);
}
[EditorBrowsable(EditorBrowsableState.Never)]
protected static SasQueryParameters Create(string version, AccountSasServices? services, AccountSasResourceTypes? resourceTypes, SasProtocol protocol, DateTimeOffset startsOn, DateTimeOffset expiresOn, SasIPRange ipRange, string identifier, string resource, string permissions, string signature, string cacheControl = null, string contentDisposition = null, string contentEncoding = null, string contentLanguage = null, string contentType = null)
{
return new SasQueryParameters(version, services, resourceTypes, protocol, startsOn, expiresOn, ipRange, identifier, resource, permissions, signature, cacheControl, contentDisposition, contentEncoding, contentLanguage, contentType);
}
[EditorBrowsable(EditorBrowsableState.Never)]
protected static SasQueryParameters Create(string version, AccountSasServices? services, AccountSasResourceTypes? resourceTypes, SasProtocol protocol, DateTimeOffset startsOn, DateTimeOffset expiresOn, SasIPRange ipRange, string identifier, string resource, string permissions, string signature, string cacheControl = null, string contentDisposition = null, string contentEncoding = null, string contentLanguage = null, string contentType = null, string authorizedAadObjectId = null, string unauthorizedAadObjectId = null, string correlationId = null, int? directoryDepth = default(int?))
{
return new SasQueryParameters(version, services, resourceTypes, protocol, startsOn, expiresOn, ipRange, identifier, resource, permissions, signature, cacheControl, contentDisposition, contentEncoding, contentLanguage, contentType, authorizedAadObjectId, unauthorizedAadObjectId, correlationId, directoryDepth);
}
public override string ToString()
{
StringBuilder stringBuilder = new StringBuilder();
AppendProperties(stringBuilder);
return stringBuilder.ToString();
}
protected internal void AppendProperties(StringBuilder stringBuilder)
{
if (!string.IsNullOrWhiteSpace(Version))
stringBuilder.AppendQueryParameter("sv", Version);
if (Services.HasValue)
stringBuilder.AppendQueryParameter("ss", _services.Raw);
if (ResourceTypes.HasValue)
stringBuilder.AppendQueryParameter("srt", _resourceTypes.Raw);
if (!string.IsNullOrEmpty(ProtocolAsString))
stringBuilder.AppendQueryParameter("spr", ProtocolAsString);
if (StartsOn != DateTimeOffset.MinValue)
stringBuilder.AppendQueryParameter("st", WebUtility.UrlEncode(StartsOnString));
if (ExpiresOn != DateTimeOffset.MinValue)
stringBuilder.AppendQueryParameter("se", WebUtility.UrlEncode(ExpiresOnString));
string text = IPRange.ToString();
if (text.Length > 0)
stringBuilder.AppendQueryParameter("sip", text);
if (!string.IsNullOrWhiteSpace(Identifier))
stringBuilder.AppendQueryParameter("si", Identifier);
if (!string.IsNullOrWhiteSpace(Resource))
stringBuilder.AppendQueryParameter("sr", Resource);
if (!string.IsNullOrWhiteSpace(Permissions))
stringBuilder.AppendQueryParameter("sp", Permissions);
if (!string.IsNullOrWhiteSpace(CacheControl))
stringBuilder.AppendQueryParameter("rscc", WebUtility.UrlEncode(CacheControl));
if (!string.IsNullOrWhiteSpace(ContentDisposition))
stringBuilder.AppendQueryParameter("rscd", WebUtility.UrlEncode(ContentDisposition));
if (!string.IsNullOrWhiteSpace(ContentEncoding))
stringBuilder.AppendQueryParameter("rsce", WebUtility.UrlEncode(ContentEncoding));
if (!string.IsNullOrWhiteSpace(ContentLanguage))
stringBuilder.AppendQueryParameter("rscl", WebUtility.UrlEncode(ContentLanguage));
if (!string.IsNullOrWhiteSpace(ContentType))
stringBuilder.AppendQueryParameter("rsct", WebUtility.UrlEncode(ContentType));
if (!string.IsNullOrWhiteSpace(PreauthorizedAgentObjectId))
stringBuilder.AppendQueryParameter("saoid", WebUtility.UrlEncode(PreauthorizedAgentObjectId));
if (!string.IsNullOrWhiteSpace(AgentObjectId))
stringBuilder.AppendQueryParameter("suoid", WebUtility.UrlEncode(AgentObjectId));
if (!string.IsNullOrWhiteSpace(CorrelationId))
stringBuilder.AppendQueryParameter("scid", WebUtility.UrlEncode(CorrelationId));
int? directoryDepth = DirectoryDepth;
if (directoryDepth.HasValue) {
directoryDepth = DirectoryDepth;
stringBuilder.AppendQueryParameter("sdd", WebUtility.UrlEncode(directoryDepth.ToString()));
}
if (!string.IsNullOrWhiteSpace(EncryptionScope))
stringBuilder.AppendQueryParameter("ses", WebUtility.UrlEncode(EncryptionScope));
if (!string.IsNullOrWhiteSpace(Signature))
stringBuilder.AppendQueryParameter("sig", WebUtility.UrlEncode(Signature));
}
private static DateTimeOffset ParseSasTime(string dateTimeString)
{
if (string.IsNullOrEmpty(dateTimeString))
return DateTimeOffset.MinValue;
return DateTimeOffset.ParseExact(dateTimeString, s_sasTimeFormats, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
}
}
}