public string CanonicalizeHttpRequest(HttpRequestMessage request, string accountName)
        {
            // Add the method (GET, POST, PUT, or HEAD).
            CanonicalizedString canonicalizedString = new CanonicalizedString(request.Method.Method, ExpectedCanonicalizedStringLength);

            // Add the Content-* HTTP headers. Empty values are allowed.
            if (request.Content != null)
            {
                canonicalizedString.AppendCanonicalizedElement((request.Content.Headers.ContentMD5 == null) ? null :
                    Convert.ToBase64String(request.Content.Headers.ContentMD5));
                canonicalizedString.AppendCanonicalizedElement((request.Content.Headers.ContentType == null) ? null :
                    request.Content.Headers.ContentType.ToString());
            }
            else
            {
                canonicalizedString.AppendCanonicalizedElement(null);
                canonicalizedString.AppendCanonicalizedElement(null);
            }

            // Add the Date HTTP header (or the x-ms-date header if it is being used)
            AuthenticationUtility.AppendCanonicalizedDateHeader(canonicalizedString, request, true);

            // Add the canonicalized URI element
            string resourceString = AuthenticationUtility.GetCanonicalizedResourceString(request.RequestUri, accountName, true);
            canonicalizedString.AppendCanonicalizedElement(resourceString);

            return canonicalizedString.ToString();
        }
Example #2
0
        /// <summary>
        /// Converts the specified HTTP request data into a standard form appropriate for signing.
        /// </summary>
        /// <param name="request">The HTTP request that needs to be signed.</param>
        /// <param name="accountName">The name of the storage account that the HTTP request will access.</param>
        /// <returns>The canonicalized string containing the HTTP request data in a standard form appropriate for signing.</returns>
        /// <seealso href="http://msdn.microsoft.com/en-us/library/Azure/dd179428.aspx">Authentication for the Microsoft Azure Storage Services</seealso>
        public string CanonicalizeHttpRequest(HttpRequestMessage request, string accountName)
        {
            CommonUtility.AssertNotNull("request", request);

            // Add the method (GET, POST, PUT, or HEAD).
            CanonicalizedString canonicalizedString = new CanonicalizedString(request.Method.Method, ExpectedCanonicalizedStringLength);

            // Add the Content-* HTTP headers. Empty values are allowed.
            if (request.Content != null)
            {
                canonicalizedString.AppendCanonicalizedElement((request.Content.Headers.ContentMD5 == null) ? null :
                                                               Convert.ToBase64String(request.Content.Headers.ContentMD5));
                //canonicalizedString.AppendCanonicalizedElement(HttpRequestParsers.GetContentCRC64(request)); // should be covered by custom headers
                canonicalizedString.AppendCanonicalizedElement((request.Content.Headers.ContentType == null) ? null :
                                                               request.Content.Headers.ContentType.ToString());
            }
            else
            {
                canonicalizedString.AppendCanonicalizedElement(null);
                canonicalizedString.AppendCanonicalizedElement(null);
            }

            // Add the Date HTTP header (only if the x-ms-date header is not being used)
            AuthenticationUtility.AppendCanonicalizedDateHeader(canonicalizedString, request);

            // Add any custom headers
            AuthenticationUtility.AppendCanonicalizedCustomHeaders(canonicalizedString, request);

            // Add the canonicalized URI element
            string resourceString = AuthenticationUtility.GetCanonicalizedResourceString(request.RequestUri, accountName, true);

            canonicalizedString.AppendCanonicalizedElement(resourceString);

            return(canonicalizedString.ToString());
        }
        /// <summary>
        /// Converts the specified HTTP request data into a standard form appropriate for signing.
        /// </summary>
        /// <param name="request">The HTTP request that needs to be signed.</param>
        /// <param name="accountName">The name of the storage account that the HTTP request will access.</param>
        /// <returns>The canonicalized string containing the HTTP request data in a standard form appropriate for signing.</returns>
        /// <seealso href="http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx">Authentication for the Windows Azure Storage Services</seealso>
        public string CanonicalizeHttpRequest(HttpWebRequest request, string accountName)
        {
            CommonUtility.AssertNotNull("request", request);

            // Add the method (GET, POST, PUT, or HEAD).
            CanonicalizedString canonicalizedString = new CanonicalizedString(request.Method);

            // Add the Content-* HTTP headers. Empty values are allowed.
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.ContentEncoding]);
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.ContentLanguage]);
            AuthenticationUtility.AppendCanonicalizedContentLengthHeader(canonicalizedString, request);
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.ContentMd5]);
            canonicalizedString.AppendCanonicalizedElement(request.ContentType);

            // Add the Date HTTP header (only if the x-ms-date header is not being used)
            AuthenticationUtility.AppendCanonicalizedDateHeader(canonicalizedString, request);

            // Add If-* headers and Range header
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.IfModifiedSince]);
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.IfMatch]);
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.IfNoneMatch]);
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.IfUnmodifiedSince]);
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.Range]);

            // Add any custom headers
            AuthenticationUtility.AppendCanonicalizedCustomHeaders(canonicalizedString, request);

            // Add the canonicalized URI element
            string resourceString = AuthenticationUtility.GetCanonicalizedResourceString(request.RequestUri, accountName);
            canonicalizedString.AppendCanonicalizedElement(resourceString);

            return canonicalizedString.ToString();
        }
Example #4
0
        public string CanonicalizeHttpRequest(HttpRequestMessage request, string accountName)
        {
            // Add the method (GET, POST, PUT, or HEAD).
            CanonicalizedString canonicalizedString = new CanonicalizedString(request.Method.Method, ExpectedCanonicalizedStringLength);

            // Add the Content-* HTTP headers. Empty values are allowed.
            if (request.Content != null)
            {
                canonicalizedString.AppendCanonicalizedElement((request.Content.Headers.ContentMD5 == null) ? null :
                                                               Convert.ToBase64String(request.Content.Headers.ContentMD5));
                canonicalizedString.AppendCanonicalizedElement((request.Content.Headers.ContentType == null) ? null :
                                                               request.Content.Headers.ContentType.ToString());
            }
            else
            {
                canonicalizedString.AppendCanonicalizedElement(null);
                canonicalizedString.AppendCanonicalizedElement(null);
            }

            // Add the Date HTTP header (or the x-ms-date header if it is being used)
            AuthenticationUtility.AppendCanonicalizedDateHeader(canonicalizedString, request, true);

            // Add the canonicalized URI element
            string resourceString = AuthenticationUtility.GetCanonicalizedResourceString(request.RequestUri, accountName, true);

            canonicalizedString.AppendCanonicalizedElement(resourceString);

            return(canonicalizedString.ToString());
        }
Example #5
0
 static string GetCanonicalizedResourceVersion2(Uri address, string accountName)
 {
     StringBuilder builder = new StringBuilder("/");
     builder.Append(accountName);
     builder.Append(address.AbsolutePath);
     CanonicalizedString str = new CanonicalizedString(builder.ToString());
     NameValueCollection values = HttpUtility.ParseQueryString(address.Query);
     NameValueCollection values2 = new NameValueCollection();
     foreach (string str2 in values.Keys)
     {
         ArrayList list = new ArrayList(values.GetValues(str2));
         list.Sort();
         StringBuilder builder2 = new StringBuilder();
         foreach (object obj2 in list)
         {
             if (builder2.Length > 0)
             {
                 builder2.Append(",");
             }
             builder2.Append(obj2.ToString());
         }
         values2.Add((str2 == null) ? str2 : str2.ToLowerInvariant(), builder2.ToString());
     }
     ArrayList list2 = new ArrayList(values2.AllKeys);
     list2.Sort();
     foreach (string str3 in list2)
     {
         StringBuilder builder3 = new StringBuilder(string.Empty);
         builder3.Append(str3);
         builder3.Append(":");
         builder3.Append(values2[str3]);
         str.AppendCanonicalizedElement(builder3.ToString());
     }
     return str.Value;
 }
Example #6
0
        /// <summary>
        /// Gets the canonicalized resource string for a Blob or Queue service request under the Shared Key authentication scheme.
        /// </summary>
        /// <param name="address">The resource URI.</param>
        /// <param name="accountName">The name of the storage account.</param>
        /// <returns>The canonicalized resource string.</returns>
        internal static string GetCanonicalizedResourceForSharedKey(Uri address, string accountName)
        {
            // Resource path
            // Note that AbsolutePath starts with a '/'.
            string resourcePath = string.Concat(
                "/",
                accountName,
                address.AbsolutePath);
            CanonicalizedString canonicalizedResource = new CanonicalizedString(resourcePath);

            // Query parameters
            Dictionary <string, string> queryVariables = HttpUtility.ParseQueryString(address.Query);
            List <string> queryVariableKeys            = new List <string>(queryVariables.Keys);

            queryVariableKeys.Sort(StringComparer.OrdinalIgnoreCase);

            foreach (string key in queryVariableKeys)
            {
                string queryParamString = string.Concat(
                    key.ToLowerInvariant(),
                    ":",
                    queryVariables[key]);

                canonicalizedResource.AppendCanonicalizedElement(queryParamString);
            }

            return(canonicalizedResource.Value);
        }
