private string GenerateSignature(Uri uri, MethodType methodType, Token token, IEnumerable<Parameter> parameters) { var hmacKeyBase = ConsumerSecret.UrlEncode() + "&" + ((token == null) ? "" : token.Secret).UrlEncode(); using (var hmacsha1 = new HMACSHA1(Encoding.UTF8.GetBytes(hmacKeyBase))) { var stringParameter = parameters.OrderBy(p => p.Key) .ThenBy(p => p.Value) .ToQueryParameter(); var signatureBase = methodType.ToUpperString() + "&" + uri.GetComponents(UriComponents.SchemeAndServer | UriComponents.Path, UriFormat.Unescaped).UrlEncode() + "&" + stringParameter.UrlEncode(); var hash = hmacsha1.ComputeHash(Encoding.UTF8.GetBytes(signatureBase)); return Convert.ToBase64String(hash).UrlEncode(); } }
protected ParameterCollection ConstructBasicParameters(string url, MethodType methodType, Token token, IEnumerable<Parameter> optionalParameters) { Guard.ArgumentNull(url, "url"); Guard.ArgumentNull(methodType, "methodType"); Guard.ArgumentNull(optionalParameters, "optionalParameters"); var parameters = new ParameterCollection { { "oauth_consumer_key", ConsumerKey }, { "oauth_nonce", random.Next() }, { "oauth_timestamp", DateTime.UtcNow.ToUnixTime() }, { "oauth_signature_method", "HMAC-SHA1" }, { "oauth_version", "1.0" } }; if (token != null) parameters.Add("oauth_token", token.Key); var signature = GenerateSignature(new Uri(url), methodType, token, parameters.Concat(optionalParameters)); parameters.Add("oauth_signature", signature); return parameters; }
protected ParameterCollection ConstructBasicParameters(string url, MethodType methodType, Token token = null, params Parameter[] optionalParameters) { Guard.ArgumentNull(optionalParameters, "optionalParameters"); return ConstructBasicParameters(url, methodType, token, optionalParameters.AsEnumerable()); }