/// <summary> /// Parses the OAuth parameters from a HttpWebResponse. /// </summary> /// <param name="response">The Http response</param> /// <returns> /// An OAuthParameters object containing the parsed reserved OAuth parameters and any additional, valid parameters.</returns> public static OAuthParameters Parse(HttpWebResponse response) { if (response == null) { return(null); } NameValueCollection bodyParams = new NameValueCollection(); using (MemoryStream ms = new MemoryStream()) { System.IO.Stream stream = response.GetResponseStream(); byte[] buffer = new byte[32768]; int read; while ((read = stream.Read(buffer, 0, buffer.Length)) > 0) { ms.Write(buffer, 0, read); } Encoding bodyEncoding = Encoding.ASCII; if (!String.IsNullOrEmpty(response.ContentEncoding)) { bodyEncoding = Encoding.GetEncoding(response.ContentEncoding); } string responseBody = bodyEncoding.GetString(ms.ToArray()); string[] nameValuePairs = responseBody.Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries); foreach (string nameValuePair in nameValuePairs) { string[] nameValuePairParts = nameValuePair.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries); if (nameValuePairParts.Length == 2) { bodyParams.Add(HttpUtility.UrlDecode(nameValuePairParts[0]), HttpUtility.UrlDecode(nameValuePairParts[1])); } } if (bodyParams.Count == 0 && responseBody.Trim().Length > 0) { bodyParams.Add(OAuthRequestExceptionParameters.Problem, responseBody); } } return(OAuthParameters.DoParse(null, response.Headers[Constants.WwwAuthenticateHeaderParameter], bodyParams, null, OAuthParameterSources.ConsumerDefault, false)); }
/// <summary> /// Parses the OAuth parameters from a NameValueCollection /// </summary> /// <param name="parameterCollection">the NameValueCollection</param> /// <returns> /// An OAuthParameters object containing the parsed reserved OAuth parameters and any additional, valid parameters.</returns> public static OAuthParameters Parse(NameValueCollection parameterCollection) { return(OAuthParameters.DoParse(null, null, parameterCollection, null, OAuthParameterSources.HttpPostBody, true)); }
/// <summary> /// Parses the OAuth parameters from the HTTP request, sourcing /// parameters from the sources specified by <paramref name="sources"/>. /// /// TODO: Mention validation /// </summary> /// /// <param name="request">The HTTP request</param> /// <param name="sources">The sources to collate parameters from /// (must not be <c>OAuthParameterSources.None</c>)</param> /// /// <returns> /// An OAuthParameters object containing the parsed reserved OAuth /// parameters and any additional, valid parameters /// </returns> public static OAuthParameters Parse(HttpRequest request, OAuthParameterSources sources) { return(OAuthParameters.DoParse(request.Headers[Constants.AuthorizationHeaderParameter], request.Headers[Constants.WwwAuthenticateHeaderParameter], request.Form, request.QueryString, sources, true)); }