Example #7
0
        public static void AppendCanonicalizedCustomHeaders(CanonicalizedString canonicalizedString, HttpWebRequest request)
        {
            List <string> headerNames = new List <string>(request.Headers.AllKeys.Length);

            foreach (string headerName in request.Headers.AllKeys)
            {
                if (headerName.StartsWith(Constants.HeaderConstants.PrefixForStorageHeader, StringComparison.OrdinalIgnoreCase))
                {
                    headerNames.Add(headerName.ToLowerInvariant());
                }
            }

            CultureInfo    sortingCulture  = new CultureInfo("en-US");
            StringComparer sortingComparer = StringComparer.Create(sortingCulture, false);

            headerNames.Sort(sortingComparer);

            StringBuilder canonicalizedElement = new StringBuilder(ExpectedHeaderNameAndValueLength);

            foreach (string headerName in headerNames)
            {
                string value = request.Headers[headerName];
                if (!string.IsNullOrEmpty(value))
                {
                    canonicalizedElement.Length = 0;
                    canonicalizedElement.Append(headerName);
                    canonicalizedElement.Append(HeaderNameValueSeparator);
                    canonicalizedElement.Append(value.TrimStart().Replace("\r\n", string.Empty));

                    canonicalizedString.AppendCanonicalizedElement(canonicalizedElement.ToString());
                }
            }
        }
        /// <summary>
        /// Converts the specified HTTP request data into a standard form appropriate for signing.
        /// </summary>
        /// <param name="request">The HTTP request that needs to be signed.</param>
        /// <param name="accountName">The name of the storage account that the HTTP request will access.</param>
        /// <returns>The canonicalized string containing the HTTP request data in a standard form appropriate for signing.</returns>
        /// <seealso href="http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx">Authentication for the Windows Azure Storage Services</seealso>
        public string CanonicalizeHttpRequest(HttpWebRequest request, string accountName)
        {
            CommonUtility.AssertNotNull("request", request);

            // Add the method (GET, POST, PUT, or HEAD).
            CanonicalizedString canonicalizedString = new CanonicalizedString(request.Method);

            // Add the Content-* HTTP headers. Empty values are allowed.
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.ContentEncoding]);
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.ContentLanguage]);
            AuthenticationUtility.AppendCanonicalizedContentLengthHeader(canonicalizedString, request);
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.ContentMd5]);
            canonicalizedString.AppendCanonicalizedElement(request.ContentType);

            // Add the Date HTTP header (only if the x-ms-date header is not being used)
            AuthenticationUtility.AppendCanonicalizedDateHeader(canonicalizedString, request);

            // Add If-* headers and Range header
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.IfModifiedSince]);
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.IfMatch]);
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.IfNoneMatch]);
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.IfUnmodifiedSince]);
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.Range]);

            // Add any custom headers
            AuthenticationUtility.AppendCanonicalizedCustomHeaders(canonicalizedString, request);

            // Add the canonicalized URI element
            string resourceString = AuthenticationUtility.GetCanonicalizedResourceString(request.RequestUri, accountName);

            canonicalizedString.AppendCanonicalizedElement(resourceString);

            return(canonicalizedString.ToString());
        }
        /// <summary>
        /// Gets the canonicalized resource string for a Blob or Queue service request under the Shared Key authentication scheme.
        /// </summary>
        /// <param name="address">The resource URI.</param>
        /// <param name="accountName">The name of the storage account.</param>
        /// <returns>The canonicalized resource string.</returns>
        internal static string GetCanonicalizedResourceForSharedKey(Uri address, string accountName)
        {
            // Resource path
            // Note that AbsolutePath starts with a '/'.
            string resourcePath = string.Concat(
                "/",
                accountName,
                address.AbsolutePath);
            CanonicalizedString canonicalizedResource = new CanonicalizedString(resourcePath);

            // Query parameters
            Dictionary<string, string> queryVariables = HttpUtility.ParseQueryString(address.Query);
            List<string> queryVariableKeys = new List<string>(queryVariables.Keys);
            queryVariableKeys.Sort(StringComparer.OrdinalIgnoreCase);

            foreach (string key in queryVariableKeys)
            {
                string queryParamString = string.Concat(
                    key.ToLowerInvariant(),
                    ":",
                    queryVariables[key]);

                canonicalizedResource.AppendCanonicalizedElement(queryParamString);
            }

            return canonicalizedResource.Value;
        }
Example #10
0
        public string CanonicalizeHttpRequest(HttpRequestMessage request, string accountName)
        {
            // Add the method (GET, POST, PUT, or HEAD).
            CanonicalizedString canonicalizedString = new CanonicalizedString(request.Method.Method);

            // Add the Content-* HTTP headers. Empty values are allowed.
            if (request.Content != null)
            {
                canonicalizedString.AppendCanonicalizedElement(HttpWebUtility.CombineHttpHeaderValues(request.Content.Headers.ContentEncoding));
                canonicalizedString.AppendCanonicalizedElement(HttpWebUtility.CombineHttpHeaderValues(request.Content.Headers.ContentLanguage));
                AuthenticationUtility.AppendCanonicalizedContentLengthHeader(canonicalizedString, request);
                canonicalizedString.AppendCanonicalizedElement((request.Content.Headers.ContentMD5 == null) ? null :
                                                               Convert.ToBase64String(request.Content.Headers.ContentMD5));
                canonicalizedString.AppendCanonicalizedElement((request.Content.Headers.ContentType == null) ? null :
                                                               request.Content.Headers.ContentType.ToString());
            }
            else
            {
                canonicalizedString.AppendCanonicalizedElement(null);
                canonicalizedString.AppendCanonicalizedElement(null);
                if (request.Method == HttpMethod.Put || request.Method == HttpMethod.Delete)
                {
                    canonicalizedString.AppendCanonicalizedElement("0");
                }
                else
                {
                    canonicalizedString.AppendCanonicalizedElement(null);
                }

                canonicalizedString.AppendCanonicalizedElement(null);
                canonicalizedString.AppendCanonicalizedElement(null);
            }

            // Add the Date HTTP header (only if the x-ms-date header is not being used)
            AuthenticationUtility.AppendCanonicalizedDateHeader(canonicalizedString, request);

            // Add If-* headers and Range header
            canonicalizedString.AppendCanonicalizedElement(AuthenticationUtility.GetCanonicalizedHeaderValue(request.Headers.IfModifiedSince));
            canonicalizedString.AppendCanonicalizedElement(CommonUtility.GetFirstHeaderValue(request.Headers.IfMatch));
            canonicalizedString.AppendCanonicalizedElement(CommonUtility.GetFirstHeaderValue(request.Headers.IfNoneMatch));
            canonicalizedString.AppendCanonicalizedElement(AuthenticationUtility.GetCanonicalizedHeaderValue(request.Headers.IfUnmodifiedSince));
            canonicalizedString.AppendCanonicalizedElement((request.Headers.Range == null) ? null :
                                                           CommonUtility.GetFirstHeaderValue(request.Headers.Range.Ranges));

            // Add any custom headers
            AuthenticationUtility.AppendCanonicalizedCustomHeaders(canonicalizedString, request);

            // Add the canonicalized URI element
            string resourceString = AuthenticationUtility.GetCanonicalizedResourceString(request.RequestUri, accountName);

            canonicalizedString.AppendCanonicalizedElement(resourceString);

            return(canonicalizedString.ToString());
        }
