Пример #1
0
        /// <summary>
        /// Use an account's <see cref="StorageSharedKeyCredential"/> to sign this
        /// shared access signature values to produce the proper SAS query
        /// parameters for authenticating requests.
        /// </summary>
        /// <param name="sharedKeyCredential">
        /// The storage account's <see cref="StorageSharedKeyCredential"/>.
        /// </param>
        /// <returns>
        /// The <see cref="SasQueryParameters"/> used for authenticating requests.
        /// </returns>
        public SasQueryParameters ToSasQueryParameters(StorageSharedKeyCredential sharedKeyCredential)
        {
            sharedKeyCredential = sharedKeyCredential ?? throw Errors.ArgumentNull(nameof(sharedKeyCredential));

            string resource;

            if (String.IsNullOrEmpty(this.FilePath))
            {
                // Make sure the permission characters are in the correct order
                this.Permissions = ShareSasPermissions.Parse(this.Permissions).ToString();
                resource         = Constants.Sas.Resource.Share;
            }
            else
            {
                // Make sure the permission characters are in the correct order
                this.Permissions = FileSasPermissions.Parse(this.Permissions).ToString();
                resource         = Constants.Sas.Resource.File;
            }

            if (String.IsNullOrEmpty(this.Version))
            {
                this.Version = SasQueryParameters.DefaultSasVersion;
            }

            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.ShareName ?? String.Empty, this.FilePath ?? String.Empty),
                                           this.Identifier,
                                           this.IPRange.ToString(),
                                           this.Protocol.ToString(),
                                           this.Version,
                                           this.CacheControl,
                                           this.ContentDisposition,
                                           this.ContentEncoding,
                                           this.ContentLanguage,
                                           this.ContentType);

            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: resource,
                permissions: this.Permissions,
                signature: signature);

            return(p);
        }
Пример #2
0
        /// <summary>
        /// Use an account's <see cref="StorageSharedKeyCredential"/> to sign this
        /// shared access signature values to produce the proper SAS query
        /// parameters for authenticating requests.
        /// </summary>
        /// <param name="sharedKeyCredential">
        /// The storage account's <see cref="StorageSharedKeyCredential"/>.
        /// </param>
        /// <returns>
        /// The <see cref="SasQueryParameters"/> used for authenticating requests.
        /// </returns>
        public SasQueryParameters ToSasQueryParameters(StorageSharedKeyCredential sharedKeyCredential)
        {
            sharedKeyCredential = sharedKeyCredential ?? throw Errors.ArgumentNull(nameof(sharedKeyCredential));

            string resource;

            if (string.IsNullOrEmpty(FilePath))
            {
                // Make sure the permission characters are in the correct order
                Permissions = ShareSasPermissions.Parse(Permissions).ToString();
                resource    = Constants.Sas.Resource.Share;
            }
            else
            {
                // Make sure the permission characters are in the correct order
                Permissions = FileSasPermissions.Parse(Permissions).ToString();
                resource    = Constants.Sas.Resource.File;
            }

            if (string.IsNullOrEmpty(Version))
            {
                Version = SasQueryParameters.DefaultSasVersion;
            }

            var startTime  = SasQueryParameters.FormatTimesForSasSigning(StartsOn);
            var expiryTime = SasQueryParameters.FormatTimesForSasSigning(ExpiresOn);

            // String to sign: http://msdn.microsoft.com/en-us/library/azure/dn140255.aspx
            var stringToSign = string.Join("\n",
                                           Permissions,
                                           startTime,
                                           expiryTime,
                                           GetCanonicalName(sharedKeyCredential.AccountName, ShareName ?? string.Empty, FilePath ?? string.Empty),
                                           Identifier,
                                           IPRange.ToString(),
                                           Protocol.ToProtocolString(),
                                           Version,
                                           CacheControl,
                                           ContentDisposition,
                                           ContentEncoding,
                                           ContentLanguage,
                                           ContentType);

            var signature = sharedKeyCredential.ComputeHMACSHA256(stringToSign);

            var p = new SasQueryParameters(
                version: Version,
                services: default,
Пример #3
0
 /// <summary>
 /// Sets the permissions for a file SAS.
 /// </summary>
 /// <param name="permissions">
 /// <see cref="FileSasPermissions"/> containing the allowed permissions.
 /// </param>
 public void SetPermissions(FileSasPermissions permissions)
 {
     Permissions = permissions.ToPermissionsString();
 }