public static string GenerateSignature(string URL, string method, string body, string clientId, AsymmetricAlgorithm privateKey, AuthenticationInterface auth = null) { OAuthParameters oAuthParameters = new OAuthParameters(); oAuthParameters.setOAuthConsumerKey(clientId); oAuthParameters.setOAuthNonce(OAuthUtil.GetNonce()); oAuthParameters.setOAuthTimestamp(OAuthUtil.GetTimestamp()); oAuthParameters.setOAuthSignatureMethod("RSA-SHA1"); oAuthParameters.setOAuthVersion("1.0"); if (!string.IsNullOrEmpty(body)) { string oAuthBodyHash = Util.Base64Encode(Util.Sha1Encode(body)); oAuthParameters.setOAuthBodyHash(oAuthBodyHash); } string oAuthSignature = OAuthUtil.RsaSign(OAuthUtil.GetBaseString(URL, method, oAuthParameters.getBaseParameters()), auth); oAuthParameters.setOAuthSignature(oAuthSignature); StringBuilder stringBuilder = new StringBuilder(); foreach (KeyValuePair <string, string> current in oAuthParameters.getBaseParameters()) { if (stringBuilder.Length == 0) { stringBuilder.Append(OAuthParameters.OAUTH_KEY).Append(" "); } else { stringBuilder.Append(","); } stringBuilder.Append(current.Key).Append("=\"").Append(Util.UriRfc3986(current.Value)).Append("\""); } return(stringBuilder.ToString()); }
public void SignRequest(Uri uri, IRestRequest request) { String uriString = uri.ToString(); String methodString = request.Method.ToString(); //String bodyString = (String) request.Parameters.FirstOrDefault (p => p.Type == ParameterType.RequestBody).Value; String bodyString = null; Parameter bodyParam = request.Parameters.FirstOrDefault(p => p.Type == ParameterType.RequestBody); if (methodString != "GET" && methodString != "DELETE" && methodString != "HEAD") { if (bodyParam != null) { bodyString = bodyParam.Value.ToString(); } else { bodyString = ""; } } String signature = OAuthUtil.GenerateSignature(uriString, methodString, bodyString, clientId, privateKey); request.AddHeader("Authorization", signature); }
/// <summary> /// This method generates an OAuth signature /// </summary> /// <param name="URL"></param> /// <param name="method"></param> /// <param name="body"></param> /// <returns></returns> public static String GenerateSignature(String URL, String method, String body, String clientId, AsymmetricAlgorithm privateKey) { OAuthParameters oAuthParameters = new OAuthParameters(); oAuthParameters.setOAuthConsumerKey(clientId); oAuthParameters.setOAuthNonce(OAuthUtil.GetNonce()); //oAuthParameters.setOAuthNonce("ssxECu3Ww1rURm2tM"); oAuthParameters.setOAuthTimestamp(OAuthUtil.GetTimestamp()); //oAuthParameters.setOAuthTimestamp("1466961120"); oAuthParameters.setOAuthSignatureMethod("RSA-SHA256"); oAuthParameters.setOAuthVersion("1.0"); //Console.WriteLine("body: " + body); if (body != null) { String encodedHash = Util.Base64Encode(Util.Sha256Encode(body)); //Console.WriteLine("bodyHash: " + encodedHash); oAuthParameters.setOAuthBodyHash(encodedHash); } String baseString = OAuthUtil.GetBaseString(URL, method, oAuthParameters.getBaseParameters()); //Console.WriteLine("baseString: " + baseString); String signature = RsaSign(baseString); //Console.WriteLine("signature: " + signature); oAuthParameters.setOAuthSignature(signature); StringBuilder builder = new StringBuilder(); foreach (KeyValuePair <string, string> entry in oAuthParameters.getBaseParameters()) { if (builder.Length == 0) { builder.Append(OAuthParameters.OAUTH_KEY).Append(" "); } else { builder.Append(","); } builder.Append((entry.Key)).Append("=\"").Append(Util.UriRfc3986(entry.Value)).Append("\""); } //Console.WriteLine("authKey: " + builder.ToString()); return(builder.ToString()); }
/// <summary> /// This method generates an OAuth signature /// </summary> /// <param name="URL"></param> /// <param name="method"></param> /// <param name="body"></param> /// <returns></returns> public static String GenerateSignature(String URL, String method, String body, String clientId, AsymmetricAlgorithm privateKey) { OAuthParameters oAuthParameters = new OAuthParameters(); oAuthParameters.setOAuthConsumerKey(clientId); oAuthParameters.setOAuthNonce(OAuthUtil.GetNonce()); oAuthParameters.setOAuthTimestamp(OAuthUtil.GetTimestamp()); oAuthParameters.setOAuthSignatureMethod("RSA-SHA1"); oAuthParameters.setOAuthVersion("1.0"); String encodedHash = Util.Base64Encode(Util.Sha1Encode(body)); oAuthParameters.setOAuthBodyHash(encodedHash); String baseString = OAuthUtil.GetBaseString(URL, method, oAuthParameters.getBaseParameters()); String signature = RsaSign(baseString); oAuthParameters.setOAuthSignature(signature); StringBuilder builder = new StringBuilder(); foreach (KeyValuePair <string, string> entry in oAuthParameters.getBaseParameters()) { if (builder.Length == 0) { builder.Append(OAuthParameters.OAUTH_KEY).Append(" "); } else { builder.Append(","); } builder.Append((entry.Key)).Append("=\"").Append(Util.uriRfc3986(entry.Value)).Append("\""); } return(builder.ToString()); }