Example #11
0
 /// <summary>
 /// Appends the value of the Content-Length header to the specified canonicalized string.
 /// </summary>
 /// <param name="canonicalizedString">The canonicalized string where the value is appended.</param>
 /// <param name="request">The request where the value is read from.</param>
 public static void AppendCanonicalizedContentLengthHeader(CanonicalizedString canonicalizedString, HttpWebRequest request)
 {
     if (request.ContentLength != -1L)
     {
         canonicalizedString.AppendCanonicalizedElement(request.ContentLength.ToString(CultureInfo.InvariantCulture));
     }
     else
     {
         canonicalizedString.AppendCanonicalizedElement(null);
     }
 }
 /// <summary>
 /// Appends the value of the Content-Length header to the specified canonicalized string.
 /// </summary>
 /// <param name="canonicalizedString">The canonicalized string where the value is appended.</param>
 /// <param name="request">The request where the value is read from.</param>
 public static void AppendCanonicalizedContentLengthHeader(CanonicalizedString canonicalizedString, HttpRequestMessage request)
 {
     long? contentLength = request.Content.Headers.ContentLength;
     if (contentLength.HasValue && contentLength.Value != -1L)
     {
         canonicalizedString.AppendCanonicalizedElement(contentLength.Value.ToString(CultureInfo.InvariantCulture));
     }
     else
     {
         canonicalizedString.AppendCanonicalizedElement(null);
     }
 }
        public string CanonicalizeHttpRequest(HttpRequestMessage request, string accountName)
        {
            // Add the method (GET, POST, PUT, or HEAD).
            CanonicalizedString canonicalizedString = new CanonicalizedString(request.Method.Method);

            // Add the Content-* HTTP headers. Empty values are allowed.
            if (request.Content != null)
            {
                canonicalizedString.AppendCanonicalizedElement(HttpWebUtility.CombineHttpHeaderValues(request.Content.Headers.ContentEncoding));
                canonicalizedString.AppendCanonicalizedElement(HttpWebUtility.CombineHttpHeaderValues(request.Content.Headers.ContentLanguage));
                AuthenticationUtility.AppendCanonicalizedContentLengthHeader(canonicalizedString, request);
                canonicalizedString.AppendCanonicalizedElement((request.Content.Headers.ContentMD5 == null) ? null :
                    Convert.ToBase64String(request.Content.Headers.ContentMD5));
                canonicalizedString.AppendCanonicalizedElement((request.Content.Headers.ContentType == null) ? null :
                    request.Content.Headers.ContentType.ToString());
            }
            else
            {
                canonicalizedString.AppendCanonicalizedElement(null);
                canonicalizedString.AppendCanonicalizedElement(null);
                if (request.Method == HttpMethod.Put || request.Method == HttpMethod.Delete)
                {
                    canonicalizedString.AppendCanonicalizedElement("0");
                }
                else
                {
                    canonicalizedString.AppendCanonicalizedElement(null);
                }

                canonicalizedString.AppendCanonicalizedElement(null);
                canonicalizedString.AppendCanonicalizedElement(null);
            }

            // Add the Date HTTP header (only if the x-ms-date header is not being used)
            AuthenticationUtility.AppendCanonicalizedDateHeader(canonicalizedString, request);

            // Add If-* headers and Range header
            canonicalizedString.AppendCanonicalizedElement(AuthenticationUtility.GetCanonicalizedHeaderValue(request.Headers.IfModifiedSince));
            canonicalizedString.AppendCanonicalizedElement(CommonUtility.GetFirstHeaderValue(request.Headers.IfMatch));
            canonicalizedString.AppendCanonicalizedElement(CommonUtility.GetFirstHeaderValue(request.Headers.IfNoneMatch));
            canonicalizedString.AppendCanonicalizedElement(AuthenticationUtility.GetCanonicalizedHeaderValue(request.Headers.IfUnmodifiedSince));
            canonicalizedString.AppendCanonicalizedElement((request.Headers.Range == null) ? null :
                CommonUtility.GetFirstHeaderValue(request.Headers.Range.Ranges));

            // Add any custom headers
            AuthenticationUtility.AppendCanonicalizedCustomHeaders(canonicalizedString, request);

            // Add the canonicalized URI element
            string resourceString = AuthenticationUtility.GetCanonicalizedResourceString(request.RequestUri, accountName);
            canonicalizedString.AppendCanonicalizedElement(resourceString);

            return canonicalizedString.ToString();
        }
        /// <summary>
        /// Converts the specified HTTP request data into a standard form appropriate for signing.
        /// </summary>
        /// <param name="request">The HTTP request that needs to be signed.</param>
        /// <param name="accountName">The name of the storage account that the HTTP request will access.</param>
        /// <returns>The canonicalized string containing the HTTP request data in a standard form appropriate for signing.</returns>
        /// <seealso href="http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx">Authentication for the Windows Azure Storage Services</seealso>
        public string CanonicalizeHttpRequest(HttpWebRequest request, string accountName)
        {
            // Add the x-ms-date or Date HTTP header
            string dateHeaderValue = AuthenticationUtility.GetPreferredDateHeaderValue(request);
            CanonicalizedString canonicalizedString = new CanonicalizedString(dateHeaderValue, ExpectedCanonicalizedStringLength);

            // Add the canonicalized URI element
            string resourceString = AuthenticationUtility.GetCanonicalizedResourceString(request.Address, accountName, true);
            canonicalizedString.AppendCanonicalizedElement(resourceString);

            return canonicalizedString.ToString();
        }
        public string CanonicalizeHttpRequest(HttpRequestMessage request, string accountName)
        {
            // Add the Date HTTP header (or the x-ms-date header if it is being used)
            string dateHeaderValue = AuthenticationUtility.GetPreferredDateHeaderValue(request);
            CanonicalizedString canonicalizedString = new CanonicalizedString(dateHeaderValue, ExpectedCanonicalizedStringLength);

            // Add the canonicalized URI element
            string resourceString = AuthenticationUtility.GetCanonicalizedResourceString(request.RequestUri, accountName, true);

            canonicalizedString.AppendCanonicalizedElement(resourceString);

            return(canonicalizedString.ToString());
        }
Example #16
0
        /// <summary>
        /// Appends the value of the Content-Length header to the specified canonicalized string.
        /// </summary>
        /// <param name="canonicalizedString">The canonicalized string where the value is appended.</param>
        /// <param name="request">The request where the value is read from.</param>
        public static void AppendCanonicalizedContentLengthHeader(CanonicalizedString canonicalizedString, HttpRequestMessage request)
        {
            long?contentLength = request.Content.Headers.ContentLength;

            if (contentLength.HasValue && contentLength.Value != -1L)
            {
                canonicalizedString.AppendCanonicalizedElement(contentLength.Value.ToString(CultureInfo.InvariantCulture));
            }
            else
            {
                canonicalizedString.AppendCanonicalizedElement(null);
            }
        }
        private static void AppendCanonicalizedDateHeader(CanonicalizedString canonicalizedString, HttpWebRequest request)
        {
            string microsoftDateHeaderValue = request.Headers["x-ms-date"];

            // Add the Date HTTP header (only if the x-ms-date header is not being used)
            if (string.IsNullOrEmpty(microsoftDateHeaderValue))
            {
                canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.Date]);
            }
            else
            {
                canonicalizedString.AppendCanonicalizedElement(null);
            }
        }
        /// <summary>
        /// Appends the values of the x-ms-* headers to the specified canonicalized string.
        /// </summary>
        /// <param name="canonicalizedString">The canonicalized string where the values are appended.</param>
        /// <param name="request">The request where the values are read from.</param>
        public static void AppendCanonicalizedCustomHeaders(CanonicalizedString canonicalizedString, HttpRequestMessage request)
        {
            CultureInfo sortingCulture = new CultureInfo("en-US");

#if WINDOWS_DESKTOP
            StringComparer sortingComparer = StringComparer.Create(sortingCulture, false);
#else
            StringComparer sortingComparer = new CultureStringComparer(sortingCulture, false);
#endif
            SortedDictionary <string, IEnumerable <string> > headers = new SortedDictionary <string, IEnumerable <string> >(sortingComparer);

            foreach (KeyValuePair <string, IEnumerable <string> > header in request.Headers)
            {
                string headerName = header.Key;
                if (headerName.StartsWith(Constants.HeaderConstants.PrefixForStorageHeader, StringComparison.OrdinalIgnoreCase))
                {
                    headers.Add(headerName.ToLowerInvariant(), header.Value);
                }
            }

            if (request.Content != null)
            {
                foreach (KeyValuePair <string, IEnumerable <string> > header in request.Content.Headers)
                {
                    string headerName = header.Key;
                    if (headerName.StartsWith(Constants.HeaderConstants.PrefixForStorageHeader, StringComparison.OrdinalIgnoreCase))
                    {
                        headers.Add(headerName.ToLowerInvariant(), header.Value);
                    }
                }
            }

            StringBuilder canonicalizedElement = new StringBuilder(ExpectedHeaderNameAndValueLength);
            foreach (KeyValuePair <string, IEnumerable <string> > header in headers)
            {
                canonicalizedElement.Clear();
                canonicalizedElement.Append(header.Key);
                canonicalizedElement.Append(HeaderNameValueSeparator);
                int keyLength = canonicalizedElement.Length;

                foreach (string value in header.Value)
                {
                    canonicalizedElement.Append(value.TrimStart().Replace("\r\n", string.Empty));
                    canonicalizedElement.Append(HeaderValueDelimiter);
                }

                canonicalizedString.AppendCanonicalizedElement(canonicalizedElement.ToString(0, canonicalizedElement.Length - 1));
            }
        }
