/// <summary> /// Use an account's <see cref="SharedKeyCredentials"/> 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="SharedKeyCredentials"/>. /// </param> /// <returns> /// The <see cref="SasQueryParameters"/> used for authenticating requests. /// </returns> public SasQueryParameters ToSasQueryParameters(SharedKeyCredentials 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.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.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); }
/// <summary> /// Use an account's <see cref="SharedKeyCredentials"/> 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="SharedKeyCredentials"/>. /// </param> /// <returns> /// The <see cref="SasQueryParameters"/> used for authenticating requests. /// </returns> public SasQueryParameters ToSasQueryParameters(SharedKeyCredentials sharedKeyCredential) { sharedKeyCredential = sharedKeyCredential ?? throw new ArgumentNullException(nameof(sharedKeyCredential)); this.EnsureState(); var startTime = SasQueryParameters.FormatTimesForSasSigning(this.StartTime); var expiryTime = SasQueryParameters.FormatTimesForSasSigning(this.ExpiryTime); // See http://msdn.microsoft.com/en-us/library/azure/dn140255.aspx var stringToSign = String.Join("\n", this.Permissions, startTime, expiryTime, GetCanonicalName(sharedKeyCredential.AccountName, this.ContainerName ?? String.Empty, this.BlobName ?? String.Empty), this.Identifier, this.IPRange.ToString(), this.Protocol.ToString(), this.Version, this.Resource, this.Snapshot, 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: this.Resource, permissions: this.Permissions, signature: signature); return(p); }
/// <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); }
// NewSASQueryParameters uses an account's shared key credential to sign this signature values to produce // the proper SAS query parameters. public SasQueryParameters ToSasQueryParameters(SharedKeyCredentials sharedKeyCredential) { // https://docs.microsoft.com/en-us/rest/api/storageservices/Constructing-an-Account-SAS sharedKeyCredential = sharedKeyCredential ?? throw Errors.ArgumentNull(nameof(sharedKeyCredential)); if (this.ExpiryTime == default || String.IsNullOrEmpty(this.Permissions) || String.IsNullOrEmpty(this.ResourceTypes) || String.IsNullOrEmpty(this.Services)) { throw Errors.AccountSasMissingData(); } if (String.IsNullOrEmpty(this.Version)) { this.Version = SasQueryParameters.SasVersion; } // Make sure the permission characters are in the correct order this.Permissions = AccountSasPermissions.Parse(this.Permissions).ToString(); 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", sharedKeyCredential.AccountName, this.Permissions, this.Services, this.ResourceTypes, startTime, expiryTime, this.IPRange.ToString(), this.Protocol.ToString(), this.Version, ""); // That's right, the account SAS requires a terminating extra newline var signature = sharedKeyCredential.ComputeHMACSHA256(stringToSign); var p = new SasQueryParameters(this.Version, this.Services, this.ResourceTypes, this.Protocol, this.StartTime, this.ExpiryTime, this.IPRange, null, null, this.Permissions, signature); return(p); }