/// <summary> /// Gets base string of the signature for current request (OAuth 1.0). /// </summary> /// <remarks><para>For more details, please visit <see href="http://tools.ietf.org/html/rfc5849#section-3.4.1.1"/></para></remarks> public static string GetSignatureBaseString(string httpMethod, Uri url, NameValueCollection parameters, OAuthAuthorization authorization) { if (String.IsNullOrEmpty(httpMethod)) { throw new ArgumentNullException("httpMethod"); } if (authorization == null) { throw new ArgumentNullException("authorization"); } if (url == null) { throw new ArgumentNullException("url"); } if (parameters == null) { parameters = new NameValueCollection(); } // append the authorization headers foreach (var itm in authorization.Parameters) { if (itm.Key.Equals("oauth_signature", StringComparison.OrdinalIgnoreCase)) { continue; } parameters.Add(itm.Key, itm.Value.ToString()); } // append the query parameters string queryString = url.GetComponents(UriComponents.Query, UriFormat.Unescaped); if (!String.IsNullOrEmpty(queryString)) { foreach (string q in queryString.Split('&')) { string[] p = q.Split('='); string key = p.First(), value = (p.Length > 1 ? p.Last() : ""); parameters.Add(key, value); } } // sorting and build base string of the signature StringBuilder signBaseString = new StringBuilder(); foreach (var itm in parameters.Sort().ToKeyValuePairCollection()) { //if (itm.Key.Equals("oauth_verifier", StringComparison.OrdinalIgnoreCase)) { continue; } if (signBaseString.Length > 0) { signBaseString.Append(OAuthUtility.UrlEncode("&")); } signBaseString.Append(OAuthUtility.UrlEncode(String.Format("{0}={1}", itm.Key, OAuthUtility.UrlEncode(itm.Value)))); } signBaseString.Insert(0, String.Format("{0}&{1}&", httpMethod.ToUpper(), OAuthUtility.UrlEncode(url.ToString()))); return(signBaseString.ToString()); }
/// <summary> /// Returns OAuth string of the current object for Authorization header. /// </summary> public override string ToString() { StringBuilder result = new StringBuilder(); if (this.Value.HasValue) { if (this.Value.Count > 0) { foreach (var itm in this.ParametersSorted) { if (result.Length > 0) { result.Append(", "); } result.AppendFormat("{0}=\"{1}\"", itm.Key, OAuthUtility.UrlEncode(itm.Value.ToString())); } } else { result.Append(this.Value.ToString()); } } result.Insert(0, this.AuthorizationType.ToString() + " "); return(result.ToString()); }
/// <summary> /// Returns an encoded string of the value. /// </summary> public string ToEncodedString(UrlEncodingType encodingType) { if (this.Value == null) { return(null); } return(OAuthUtility.UrlEncode(this.ToString(), encodingType)); }
/// <summary> /// Initializes a new instance of the <see cref="OAuthRequestToken"/> class. /// </summary> /// <param name="result">The request result.</param> /// <param name="authorizeUrl">The address of the authorization.</param> /// <param name="parameters">The query parameters. Will be used in the formation of the authorization address.</param> public OAuthRequestToken(RequestResult result, string authorizeUrl, NameValueCollection parameters) : base(result) { _OAuthToken = base["oauth_token"].ToString(); _OAuthTokenSecret = base["oauth_token_secret"].ToString(); _OAuthCallbackConfirmed = Convert.ToBoolean(base["oauth_callback_confirmed"]); _AuthorizationUrl = authorizeUrl; _AuthorizationUrl += _AuthorizationUrl.EndsWith("?") ? "&" : "?"; _AuthorizationUrl += String.Format("oauth_token={0}", OAuthUtility.UrlEncode(_OAuthToken)); if (parameters != null && parameters.Count > 0) { _AuthorizationUrl += "&" + parameters.ToParametersString("&"); } }
/// <summary> /// Returns OAuth string of the current object for Authorization header. /// </summary> public override string ToString() { StringBuilder result = new StringBuilder(); foreach (var itm in this.ParametersSorted) { if (result.Length > 0) { result.Append(", "); } result.AppendFormat("{0}=\"{1}\"", itm.Key, OAuthUtility.UrlEncode(itm.Value.ToString())); } result.Insert(0, "OAuth "); return(result.ToString()); }
/// <summary> /// Returns an encoded string of the value. /// </summary> public string ToEncodedString(UrlEncodingType encodingType) { if (this.Value == null) { return(null); } if (encodingType == UrlEncodingType.Default) { return(HttpUtility.UrlEncode(this.ToString())); } else if (encodingType == UrlEncodingType.PercentEncoding) { return(OAuthUtility.UrlEncode(this.ToString())); } else { return(this.ToString()); } }
/// <summary> /// Encodes a <b>URL</b> string using the specified encoding object. /// </summary> /// <param name="value">The text to encode.</param> /// <param name="encodingType">The type of the encoder.</param> public static string UrlEncode(string value, UrlEncodingType encodingType) { return(OAuthUtility.UrlEncode(value, encodingType, Encoding.UTF8)); }
/// <summary> /// Percent encoding. /// </summary> /// <param name="value">The text to encode.</param> public static string UrlEncode(string value) { return(OAuthUtility.UrlEncode(value, UrlEncodingType.PercentEncoding, Encoding.UTF8)); }