Example #19
0
        /// <summary>
        /// Add x-ms- prefixed headers in a fixed order.
        /// </summary>
        /// <param name="headers">The headers.</param>
        /// <param name="canonicalizedString">The canonicalized string.</param>
        internal static void AddCanonicalizedHeaders(HttpRequestMessage request, CanonicalizedString canonicalizedString)
        {
            CultureInfo    sortingCulture  = new CultureInfo("en-US");
            StringComparer sortingComparer = new CultureStringComparer(sortingCulture, false);

            // Look for header names that start with x-ms-, then sort them in case-insensitive manner.
            SortedDictionary <string, IEnumerable <string> > httpStorageHeaders = new SortedDictionary <string, IEnumerable <string> >(sortingComparer);

            foreach (KeyValuePair <string, IEnumerable <string> > header in request.Headers)
            {
                if (header.Key.StartsWith(Constants.HeaderConstants.PrefixForStorageHeader, StringComparison.OrdinalIgnoreCase))
                {
                    httpStorageHeaders.Add(header.Key.ToLowerInvariant(), header.Value);
                }
            }

            if (request.Content != null)
            {
                foreach (KeyValuePair <string, IEnumerable <string> > header in request.Content.Headers)
                {
                    if (header.Key.StartsWith(Constants.HeaderConstants.PrefixForStorageHeader, StringComparison.OrdinalIgnoreCase))
                    {
                        httpStorageHeaders.Add(header.Key.ToLowerInvariant(), header.Value);
                    }
                }
            }

            // Now go through each header's values in the sorted order and append them to the canonicalized string.
            foreach (KeyValuePair <string, IEnumerable <string> > header in httpStorageHeaders)
            {
                StringBuilder canonicalizedElement = new StringBuilder(header.Key);
                string        delimiter            = ":";

                // Go through values, unfold them, and then append them to the canonicalized element string.
                foreach (string value in header.Value)
                {
                    // Unfolding is simply removal of CRLF.
                    string unfoldedValue = value.TrimStart().Replace("\r\n", string.Empty);

                    // Append it to the canonicalized element string.
                    canonicalizedElement.Append(delimiter);
                    canonicalizedElement.Append(unfoldedValue);
                    delimiter = ",";
                }

                // Now, add this canonicalized element to the canonicalized header string.
                canonicalizedString.AppendCanonicalizedElement(canonicalizedElement.ToString());
            }
        }
Example #20
0
        /// <summary>
        /// Converts the specified HTTP request data into a standard form appropriate for signing.
        /// </summary>
        /// <param name="request">The HTTP request that needs to be signed.</param>
        /// <param name="accountName">The name of the storage account that the HTTP request will access.</param>
        /// <returns>The canonicalized string containing the HTTP request data in a standard form appropriate for signing.</returns>
        /// <seealso href="http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx">Authentication for the Windows Azure Storage Services</seealso>
        public string CanonicalizeHttpRequest(HttpWebRequest request, string accountName)
        {
            CommonUtility.AssertNotNull("request", request);

            // Add the x-ms-date or Date HTTP header
            string dateHeaderValue = AuthenticationUtility.GetPreferredDateHeaderValue(request);
            CanonicalizedString canonicalizedString = new CanonicalizedString(dateHeaderValue, ExpectedCanonicalizedStringLength);

            // Add the canonicalized URI element
            string resourceString = AuthenticationUtility.GetCanonicalizedResourceString(request.RequestUri, accountName, true);

            canonicalizedString.AppendCanonicalizedElement(resourceString);

            return(canonicalizedString.ToString());
        }
        public static string CanonicalizeHttpRequest(UnityWebRequest request, string accountName)
        {
            CanonicalizedString canonicalizedString = new CanonicalizedString(request.method, 200);

            canonicalizedString.AppendCanonicalizedElement(request.GetRequestHeader("ContentMd5"));
            canonicalizedString.AppendCanonicalizedElement(request.GetRequestHeader("Content-Type"));

            AuthenticationUtility.AppendCanonicalizedDateHeader(canonicalizedString, request, true);

            string resourceString = AuthenticationUtility.GetCanonicalizedResourceString(new Uri(request.url), accountName, true);

            canonicalizedString.AppendCanonicalizedElement(resourceString);

            return(canonicalizedString.ToString());
        }
        /// <summary>
        /// Add x-ms- prefixed headers in a fixed order.
        /// </summary>
        /// <param name="headers">The headers.</param>
        /// <param name="canonicalizedString">The canonicalized string.</param>
        internal static void AddCanonicalizedHeaders(HttpRequestMessage request, CanonicalizedString canonicalizedString)
        {
            CultureInfo sortingCulture = new CultureInfo("en-US");
            StringComparer sortingComparer = new CultureStringComparer(sortingCulture, false);

            // Look for header names that start with x-ms-, then sort them in case-insensitive manner.
            SortedDictionary<string, IEnumerable<string>> httpStorageHeaders = new SortedDictionary<string, IEnumerable<string>>(sortingComparer);
            foreach (KeyValuePair<string, IEnumerable<string>> header in request.Headers)
            {
                if (header.Key.StartsWith(Constants.HeaderConstants.PrefixForStorageHeader, StringComparison.OrdinalIgnoreCase))
                {
                    httpStorageHeaders.Add(header.Key.ToLowerInvariant(), header.Value);
                }
            }

            if (request.Content != null)
            {
                foreach (KeyValuePair<string, IEnumerable<string>> header in request.Content.Headers)
                {
                    if (header.Key.StartsWith(Constants.HeaderConstants.PrefixForStorageHeader, StringComparison.OrdinalIgnoreCase))
                    {
                        httpStorageHeaders.Add(header.Key.ToLowerInvariant(), header.Value);
                    }
                }
            }

            // Now go through each header's values in the sorted order and append them to the canonicalized string.
            foreach (KeyValuePair<string, IEnumerable<string>> header in httpStorageHeaders)
            {
                StringBuilder canonicalizedElement = new StringBuilder(header.Key);
                string delimiter = ":";

                // Go through values, unfold them, and then append them to the canonicalized element string.
                foreach (string value in header.Value)
                {
                    // Unfolding is simply removal of CRLF.
                    string unfoldedValue = value.TrimStart().Replace("\r\n", string.Empty);

                    // Append it to the canonicalized element string.
                    canonicalizedElement.Append(delimiter);
                    canonicalizedElement.Append(unfoldedValue);
                    delimiter = ",";
                }

                // Now, add this canonicalized element to the canonicalized header string.
                canonicalizedString.AppendCanonicalizedElement(canonicalizedElement.ToString());
            }
        }
        public string CanonicalizeHttpRequest(HttpWebRequest request, string accountName)
        {
            // Start with x-ms-date or Date
            string dateValue = request.Headers[Constants.HeaderConstants.Date];
            if (string.IsNullOrEmpty(dateValue))
            {
                dateValue = request.Headers[HttpRequestHeader.Date];
            }

            CanonicalizedString canonicalizedString = new CanonicalizedString(dateValue);

            // And we only need the canonicalized resource in addition to date
            canonicalizedString.AppendCanonicalizedElement(CanonicalizationHelper.GetCanonicalizedResourceForSharedKeyLite(request.Address, accountName));

            return canonicalizedString.Value;
        }
 /// <summary>
 /// Appends the value of the Date header (or, optionally, the x-ms-date header) to the specified canonicalized string.
 /// </summary>
 /// <param name="canonicalizedString">The canonicalized string where the value is appended.</param>
 /// <param name="request">The request where the value is read from.</param>
 /// <param name="allowMicrosoftDateHeader">true if the value of the x-ms-date header can be used and is preferred; otherwise, false.</param>
 public static void AppendCanonicalizedDateHeader(CanonicalizedString canonicalizedString, HttpRequestMessage request, bool allowMicrosoftDateHeader = false)
 {
     string microsoftDateHeaderValue = HttpResponseMessageUtils.GetHeaderSingleValueOrDefault(request.Headers, Constants.HeaderConstants.Date);
     if (string.IsNullOrEmpty(microsoftDateHeaderValue))
     {
         canonicalizedString.AppendCanonicalizedElement(AuthenticationUtility.GetCanonicalizedHeaderValue(request.Headers.Date));
     }
     else if (allowMicrosoftDateHeader)
     {
         canonicalizedString.AppendCanonicalizedElement(microsoftDateHeaderValue);
     }
     else
     {
         canonicalizedString.AppendCanonicalizedElement(null);
     }
 }
        public string CanonicalizeHttpRequest(HttpRequestMessage request, string accountName)
        {
            // Start with x-ms-date or Date
            string dateValue = HttpResponseMessageUtils.GetHeaderSingleValueOrDefault(request.Headers, Constants.HeaderConstants.Date);          
            if (string.IsNullOrEmpty(dateValue))
            {
                dateValue = CommonUtils.GetDateTimeValueOrDefault(request.Headers.Date);
            }

            CanonicalizedString canonicalizedString = new CanonicalizedString(dateValue);

            // And we only need the canonicalized resource in addition to date
            canonicalizedString.AppendCanonicalizedElement(CanonicalizationHelper.GetCanonicalizedResourceForSharedKeyLite(request.RequestUri, accountName));

            return canonicalizedString.Value;
        }
        /// <summary>
        /// Add x-ms- prefixed headers in a fixed order.
        /// </summary>
        /// <param name="headers">The headers.</param>
        /// <param name="canonicalizedString">The canonicalized string.</param>
        internal static void AddCanonicalizedHeaders(WebHeaderCollection headers, CanonicalizedString canonicalizedString)
        {
            CultureInfo    sortingCulture  = new CultureInfo("en-US");
            StringComparer sortingComparer = StringComparer.Create(sortingCulture, false);

            // Look for header names that start with x-ms-, then sort them in case-insensitive manner.
            List <string> httpStorageHeaderNameArray = new List <string>();

            foreach (string key in headers.Keys)
            {
                if (key.StartsWith(Constants.HeaderConstants.PrefixForStorageHeader, StringComparison.OrdinalIgnoreCase))
                {
                    httpStorageHeaderNameArray.Add(key.ToLowerInvariant());
                }
            }

            httpStorageHeaderNameArray.Sort(sortingComparer);

            // Now go through each header's values in the sorted order and append them to the canonicalized string.
            foreach (string key in httpStorageHeaderNameArray)
            {
                StringBuilder canonicalizedElement = new StringBuilder(key);
                string        delimiter            = ":";

                // Go through values, unfold them, and then append them to the canonicalized element string.
                string[] values = headers.GetValues(key);

                if (values.Length == 0 || string.IsNullOrEmpty(values[0]))
                {
                    continue;
                }

                foreach (string value in values)
                {
                    // Unfolding is simply removal of CRLF.
                    string unfoldedValue = value.TrimStart().Replace("\r\n", string.Empty);

                    // Append it to the canonicalized element string.
                    canonicalizedElement.Append(delimiter);
                    canonicalizedElement.Append(unfoldedValue);
                    delimiter = ",";
                }

                // Now, add this canonicalized element to the canonicalized header string.
                canonicalizedString.AppendCanonicalizedElement(canonicalizedElement.ToString());
            }
        }
