private static void AddCanonicalizedHeaders(WebHeaderCollection headers, CanonicalizedString canonicalizedString)
        {
            var keyList = new List<string>();
            foreach (string key in headers.AllKeys)
            {
                if (key.ToLowerInvariant().StartsWith("x-ms-", StringComparison.Ordinal))
                {
                    keyList.Add(key.ToLowerInvariant());
                }
            }

            keyList.Sort();
            foreach (string key in keyList)
            {
                var builder = new StringBuilder(key);
                var colonAndComma = ":";
                foreach (string values in GetHeaderValues(headers, key))
                {
                    string value = values.Replace("\r\n", string.Empty);
                    builder.Append(colonAndComma);
                    builder.Append(value);
                    colonAndComma = ",";
                }

                canonicalizedString.AppendCanonicalizedElement(builder.ToString());
            }
        }
        private static string GetCanonicalizedResourceVersion2(Uri address, string accountName)
        {
            var builder = new StringBuilder("/");
            builder.Append(accountName);
            builder.Append(address.AbsolutePath);

            var canonicalizedString = new CanonicalizedString(builder.ToString());
            var values = HttpUtility.ParseQueryString(address.Query);
            var allValues = new WebHeaderCollection();
            foreach (string key in values.AllKeys)
            {
                var list = new List<string>(values.GetValues(key));
                list.Sort();

                var innerBuilder = new StringBuilder();
                foreach (object obj2 in list)
                {
                    if (innerBuilder.Length > 0)
                    {
                        innerBuilder.Append(",");
                    }

                    innerBuilder.Append(obj2.ToString());
                }

                allValues.Add(string.IsNullOrWhiteSpace(key) ? key : key.ToLowerInvariant(), innerBuilder.ToString());
            }

            var keyList = new List<string>(allValues.AllKeys);
            keyList.Sort();

            foreach (string key in keyList)
            {
                var innerBuilder = new StringBuilder(string.Empty);
                innerBuilder.Append(key);
                innerBuilder.Append(":");
                innerBuilder.Append(allValues[key]);
                canonicalizedString.AppendCanonicalizedElement(innerBuilder.ToString());
            }

            return canonicalizedString.Value;
        }
 private static string CanonicalizeHttpRequest(string accountName, long contentLength, WebRequest webRequest)
 {
     var canonicalizedString = new CanonicalizedString(webRequest.Method);
     canonicalizedString.AppendCanonicalizedElement(GetStandardHeaderValue(webRequest.Headers, "Content-Encoding"));
     canonicalizedString.AppendCanonicalizedElement(GetStandardHeaderValue(webRequest.Headers, "Content-Language"));
     canonicalizedString.AppendCanonicalizedElement((contentLength == -1L) ? string.Empty : contentLength.ToString(CultureInfo.InvariantCulture));
     canonicalizedString.AppendCanonicalizedElement(GetStandardHeaderValue(webRequest.Headers, "Content-MD5"));
     canonicalizedString.AppendCanonicalizedElement(webRequest.ContentType);
     canonicalizedString.AppendCanonicalizedElement(!string.IsNullOrWhiteSpace(GetStandardHeaderValue(webRequest.Headers, "x-ms-date")) ? string.Empty : GetStandardHeaderValue(webRequest.Headers, "Date"));
     canonicalizedString.AppendCanonicalizedElement(GetStandardHeaderValue(webRequest.Headers, "If-Modified-Since"));
     canonicalizedString.AppendCanonicalizedElement(GetStandardHeaderValue(webRequest.Headers, "If-Match"));
     canonicalizedString.AppendCanonicalizedElement(GetStandardHeaderValue(webRequest.Headers, "If-None-Match"));
     canonicalizedString.AppendCanonicalizedElement(GetStandardHeaderValue(webRequest.Headers, "If-Unmodified-Since"));
     canonicalizedString.AppendCanonicalizedElement(GetStandardHeaderValue(webRequest.Headers, "Range"));
     AddCanonicalizedHeaders(webRequest.Headers, canonicalizedString);
     AddCanonicalizedResourceVersion2(webRequest.RequestUri, accountName, canonicalizedString);
     return canonicalizedString.Value;
 }
        private static string CanonicalizeHttpRequestLite(string accountName, string xmsdate, Uri requestUri)
        {
            if (string.IsNullOrWhiteSpace(xmsdate))
            {
                throw new ArgumentException(
                        string.Format(
                        CultureInfo.CurrentCulture,
                        "Canonicalization did not find a non empty x-ms-date header in the WebRequest. Please use a WebRequest with a valid x-ms-date header in RFC 123 format (example request.Headers[\"x-ms-date\"] = DateTime.UtcNow.ToString(\"R\", CultureInfo.InvariantCulture))",
                        new object[0]),
                    "xmsdate");
            }

            var canonicalizedString = new CanonicalizedString(xmsdate);
            canonicalizedString.AppendCanonicalizedElement(GetCanonicalizedResourceLite(requestUri, accountName));

            return canonicalizedString.Value;
        }
        private static void AddCanonicalizedResourceVersion2(Uri address, string accountName, CanonicalizedString canonicalizedString)
        {
            var element = GetCanonicalizedResourceVersion2(address, accountName);

            canonicalizedString.AppendCanonicalizedElement(element);
        }