Exemplo n.º 1
0
        /// <summary>
        /// Use an account's <see cref="TableSharedKeyCredential"/> 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="TableSharedKeyCredential"/>.
        /// </param>
        /// <returns>
        /// An instance of <see cref="TableSasQueryParameters"/>.
        /// </returns>
        public TableSasQueryParameters ToSasQueryParameters(TableSharedKeyCredential sharedKeyCredential)
        {
            sharedKeyCredential = sharedKeyCredential ?? throw Errors.ArgumentNull(nameof(sharedKeyCredential));

            EnsureState();

            var startTime  = TableSasExtensions.FormatTimesForSasSigning(StartsOn);
            var expiryTime = TableSasExtensions.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, TableName),
                                           Identifier,
                                           IPRange.ToString(),
                                           TableSasExtensions.ToProtocolString(Protocol),
                                           Version,
                                           PartitionKeyStart,
                                           RowKeyStart,
                                           PartitionKeyEnd,
                                           RowKeyEnd);
            var signature = TableSharedKeyCredential.ComputeSasSignature(sharedKeyCredential, stringToSign);
            var p         = new TableSasQueryParameters(
                version: Version,
                resourceTypes: default,
        /// <summary>
        /// Use an account's <see cref="TableSharedKeyCredential"/> 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="TableSharedKeyCredential"/>.
        /// </param>
        /// <returns>
        /// An instance of <see cref="TableAccountSasQueryParameters"/>.
        /// </returns>
        public TableAccountSasQueryParameters ToSasQueryParameters(TableSharedKeyCredential sharedKeyCredential)
        {
            sharedKeyCredential = sharedKeyCredential ?? throw Errors.ArgumentNull(nameof(sharedKeyCredential));

            EnsureState();

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

            // String to sign: https://docs.microsoft.com/en-us/rest/api/storageservices/create-account-sas#constructing-the-signature-string
            var stringToSign = string.Join("\n",
                                           sharedKeyCredential.AccountName,
                                           Permissions,
                                           TableConstants.Sas.TableAccountServices.Table,
                                           ResourceTypes.ToPermissionsString(),
                                           startTime,
                                           expiryTime,
                                           IPRange.ToString(),
                                           Protocol.ToProtocolString(),
                                           Version,
                                           "");
            var signature = TableSharedKeyCredential.ComputeSasSignature(sharedKeyCredential, stringToSign);
            var p         = new TableAccountSasQueryParameters(
                version: Version,
                resourceTypes: ResourceTypes,
                protocol: Protocol,
                startsOn: StartsOn,
                expiresOn: ExpiresOn,
                ipRange: IPRange,
                identifier: Identifier,
                resource: null,
                permissions: Permissions,
                signature: signature);

            return(p);
        }
        /// <summary>
        /// Creates a new instance of the <see cref="TableAccountSasQueryParameters"/>
        /// type based on the supplied query parameters <paramref name="values"/>.
        /// All SAS-related query parameters will be removed from
        /// <paramref name="values"/>.
        /// </summary>
        /// <param name="values">URI query parameters</param>
        internal TableAccountSasQueryParameters(
            IDictionary <string, string> values)
        {
            // make copy, otherwise we'll get an exception when we remove
            IEnumerable <KeyValuePair <string, string> > kvps = values.ToArray();

            foreach (KeyValuePair <string, string> kv in kvps)
            {
                // these are already decoded
                var isSasKey = true;
                switch (kv.Key.ToUpperInvariant())
                {
                case TableConstants.Sas.Parameters.VersionUpper:
                    _version = kv.Value;
                    break;

                case TableConstants.Sas.Parameters.ResourceTypesUpper:
                    _resourceTypes = TableSasExtensions.ParseResourceTypes(kv.Value);
                    break;

                case TableConstants.Sas.Parameters.ProtocolUpper:
                    _protocol = TableSasExtensions.ParseProtocol(kv.Value);
                    break;

                case TableConstants.Sas.Parameters.StartTimeUpper:
                    _startTime = DateTimeOffset.ParseExact(kv.Value, TableConstants.Sas.SasTimeFormat, CultureInfo.InvariantCulture);
                    break;

                case TableConstants.Sas.Parameters.ExpiryTimeUpper:
                    _expiryTime = DateTimeOffset.ParseExact(kv.Value, TableConstants.Sas.SasTimeFormat, CultureInfo.InvariantCulture);
                    break;

                case TableConstants.Sas.Parameters.IPRangeUpper:
                    _ipRange = TableSasIPRange.Parse(kv.Value);
                    break;

                case TableConstants.Sas.Parameters.IdentifierUpper:
                    _identifier = kv.Value;
                    break;

                case TableConstants.Sas.Parameters.ResourceUpper:
                    _resource = kv.Value;
                    break;

                case TableConstants.Sas.Parameters.PermissionsUpper:
                    _permissions = kv.Value;
                    break;

                case TableConstants.Sas.Parameters.SignatureUpper:
                    _signature = kv.Value;
                    break;

                // We didn't recognize the query parameter
                default:
                    isSasKey = false;
                    break;
                }

                // Remove the query parameter if it's part of the SAS
                if (isSasKey)
                {
                    values.Remove(kv.Key);
                }
            }
        }
        /// <summary>
        /// Creates a new instance of the <see cref="TableAccountSasQueryParameters"/>
        /// type based on the supplied query parameters <paramref name="values"/>.
        /// All SAS-related query parameters will be removed from
        /// <paramref name="values"/>.
        /// </summary>
        /// <param name="values">URI query parameters</param>
        internal TableAccountSasQueryParameters(
            IDictionary <string, string> values)
        {
            foreach (var key in values.Keys.ToList())
            {
                // these are already decoded
                var isSasKey = true;
                switch (key.ToUpperInvariant())
                {
                case TableConstants.Sas.Parameters.VersionUpper:
                    _version = values[key];
                    break;

                case TableConstants.Sas.Parameters.ResourceTypesUpper:
                    _resourceTypes = TableSasExtensions.ParseResourceTypes(values[key]);
                    break;

                case TableConstants.Sas.Parameters.ProtocolUpper:
                    _protocol = TableSasExtensions.ParseProtocol(values[key]);
                    break;

                case TableConstants.Sas.Parameters.StartTimeUpper:
                    _startTime       = ParseSasTime(values[key]);
                    _startTimeString = values[key];
                    break;

                case TableConstants.Sas.Parameters.ExpiryTimeUpper:
                    _expiryTime       = ParseSasTime(values[key]);
                    _expiryTimeString = values[key];
                    break;

                case TableConstants.Sas.Parameters.IPRangeUpper:
                    _ipRange = TableSasIPRange.Parse(values[key]);
                    break;

                case TableConstants.Sas.Parameters.IdentifierUpper:
                    _identifier = values[key];
                    break;

                case TableConstants.Sas.Parameters.ResourceUpper:
                    _resource = values[key];
                    break;

                case TableConstants.Sas.Parameters.PermissionsUpper:
                    _permissions = values[key];
                    break;

                case TableConstants.Sas.Parameters.SignatureUpper:
                    _signature = values[key];
                    break;

                // We didn't recognize the query parameter
                default:
                    isSasKey = false;
                    break;
                }

                // Set the value to null if it's part of the SAS
                if (isSasKey)
                {
                    values[key] = null;
                }
            }
        }