Example #27
0
        /// <summary>
        /// Appends the value of the Date header (or, optionally, the x-ms-date header) to the specified canonicalized string.
        /// </summary>
        /// <param name="canonicalizedString">The canonicalized string where the value is appended.</param>
        /// <param name="request">The request where the value is read from.</param>
        /// <param name="allowMicrosoftDateHeader">true if the value of the x-ms-date header can be used and is preferred; otherwise, false.</param>
        public static void AppendCanonicalizedDateHeader(CanonicalizedString canonicalizedString, HttpRequestMessage request, bool allowMicrosoftDateHeader = false)
        {
            string microsoftDateHeaderValue = HttpResponseMessageUtils.GetHeaderSingleValueOrDefault(request.Headers, Constants.HeaderConstants.Date);

            if (string.IsNullOrEmpty(microsoftDateHeaderValue))
            {
                canonicalizedString.AppendCanonicalizedElement(AuthenticationUtility.GetCanonicalizedHeaderValue(request.Headers.Date));
            }
            else if (allowMicrosoftDateHeader)
            {
                canonicalizedString.AppendCanonicalizedElement(microsoftDateHeaderValue);
            }
            else
            {
                canonicalizedString.AppendCanonicalizedElement(null);
            }
        }
Example #28
0
        /// <summary>
        /// Appends the value of the Date header (or, optionally, the x-ms-date header) to the specified canonicalized string.
        /// </summary>
        /// <param name="canonicalizedString">The canonicalized string where the value is appended.</param>
        /// <param name="request">The request where the value is read from.</param>
        /// <param name="allowMicrosoftDateHeader">true if the value of the x-ms-date header can be used and is preferred; otherwise, false.</param>
        public static void AppendCanonicalizedDateHeader(CanonicalizedString canonicalizedString, HttpWebRequest request, bool allowMicrosoftDateHeader = false)
        {
            string microsoftDateHeaderValue = request.Headers[Constants.HeaderConstants.Date];

            if (string.IsNullOrEmpty(microsoftDateHeaderValue))
            {
                canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.Date]);
            }
            else if (allowMicrosoftDateHeader)
            {
                canonicalizedString.AppendCanonicalizedElement(microsoftDateHeaderValue);
            }
            else
            {
                canonicalizedString.AppendCanonicalizedElement(null);
            }
        }
Example #29
0
        public string CanonicalizeHttpRequest(HttpRequestMessage request, string accountName)
        {
            // Start with x-ms-date or Date
            string dateValue = HttpResponseMessageUtils.GetHeaderSingleValueOrDefault(request.Headers, Constants.HeaderConstants.Date);

            if (string.IsNullOrEmpty(dateValue))
            {
                dateValue = CommonUtils.GetDateTimeValueOrDefault(request.Headers.Date);
            }

            CanonicalizedString canonicalizedString = new CanonicalizedString(dateValue);

            // And we only need the canonicalized resource in addition to date
            canonicalizedString.AppendCanonicalizedElement(CanonicalizationHelper.GetCanonicalizedResourceForSharedKeyLite(request.RequestUri, accountName));

            return(canonicalizedString.Value);
        }
        public string CanonicalizeHttpRequest(HttpWebRequest request, string accountName)
        {
            // Start with x-ms-date or Date
            string dateValue = request.Headers[Constants.HeaderConstants.Date];

            if (string.IsNullOrEmpty(dateValue))
            {
                dateValue = request.Headers[HttpRequestHeader.Date];
            }

            CanonicalizedString canonicalizedString = new CanonicalizedString(dateValue);

            // And we only need the canonicalized resource in addition to date
            canonicalizedString.AppendCanonicalizedElement(CanonicalizationHelper.GetCanonicalizedResourceForSharedKeyLite(request.Address, accountName));

            return(canonicalizedString.Value);
        }
