コード例 #1
0
        /// <summary>
        /// Sorts a <see cref="WebParameterCollection"/> by name, and then value if equal.
        /// </summary>
        /// <param name="parameters">A collection of parameters to sort</param>
        /// <returns>A sorted parameter collection</returns>
        public static WebParameterCollection SortParametersExcludingSignature(WebParameterCollection parameters)
        {
            var copy       = new WebParameterCollection(parameters.Select(x => new WebParameter(x.Name, x.Value, x.Type)));
            var exclusions = copy.Where(n => string.Equals(n.Name, "oauth_signature", StringComparison.OrdinalIgnoreCase));

            copy.RemoveAll(exclusions);
            copy.ForEach(p =>
            {
                p.Name = UrlEncodeStrict(p.Name);
                if (p.Type == WebParameterType.Query)
                {
                    // Parameter provided by the user
                    p.Value = _escapeUtility.Escape(p.Value, _encoding, UrlEscapeFlags.AllowLikeWebRequest);
                }
                else
                {
                    // Authorization or POST parameter
                    p.Value = UrlEncodeStrict(p.Value);
                }
            });
            copy.Sort(
                (x, y) =>
                string.CompareOrdinal(x.Name, y.Name) != 0
            ? string.CompareOrdinal(x.Name, y.Name)
            : string.CompareOrdinal(x.Value, y.Value));
            return(copy);
        }
コード例 #2
0
        /// <summary>
        /// Sorts a <see cref="WebParameterCollection"/> by name, and then value if equal.
        /// </summary>
        /// <param name="parameters">A collection of parameters to sort</param>
        /// <returns>A sorted parameter collection</returns>
        public static WebParameterCollection SortParametersExcludingSignature(WebParameterCollection parameters)
        {
            var copy       = new WebParameterCollection(parameters);
            var exclusions = copy.Where(n => string.Equals(n.Name, "oauth_signature", StringComparison.OrdinalIgnoreCase));

            copy.RemoveAll(exclusions);
            copy.ForEach(p => { p.Name = UrlEncodeStrict(p.Name); p.Value = UrlEncodeStrict(p.Value); });
            copy.Sort(
                (x, y) =>
                string.CompareOrdinal(x.Name, y.Name) != 0
            ? string.CompareOrdinal(x.Name, y.Name)
            : string.CompareOrdinal(x.Value, y.Value));
            return(copy);
        }
コード例 #3
0
 /// <summary>
 /// Sorts a <see cref="WebParameterCollection"/> by name, and then value if equal.
 /// </summary>
 /// <param name="parameters">A collection of parameters to sort</param>
 /// <returns>A sorted parameter collection</returns>
 public static WebParameterCollection SortParametersExcludingSignature(WebParameterCollection parameters)
 {
     var copy = new WebParameterCollection(parameters.Select(x => new WebParameter(x.Name, x.Value, x.Type)));
     var exclusions = copy.Where(n => string.Equals(n.Name, "oauth_signature", StringComparison.OrdinalIgnoreCase));
     copy.RemoveAll(exclusions);
     copy.ForEach(p =>
     {
         p.Name = UrlEncodeStrict(p.Name);
         if (p.Type == WebParameterType.Query)
         {
             // Parameter provided by the user
             p.Value = _escapeUtility.Escape(p.Value, _encoding, UrlEscapeFlags.AllowLikeWebRequest);
         }
         else
         {
             // Authorization or POST parameter
             p.Value = UrlEncodeStrict(p.Value);
         }
     });
     copy.Sort(
     (x, y) =>
     string.CompareOrdinal(x.Name, y.Name) != 0
     ? string.CompareOrdinal(x.Name, y.Name)
     : string.CompareOrdinal(x.Value, y.Value));
     return copy;
 }
コード例 #4
0
 /// <summary>
 /// Sorts a <see cref="WebParameterCollection"/> by name, and then value if equal.
 /// </summary>
 /// <param name="parameters">A collection of parameters to sort</param>
 /// <returns>A sorted parameter collection</returns>
 public static WebParameterCollection SortParametersExcludingSignature(WebParameterCollection parameters)
 {
     var copy = new WebParameterCollection(parameters);
     var exclusions = copy.Where(n => string.Equals(n.Name, "oauth_signature", StringComparison.OrdinalIgnoreCase));
     copy.RemoveAll(exclusions);
     copy.ForEach(p => { p.Name = UrlEncodeStrict(p.Name); p.Value = UrlEncodeStrict(p.Value); });
     copy.Sort(
     (x, y) =>
     string.CompareOrdinal(x.Name, y.Name) != 0
     ? string.CompareOrdinal(x.Name, y.Name)
     : string.CompareOrdinal(x.Value, y.Value));
     return copy;
 }
コード例 #5
0
        private string GetAuthorizationHeader(WebParameterCollection parameters)
        {
            var sb = new StringBuilder();
            if (!string.IsNullOrEmpty(Realm))
            {
                sb.Append(string.Format("realm=\"{0}\",", OAuthTools.UrlEncodeRelaxed(Realm)));
            }

            parameters.Sort((l, r) => string.Compare(l.Name, r.Name, StringComparison.Ordinal));
            var parameterCount = 0;
            var oathParameters = parameters.Where(
                parameter => !string.IsNullOrEmpty(parameter.Name)
                             && !string.IsNullOrEmpty(parameter.Value)
                             && (parameter.Name.StartsWith("oauth_") || parameter.Name.StartsWith("x_auth_"))).ToList();
            foreach (var parameter in oathParameters)
            {
                parameterCount++;
                var format = parameterCount < oathParameters.Count ? "{0}=\"{1}\"," : "{0}=\"{1}\"";
                sb.Append(string.Format(format, parameter.Name, parameter.Value));
            }

            var authorization = sb.ToString();
            return string.Format("{0} {1}", AuthenticationMethod, authorization);
        }