private HttpWebRequest GenerateRequest(string contentType, string requestMethod) { var ts = UnixTime.ToUnixTime(DateTime.Now); //Create the needed OAuth Parameters. //Refer - http://oauth.net/core/1.0/#sig_base_example var param = new OAuthParameters() { ConsumerKey = _consumerKey, SignatureMethod = SigningProvider.SignatureMethod, Version = Constants.Version1_0, Nonce = NonceProvider.GenerateNonce(ts), Timestamp = ts.ToString(), }; //Generate Signature Hash var signatureBase = SignatureBase.Create(requestMethod.ToUpper(), _serviceProviderUri, param); //Set Signature Hash as one of the OAuth Parameter param.Signature = SigningProvider.ComputeSignature(signatureBase, _consumerSecret, null); var httpWebRequest = (HttpWebRequest)WebRequest.Create(_serviceProviderUri); httpWebRequest.Method = requestMethod; httpWebRequest.ContentType = contentType; httpWebRequest.Timeout = RequestTimeOut; //Add the OAuth Parameters to Authorization Header of Request httpWebRequest.Headers.Add(Constants.AuthorizationHeaderParameter, param.ToHeaderFormat()); return httpWebRequest; }
public object BeforeSendRequest(ref Message request, IClientChannel channel) { HttpRequestMessageProperty httpRequestProperty = request.Properties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty; NameValueCollection rawParameters = HttpUtility.ParseQueryString(request.Headers.To.Query); UriBuilder uriBuilder = new UriBuilder(request.Headers.To); uriBuilder.Query = Rfc3986.EncodeAndJoin(rawParameters); request.Headers.To = uriBuilder.Uri; OAuthConsumer consumer = new OAuthConsumer(_credentials.OAuthKey, _credentials.OAuthSecret); int unixTime = UnixTime.ToUnixTime(DateTime.UtcNow); OAuthParameters parameters = new OAuthParameters(); parameters.Version = Constants.Version1_0; parameters.SignatureMethod = "RSA-SHA1"; parameters.ConsumerKey = _credentials.OAuthKey; parameters.Token = _credentials.OAuthToken; parameters.TokenSecret = _credentials.OAuthTokenSecret; parameters.Timestamp = unixTime.ToString(); parameters.Nonce = _nonceProvider.GenerateNonce(unixTime); RsaSha1SigningProvider provider = new RsaSha1SigningProvider(); provider.PfxFile = _credentials.OAuthPfxFile; provider.PfxPassword = _credentials.OAuthPfxPassword; parameters.Sign(request.Headers.To, httpRequestProperty.Method, consumer, null, provider); httpRequestProperty.Headers["Authorization"] = parameters.ToHeaderFormat(); return null; }
/// <summary> /// /// </summary> /// <param name="requestUri"></param> /// <param name="authParameters"></param> /// <param name="httpMethod"></param> /// <param name="contentType"></param> /// <param name="bodyStream"></param> /// <returns></returns> protected virtual HttpWebRequest CreateRequest(Uri requestUri, OAuthParameters authParameters, string httpMethod, string contentType, System.IO.Stream bodyStream) { NameValueCollection requestSpecificParameters = new NameValueCollection(authParameters.AdditionalParameters); if (!this.Service.UseAuthorizationHeader) { ////The OAuth params need to be added either into the querystring or into the post body. requestSpecificParameters.Add(authParameters.OAuthRequestParams()); } if (Constants.HttpPostUrlEncodedContentTypeRegex.IsMatch(contentType) && bodyStream == null) { ////All the requestSpecificParameters need to be encoded into the body bytes string body = Rfc3986.EncodeAndJoin(requestSpecificParameters); bodyStream = new MemoryStream(Encoding.ASCII.GetBytes(body)); } else { ////They go into the querystring. string query = Rfc3986.EncodeAndJoin(requestSpecificParameters); if (!string.IsNullOrEmpty(query)) { UriBuilder mutableRequestUri = new UriBuilder(requestUri); if (string.IsNullOrEmpty(mutableRequestUri.Query)) mutableRequestUri.Query = query; else mutableRequestUri.Query = mutableRequestUri.Query.Substring(1) + "&" + query; requestUri = mutableRequestUri.Uri; } } HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUri); request.Method = httpMethod; if (this.Service.UseAuthorizationHeader) request.Headers.Add(HttpRequestHeader.Authorization, authParameters.ToHeaderFormat()); if (!String.IsNullOrEmpty(contentType)) { request.ContentType = contentType; if (bodyStream != null) { if (bodyStream.CanSeek) request.ContentLength = bodyStream.Length; StreamCopier.CopyTo(bodyStream, request.GetRequestStream()); } } return request; }