Example #31
0
        public static void AppendCanonicalizedDateHeader(CanonicalizedString canonicalizedString, UnityWebRequest request, bool allowMicrosoftDateHeader = false)
        {
            string microsoftDateHeaderValue = request.GetRequestHeader("x-ms-date");

            if (string.IsNullOrEmpty(microsoftDateHeaderValue))
            {
                canonicalizedString.AppendCanonicalizedElement(request.GetRequestHeader("Date"));
            }
            else if (allowMicrosoftDateHeader)
            {
                canonicalizedString.AppendCanonicalizedElement(microsoftDateHeaderValue);
            }
            else
            {
                canonicalizedString.AppendCanonicalizedElement(null);
            }
        }
        /// <summary>
        /// Add x-ms- prefixed headers in a fixed order.
        /// </summary>
        /// <param name="headers">The headers.</param>
        /// <param name="canonicalizedString">The canonicalized string.</param>
        internal static void AddCanonicalizedHeaders(WebHeaderCollection headers, CanonicalizedString canonicalizedString)
        {
            CultureInfo sortingCulture = new CultureInfo("en-US");
            StringComparer sortingComparer = StringComparer.Create(sortingCulture, false);

            // Look for header names that start with x-ms-, then sort them in case-insensitive manner.
            List<string> httpStorageHeaderNameArray = new List<string>();
            foreach (string key in headers.Keys)
            {
                if (key.StartsWith(Constants.HeaderConstants.PrefixForStorageHeader, StringComparison.OrdinalIgnoreCase))
                {
                    httpStorageHeaderNameArray.Add(key.ToLowerInvariant());
                }
            }

            httpStorageHeaderNameArray.Sort(sortingComparer);

            // Now go through each header's values in the sorted order and append them to the canonicalized string.
            foreach (string key in httpStorageHeaderNameArray)
            {
                StringBuilder canonicalizedElement = new StringBuilder(key);
                string delimiter = ":";

                // Go through values, unfold them, and then append them to the canonicalized element string.
                string[] values = headers.GetValues(key);

                if (values.Length == 0 || string.IsNullOrEmpty(values[0]))
                {
                    continue;
                }

                foreach (string value in values)
                {
                    // Unfolding is simply removal of CRLF.
                    string unfoldedValue = value.TrimStart().Replace("\r\n", string.Empty);

                    // Append it to the canonicalized element string.
                    canonicalizedElement.Append(delimiter);
                    canonicalizedElement.Append(unfoldedValue);
                    delimiter = ",";
                }

                // Now, add this canonicalized element to the canonicalized header string.
                canonicalizedString.AppendCanonicalizedElement(canonicalizedElement.ToString());
            }
        }
        /// <summary>
        /// Converts the specified HTTP request data into a standard form appropriate for signing.
        /// </summary>
        /// <param name="request">The HTTP request that needs to be signed.</param>
        /// <param name="accountName">The name of the storage account that the HTTP request will access.</param>
        /// <returns>The canonicalized string containing the HTTP request data in a standard form appropriate for signing.</returns>
        /// <seealso href="http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx">Authentication for the Windows Azure Storage Services</seealso>
        public string CanonicalizeHttpRequest(HttpWebRequest request, string accountName)
        {
            // Add the method (GET, POST, PUT, or HEAD).
            CanonicalizedString canonicalizedString = new CanonicalizedString(request.Method, ExpectedCanonicalizedStringLength);

            // Add the Content-* HTTP headers. Empty values are allowed.
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.ContentMd5]);
            canonicalizedString.AppendCanonicalizedElement(request.ContentType);

            // Add the Date HTTP header (or the x-ms-date header if it is being used)
            AuthenticationUtility.AppendCanonicalizedDateHeader(canonicalizedString, request, true);

            // Add the canonicalized URI element
            string resourceString = AuthenticationUtility.GetCanonicalizedResourceString(request.Address, accountName, true);
            canonicalizedString.AppendCanonicalizedElement(resourceString);

            return canonicalizedString.ToString();
        }
        public string CanonicalizeHttpRequest(HttpWebRequest request, string accountName)
        {
            // The first element should be the Method of the request.
            // I.e. GET, POST, PUT, or HEAD.
            CanonicalizedString canonicalizedString = new CanonicalizedString(request.Method);

            // The next elements are Content*
            // If any element is missing it may be empty.
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.ContentEncoding]);
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.ContentLanguage]);
            canonicalizedString.AppendCanonicalizedElement(request.ContentLength == -1 ? string.Empty : request.ContentLength.ToString());
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.ContentMd5]);
            canonicalizedString.AppendCanonicalizedElement(request.ContentType);

            // If x-ms-date header exists, Date should be empty string
            string dateValue = CommonUtils.GetSingleValueOrDefault(request.Headers.GetValues(Constants.HeaderConstants.Date));

            if (!string.IsNullOrEmpty(dateValue))
            {
                dateValue = string.Empty;
            }
            else
            {
                dateValue = request.Headers[HttpRequestHeader.Date];
            }

            canonicalizedString.AppendCanonicalizedElement(dateValue);

            // Conditionals and range
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.IfModifiedSince]);
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.IfMatch]);
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.IfNoneMatch]);
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.IfUnmodifiedSince]);
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.Range]);

            // Rest of the headers
            CanonicalizationHelper.AddCanonicalizedHeaders(request.Headers, canonicalizedString);

            // Now we append the canonicalized resource element
            canonicalizedString.AppendCanonicalizedElement(CanonicalizationHelper.GetCanonicalizedResourceForSharedKey(request.Address, accountName));

            return(canonicalizedString.Value);
        }
        /// <summary>
        /// Converts the specified HTTP request data into a standard form appropriate for signing.
        /// </summary>
        /// <param name="request">The HTTP request that needs to be signed.</param>
        /// <param name="accountName">The name of the storage account that the HTTP request will access.</param>
        /// <returns>The canonicalized string containing the HTTP request data in a standard form appropriate for signing.</returns>
        /// <seealso href="http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx">Authentication for the Windows Azure Storage Services</seealso>
        public string CanonicalizeHttpRequest(HttpWebRequest request, string accountName)
        {
            // Add the method (GET, POST, PUT, or HEAD).
            CanonicalizedString canonicalizedString = new CanonicalizedString(request.Method, ExpectedCanonicalizedStringLength);

            // Add the Content-* HTTP headers. Empty values are allowed.
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.ContentMd5]);
            canonicalizedString.AppendCanonicalizedElement(request.ContentType);

            // Add the Date HTTP header (or the x-ms-date header if it is being used)
            AuthenticationUtility.AppendCanonicalizedDateHeader(canonicalizedString, request, true);

            // Add the canonicalized URI element
            string resourceString = AuthenticationUtility.GetCanonicalizedResourceString(request.Address, accountName, true);

            canonicalizedString.AppendCanonicalizedElement(resourceString);

            return(canonicalizedString.ToString());
        }
        public string CanonicalizeHttpRequest(HttpWebRequest request, string accountName)
        {
            // The first element should be the Method of the request.
            // I.e. GET, POST, PUT, or HEAD.
            CanonicalizedString canonicalizedString = new CanonicalizedString(request.Method);

            // The next elements are Content*
            // If any element is missing it may be empty.
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.ContentEncoding]);
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.ContentLanguage]);
            canonicalizedString.AppendCanonicalizedElement(request.ContentLength == -1 ? string.Empty : request.ContentLength.ToString());
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.ContentMd5]);
            canonicalizedString.AppendCanonicalizedElement(request.ContentType);

            // If x-ms-date header exists, Date should be empty string
            string dateValue = CommonUtils.GetSingleValueOrDefault(request.Headers.GetValues(Constants.HeaderConstants.Date));
            if (!string.IsNullOrEmpty(dateValue))
            {
                dateValue = string.Empty;
            }
            else
            {
                dateValue = request.Headers[HttpRequestHeader.Date];
            }

            canonicalizedString.AppendCanonicalizedElement(dateValue);

            // Conditionals and range
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.IfModifiedSince]);
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.IfMatch]);
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.IfNoneMatch]);
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.IfUnmodifiedSince]);
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.Range]);

            // Rest of the headers
            CanonicalizationHelper.AddCanonicalizedHeaders(request.Headers, canonicalizedString);

            // Now we append the canonicalized resource element
            canonicalizedString.AppendCanonicalizedElement(CanonicalizationHelper.GetCanonicalizedResourceForSharedKey(request.Address, accountName));

            return canonicalizedString.Value;
        }
