/// <summary>
        /// Create a permissions string to provide
        /// <see cref="QueueSasBuilder.Permissions"/>.
        /// </summary>
        /// <returns>A permissions string.</returns>
        internal static string ToPermissionsString(this QueueAccountSasPermissions permissions)
        {
            var sb = new StringBuilder();

            if ((permissions & QueueAccountSasPermissions.Read) == QueueAccountSasPermissions.Read)
            {
                sb.Append(Constants.Sas.Permissions.Read);
            }
            if ((permissions & QueueAccountSasPermissions.Write) == QueueAccountSasPermissions.Write)
            {
                sb.Append(Constants.Sas.Permissions.Write);
            }
            if ((permissions & QueueAccountSasPermissions.Delete) == QueueAccountSasPermissions.Delete)
            {
                sb.Append(Constants.Sas.Permissions.Delete);
            }
            if ((permissions & QueueAccountSasPermissions.List) == QueueAccountSasPermissions.List)
            {
                sb.Append(Constants.Sas.Permissions.List);
            }
            if ((permissions & QueueAccountSasPermissions.Add) == QueueAccountSasPermissions.Add)
            {
                sb.Append(Constants.Sas.Permissions.Add);
            }
            if ((permissions & QueueAccountSasPermissions.Update) == QueueAccountSasPermissions.Update)
            {
                sb.Append(Constants.Sas.Permissions.Update);
            }
            if ((permissions & QueueAccountSasPermissions.Process) == QueueAccountSasPermissions.Process)
            {
                sb.Append(Constants.Sas.Permissions.Process);
            }
            return(sb.ToString());
        }
Exemplo n.º 2
0
        /// <summary>
        /// ToSasQueryParameters uses an account's shared key credential to sign this signature values to produce
        /// the proper SAS query parameters.
        /// </summary>
        /// <param name="sharedKeyCredential">
        /// <see cref="SharedKeyCredentials"/>
        /// </param>
        /// <returns>
        /// <see cref="SasQueryParameters"/>
        /// </returns>
        public SasQueryParameters ToSasQueryParameters(SharedKeyCredentials sharedKeyCredential)
        {
            sharedKeyCredential = sharedKeyCredential ?? throw Errors.ArgumentNull(nameof(sharedKeyCredential));

            this.Permissions = QueueAccountSasPermissions.Parse(this.Permissions).ToString();
            if (String.IsNullOrEmpty(this.Version))
            {
                this.Version = SasQueryParameters.SasVersion;
            }

            var startTime  = SasQueryParameters.FormatTimesForSasSigning(this.StartTime);
            var expiryTime = SasQueryParameters.FormatTimesForSasSigning(this.ExpiryTime);

            // String to sign: http://msdn.microsoft.com/en-us/library/azure/dn140255.aspx
            var stringToSign = String.Join("\n",
                                           this.Permissions,
                                           startTime,
                                           expiryTime,
                                           GetCanonicalName(sharedKeyCredential.AccountName, this.QueueName ?? String.Empty),
                                           this.Identifier,
                                           this.IPRange.ToString(),
                                           this.Protocol.ToString(),
                                           this.Version);
            var signature = sharedKeyCredential.ComputeHMACSHA256(stringToSign);
            var p         = new SasQueryParameters(
                version: this.Version,
                services: null,
                resourceTypes: null,
                protocol: this.Protocol,
                startTime: this.StartTime,
                expiryTime: this.ExpiryTime,
                ipRange: this.IPRange,
                identifier: this.Identifier,
                resource: null,
                permissions: this.Permissions,
                signature: signature);

            return(p);
        }