Example #37
0
        static string GetCanonicalizedResourceVersion2(Uri address, string accountName)
        {
            StringBuilder builder = new StringBuilder("/");

            builder.Append(accountName);
            builder.Append(address.AbsolutePath);
            CanonicalizedString str     = new CanonicalizedString(builder.ToString());
            NameValueCollection values  = HttpUtility.ParseQueryString(address.Query);
            NameValueCollection values2 = new NameValueCollection();

            foreach (string str2 in values.Keys)
            {
                ArrayList list = new ArrayList(values.GetValues(str2));
                list.Sort();
                StringBuilder builder2 = new StringBuilder();
                foreach (object obj2 in list)
                {
                    if (builder2.Length > 0)
                    {
                        builder2.Append(",");
                    }
                    builder2.Append(obj2.ToString());
                }
                values2.Add((str2 == null) ? str2 : str2.ToLowerInvariant(), builder2.ToString());
            }
            ArrayList list2 = new ArrayList(values2.AllKeys);

            list2.Sort();
            foreach (string str3 in list2)
            {
                StringBuilder builder3 = new StringBuilder(string.Empty);
                builder3.Append(str3);
                builder3.Append(":");
                builder3.Append(values2[str3]);
                str.AppendCanonicalizedElement(builder3.ToString());
            }
            return(str.Value);
        }
        /// <summary>
        /// Converts the specified HTTP request data into a standard form appropriate for signing.
        /// </summary>
        /// <param name="request">The HTTP request that needs to be signed.</param>
        /// <param name="accountName">The name of the storage account that the HTTP request will access.</param>
        /// <returns>The canonicalized string containing the HTTP request data in a standard form appropriate for signing.</returns>
        /// <seealso href="http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx">Authentication for the Microsoft Azure Storage Services</seealso>
        public string CanonicalizeHttpRequest(HttpWebRequest request, string accountName)
        {
            CommonUtility.AssertNotNull("request", request);

            // Add the method (GET, POST, PUT, or HEAD).
            CanonicalizedString canonicalizedString = new CanonicalizedString(request.Method, ExpectedCanonicalizedStringLength);

            // Add the Content-* HTTP headers. Empty values are allowed.
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.ContentMd5]);
            canonicalizedString.AppendCanonicalizedElement(request.ContentType);

            // Add the Date HTTP header (only if the x-ms-date header is not being used)
            AuthenticationUtility.AppendCanonicalizedDateHeader(canonicalizedString, request);

            // Add any custom headers
            AuthenticationUtility.AppendCanonicalizedCustomHeaders(canonicalizedString, request);

            // Add the canonicalized URI element
            string resourceString = AuthenticationUtility.GetCanonicalizedResourceString(request.RequestUri, accountName, true);
            canonicalizedString.AppendCanonicalizedElement(resourceString);

            return canonicalizedString.ToString();
        }
        /// <summary>
        /// Converts the specified HTTP request data into a standard form appropriate for signing.
        /// </summary>
        /// <param name="request">The HTTP request that needs to be signed.</param>
        /// <param name="accountName">The name of the storage account that the HTTP request will access.</param>
        /// <returns>The canonicalized string containing the HTTP request data in a standard form appropriate for signing.</returns>
        /// <seealso href="http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx">Authentication for the Windows Azure Storage Services</seealso>
        public string CanonicalizeHttpRequest(HttpWebRequest request, string accountName)
        {
            CommonUtility.AssertNotNull("request", request);

            // Add the method (GET, POST, PUT, or HEAD).
            CanonicalizedString canonicalizedString = new CanonicalizedString(request.Method, ExpectedCanonicalizedStringLength);

            // Add the Content-* HTTP headers. Empty values are allowed.
            canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.ContentMd5]);
            canonicalizedString.AppendCanonicalizedElement(request.ContentType);

            // Add the Date HTTP header (only if the x-ms-date header is not being used)
            AuthenticationUtility.AppendCanonicalizedDateHeader(canonicalizedString, request);

            // Add any custom headers
            AuthenticationUtility.AppendCanonicalizedCustomHeaders(canonicalizedString, request);

            // Add the canonicalized URI element
            string resourceString = AuthenticationUtility.GetCanonicalizedResourceString(request.RequestUri, accountName, true);
            canonicalizedString.AppendCanonicalizedElement(resourceString);

            return canonicalizedString.ToString();
        }
        public string CanonicalizeHttpRequest(HttpRequestMessage request, string accountName)
        {
            // The first element should be the Method of the request.
            // I.e. GET, POST, PUT, or HEAD.
            CanonicalizedString canonicalizedString = new CanonicalizedString(request.Method.Method);

            // The next elements are Content*
            // If any element is missing it may be empty.
            if (request.Content != null)
            {
                canonicalizedString.AppendCanonicalizedElement(HttpUtility.CombineHttpHeaderValues(request.Content.Headers.ContentEncoding));
                canonicalizedString.AppendCanonicalizedElement(HttpUtility.CombineHttpHeaderValues(request.Content.Headers.ContentLanguage));

                long contentLength = request.Content.Headers.ContentLength.HasValue ? request.Content.Headers.ContentLength.Value : -1;
                canonicalizedString.AppendCanonicalizedElement(contentLength == -1 ? string.Empty : contentLength.ToString());

                canonicalizedString.AppendCanonicalizedElement((request.Content.Headers.ContentMD5 == null) ? string.Empty :
                                                               Convert.ToBase64String(request.Content.Headers.ContentMD5));
                canonicalizedString.AppendCanonicalizedElement((request.Content.Headers.ContentType == null) ? string.Empty :
                                                               request.Content.Headers.ContentType.ToString());
            }
            else
            {
                canonicalizedString.AppendCanonicalizedElement(string.Empty);
                canonicalizedString.AppendCanonicalizedElement(string.Empty);
                if ((request.Method == HttpMethod.Put) ||
                    (request.Method == HttpMethod.Delete))
                {
                    canonicalizedString.AppendCanonicalizedElement("0");
                }
                else
                {
                    canonicalizedString.AppendCanonicalizedElement(string.Empty);
                }

                canonicalizedString.AppendCanonicalizedElement(string.Empty);
                canonicalizedString.AppendCanonicalizedElement(string.Empty);
            }

            // If x-ms-date header exists, Date should be empty string
            string dateValue = HttpResponseMessageUtils.GetHeaderSingleValueOrDefault(request.Headers, Constants.HeaderConstants.Date);

            if (!string.IsNullOrEmpty(dateValue))
            {
                dateValue = string.Empty;
            }
            else
            {
                dateValue = CommonUtils.GetDateTimeValueOrDefault(request.Headers.Date);
            }

            canonicalizedString.AppendCanonicalizedElement(dateValue);

            // Conditionals and range
            canonicalizedString.AppendCanonicalizedElement(CommonUtils.GetDateTimeValueOrDefault(request.Headers.IfModifiedSince));
            canonicalizedString.AppendCanonicalizedElement(CommonUtils.GetSingleValueOrDefault(request.Headers.IfMatch));
            canonicalizedString.AppendCanonicalizedElement(CommonUtils.GetSingleValueOrDefault(request.Headers.IfNoneMatch));
            canonicalizedString.AppendCanonicalizedElement(CommonUtils.GetDateTimeValueOrDefault(request.Headers.IfUnmodifiedSince));
            canonicalizedString.AppendCanonicalizedElement((request.Headers.Range == null) ? string.Empty :
                                                           CommonUtils.GetSingleValueOrDefault(request.Headers.Range.Ranges));

            // Rest of the headers
            CanonicalizationHelper.AddCanonicalizedHeaders(request, canonicalizedString);

            // Now we append the canonicalized resource element
            canonicalizedString.AppendCanonicalizedElement(CanonicalizationHelper.GetCanonicalizedResourceForSharedKey(request.RequestUri, accountName));

            return(canonicalizedString.Value);
        }
Example #41
0
        internal static string CanonicalizeHttpRequest(
            Uri address, 
            ResourceUriComponents uriComponents, 
            string method, 
            string contentType, 
            string date, 
            NameValueCollection headers)
        {
            // The first element should be the Method of the request.
            // I.e. GET, POST, PUT, or HEAD.
            CanonicalizedString canonicalizedString = new CanonicalizedString(method);

            // The second element should be the MD5 value.
            // This is optional and may be empty.
            string httpContentMD5Value = string.Empty;

            // First extract all the content MD5 values from the header.
            ArrayList httpContentMD5Values = HttpRequestAccessor.GetHeaderValues(headers, StorageHttpConstants.HeaderNames.ContentMD5);

            // If we only have one, then set it to the value we want to append to the canonicalized string.
            if (httpContentMD5Values.Count == 1)
            {
                httpContentMD5Value = (string)httpContentMD5Values[0];
            }

            canonicalizedString.AppendCanonicalizedElement(httpContentMD5Value);

            // The third element should be the content type.
            canonicalizedString.AppendCanonicalizedElement(contentType);

            // The fourth element should be the request date.
            // See if there's an storage date header.
            // If there's one, then don't use the date header.
            ArrayList httpStorageDateValues = HttpRequestAccessor.GetHeaderValues(headers, StorageHttpConstants.HeaderNames.StorageDateTime);
            if (httpStorageDateValues.Count > 0)
            {
                date = null;
            }

            canonicalizedString.AppendCanonicalizedElement(date);

            // Look for header names that start with StorageHttpConstants.HeaderNames.PrefixForStorageHeader
            // Then sort them in case-insensitive manner.
            ArrayList httpStorageHeaderNameArray = new ArrayList();
            foreach (string key in headers.Keys)
            {
                if (key.ToLowerInvariant().StartsWith(StorageHttpConstants.HeaderNames.PrefixForStorageHeader, StringComparison.Ordinal))
                {
                    httpStorageHeaderNameArray.Add(key.ToLowerInvariant());
                }
            }

            httpStorageHeaderNameArray.Sort();

            // Now go through each header's values in the sorted order and append them to the canonicalized string.
            foreach (string key in httpStorageHeaderNameArray)
            {
                StringBuilder canonicalizedElement = new StringBuilder(key);
                string delimiter = ":";
                ArrayList values = HttpRequestAccessor.GetHeaderValues(headers, key);

                // Go through values, unfold them, and then append them to the canonicalized element string.
                foreach (string value in values)
                {
                    // Unfolding is simply removal of CRLF.
                    string unfoldedValue = value.Replace(StorageHttpConstants.ConstChars.CarriageReturnLinefeed, string.Empty);

                    // Append it to the canonicalized element string.
                    canonicalizedElement.Append(delimiter);
                    canonicalizedElement.Append(unfoldedValue);
                    delimiter = ",";
                }

                // Now, add this canonicalized element to the canonicalized header string.
                canonicalizedString.AppendCanonicalizedElement(canonicalizedElement.ToString());
            }

            // Now we append the canonicalized resource element.
            string canonicalizedResource = GetCanonicalizedResource(address, uriComponents);
            canonicalizedString.AppendCanonicalizedElement(canonicalizedResource);

            return canonicalizedString.Value;
        }
Example #42
0
        internal static string CanonicalizeHttpRequest(
            Uri address,
            ResourceUriComponents uriComponents,
            string method,
            string contentType,
            string date,
            NameValueCollection headers)
        {
            // The first element should be the Method of the request.
            // I.e. GET, POST, PUT, or HEAD.
            CanonicalizedString canonicalizedString = new CanonicalizedString(method);

            // The second element should be the MD5 value.
            // This is optional and may be empty.
            string httpContentMD5Value = string.Empty;

            // First extract all the content MD5 values from the header.
            ArrayList httpContentMD5Values = HttpRequestAccessor.GetHeaderValues(headers, StorageHttpConstants.HeaderNames.ContentMD5);

            // If we only have one, then set it to the value we want to append to the canonicalized string.
            if (httpContentMD5Values.Count == 1)
            {
                httpContentMD5Value = (string)httpContentMD5Values[0];
            }

            canonicalizedString.AppendCanonicalizedElement(httpContentMD5Value);

            // The third element should be the content type.
            canonicalizedString.AppendCanonicalizedElement(contentType);

            // The fourth element should be the request date.
            // See if there's an storage date header.
            // If there's one, then don't use the date header.
            ArrayList httpStorageDateValues = HttpRequestAccessor.GetHeaderValues(headers, StorageHttpConstants.HeaderNames.StorageDateTime);

            if (httpStorageDateValues.Count > 0)
            {
                date = null;
            }

            canonicalizedString.AppendCanonicalizedElement(date);

            // Look for header names that start with StorageHttpConstants.HeaderNames.PrefixForStorageHeader
            // Then sort them in case-insensitive manner.
            ArrayList httpStorageHeaderNameArray = new ArrayList();

            foreach (string key in headers.Keys)
            {
                if (key.ToLowerInvariant().StartsWith(StorageHttpConstants.HeaderNames.PrefixForStorageHeader, StringComparison.Ordinal))
                {
                    httpStorageHeaderNameArray.Add(key.ToLowerInvariant());
                }
            }

            httpStorageHeaderNameArray.Sort();

            // Now go through each header's values in the sorted order and append them to the canonicalized string.
            foreach (string key in httpStorageHeaderNameArray)
            {
                StringBuilder canonicalizedElement = new StringBuilder(key);
                string        delimiter            = ":";
                ArrayList     values = HttpRequestAccessor.GetHeaderValues(headers, key);

                // Go through values, unfold them, and then append them to the canonicalized element string.
                foreach (string value in values)
                {
                    // Unfolding is simply removal of CRLF.
                    string unfoldedValue = value.Replace(StorageHttpConstants.ConstChars.CarriageReturnLinefeed, string.Empty);

                    // Append it to the canonicalized element string.
                    canonicalizedElement.Append(delimiter);
                    canonicalizedElement.Append(unfoldedValue);
                    delimiter = ",";
                }

                // Now, add this canonicalized element to the canonicalized header string.
                canonicalizedString.AppendCanonicalizedElement(canonicalizedElement.ToString());
            }

            // Now we append the canonicalized resource element.
            string canonicalizedResource = GetCanonicalizedResource(address, uriComponents);

            canonicalizedString.AppendCanonicalizedElement(canonicalizedResource);

            return(canonicalizedString.Value);
        }
        /// <summary>
        /// Appends the values of the x-ms-* headers to the specified canonicalized string.
        /// </summary>
        /// <param name="canonicalizedString">The canonicalized string where the values are appended.</param>
        /// <param name="request">The request where the values are read from.</param>
        public static void AppendCanonicalizedCustomHeaders(CanonicalizedString canonicalizedString, HttpRequestMessage request)
        {
            CultureInfo sortingCulture = new CultureInfo("en-US");
            StringComparer sortingComparer = new CultureStringComparer(sortingCulture, false);
            SortedDictionary<string, IEnumerable<string>> headers = new SortedDictionary<string, IEnumerable<string>>(sortingComparer);

            foreach (KeyValuePair<string, IEnumerable<string>> header in request.Headers)
            {
                string headerName = header.Key;
                if (headerName.StartsWith(Constants.HeaderConstants.PrefixForStorageHeader, StringComparison.OrdinalIgnoreCase))
                {
                    headers.Add(headerName.ToLowerInvariant(), header.Value);
                }
            }

            if (request.Content != null)
            {
                foreach (KeyValuePair<string, IEnumerable<string>> header in request.Content.Headers)
                {
                    string headerName = header.Key;
                    if (headerName.StartsWith(Constants.HeaderConstants.PrefixForStorageHeader, StringComparison.OrdinalIgnoreCase))
                    {
                        headers.Add(headerName.ToLowerInvariant(), header.Value);
                    }
                }
            }

            StringBuilder canonicalizedElement = new StringBuilder(ExpectedHeaderNameAndValueLength);
            foreach (KeyValuePair<string, IEnumerable<string>> header in headers)
            {
                canonicalizedElement.Clear();
                canonicalizedElement.Append(header.Key);
                canonicalizedElement.Append(HeaderNameValueSeparator);

                foreach (string value in header.Value)
                {
                    canonicalizedElement.Append(value.TrimStart().Replace("\r\n", string.Empty));
                    canonicalizedElement.Append(HeaderValueDelimiter);
                }

                canonicalizedString.AppendCanonicalizedElement(canonicalizedElement.ToString(0, canonicalizedElement.Length - 1));
            }
        }
        public static void AppendCanonicalizedCustomHeaders(CanonicalizedString canonicalizedString, HttpWebRequest request)
        {
            List<string> headerNames = new List<string>(request.Headers.AllKeys.Length);
            foreach (string headerName in request.Headers.AllKeys)
            {
                if (headerName.StartsWith(Constants.HeaderConstants.PrefixForStorageHeader, StringComparison.OrdinalIgnoreCase))
                {
                    headerNames.Add(headerName.ToLowerInvariant());
                }
            }

            CultureInfo sortingCulture = new CultureInfo("en-US");
            StringComparer sortingComparer = StringComparer.Create(sortingCulture, false);
            headerNames.Sort(sortingComparer);

            StringBuilder canonicalizedElement = new StringBuilder(ExpectedHeaderNameAndValueLength);
            foreach (string headerName in headerNames)
            {
                string value = request.Headers[headerName];
                if (!string.IsNullOrEmpty(value))
                {
                    canonicalizedElement.Length = 0;
                    canonicalizedElement.Append(headerName);
                    canonicalizedElement.Append(HeaderNameValueSeparator);
                    canonicalizedElement.Append(value.TrimStart().Replace("\r\n", string.Empty));

                    canonicalizedString.AppendCanonicalizedElement(canonicalizedElement.ToString());
                }
            }
        }
 /// <summary>
 /// Appends the value of the Date header (or, optionally, the x-ms-date header) to the specified canonicalized string.
 /// </summary>
 /// <param name="canonicalizedString">The canonicalized string where the value is appended.</param>
 /// <param name="request">The request where the value is read from.</param>
 /// <param name="allowMicrosoftDateHeader">true if the value of the x-ms-date header can be used and is preferred; otherwise, false.</param>
 public static void AppendCanonicalizedDateHeader(CanonicalizedString canonicalizedString, HttpWebRequest request, bool allowMicrosoftDateHeader = false)
 {
     string microsoftDateHeaderValue = request.Headers[Constants.HeaderConstants.Date];
     if (string.IsNullOrEmpty(microsoftDateHeaderValue))
     {
         canonicalizedString.AppendCanonicalizedElement(request.Headers[HttpRequestHeader.Date]);
     }
     else if (allowMicrosoftDateHeader)
     {
         canonicalizedString.AppendCanonicalizedElement(microsoftDateHeaderValue);
     }
     else
     {
         canonicalizedString.AppendCanonicalizedElement(null);
     }
 }
 /// <summary>
 /// Appends the value of the Content-Length header to the specified canonicalized string.
 /// </summary>
 /// <param name="canonicalizedString">The canonicalized string where the value is appended.</param>
 /// <param name="request">The request where the value is read from.</param>
 public static void AppendCanonicalizedContentLengthHeader(CanonicalizedString canonicalizedString, HttpWebRequest request)
 {
     if (request.ContentLength != -1L)
     {
         canonicalizedString.AppendCanonicalizedElement(request.ContentLength.ToString(CultureInfo.InvariantCulture));
     }
     else
     {
         canonicalizedString.AppendCanonicalizedElement(null);
     }
 }