C# 投递箱中有什么OAuth签名?
我正在为DropBox API开发一个应用程序,在我的一生中都找不到任何关于身份验证期间第一个令牌请求的“签名”的信息C# 投递箱中有什么OAuth签名?,c#,oauth,dropbox,C#,Oauth,Dropbox,我正在为DropBox API开发一个应用程序,在我的一生中都找不到任何关于身份验证期间第一个令牌请求的“签名”的信息 其他人知道吗?如果是OAuth,则签名是 OAuthVersion 非统币 时间戳 消费主义 签名方法(例如HMACSHA1) 您希望发送的任何参数 HTTP方法 例如,这里有一些代码 /// <summary> /// Generate the signature base that is used to produce the signatu
其他人知道吗?如果是OAuth,则签名是
- OAuthVersion
- 非统币
- 时间戳
- 消费主义
- 签名方法(例如HMACSHA1)
- 您希望发送的任何参数
- HTTP方法
/// <summary>
/// Generate the signature base that is used to produce the signature
/// </summary>
/// <param name="url">The full url that needs to be signed including its non OAuth url parameters</param>
/// <param name="consumerKey">The consumer key</param>
/// <param name="token">The token, if available. If not available pass null or an empty string</param>
/// <param name="tokenSecret">The token secret, if available. If not available pass null or an empty string</param>
/// <param name="httpMethod">The http method used. Must be a valid HTTP method verb (POST,GET,PUT, etc)</param>
/// <param name="signatureType">The signature type. To use the default values use <see cref="OAuthBase.SignatureTypes">OAuthBase.SignatureTypes</see>.</param>
/// <returns>The signature base</returns>
public static string GenerateSignatureBase(Uri url, string consumerKey, string token, string tokenSecret, string httpMethod, string timeStamp, string nonce, string signatureType, SortedList<String, String> customParameters, out string normalizedUrl, out string normalizedRequestParameters)
{
if (token == null)
{
token = string.Empty;
}
if (tokenSecret == null)
{
tokenSecret = string.Empty;
}
if (string.IsNullOrEmpty(consumerKey))
{
throw new ArgumentNullException("consumerKey");
}
if (string.IsNullOrEmpty(httpMethod))
{
throw new ArgumentNullException("httpMethod");
}
if (string.IsNullOrEmpty(signatureType))
{
throw new ArgumentNullException("signatureType");
}
normalizedUrl = null;
normalizedRequestParameters = null;
List<QueryParameter> parameters = GetQueryParameters(url.Query);
parameters.Add(new QueryParameter(OAuthVersionKey, OAuthVersion));
parameters.Add(new QueryParameter(OAuthNonceKey, nonce));
parameters.Add(new QueryParameter(OAuthTimestampKey, timeStamp));
parameters.Add(new QueryParameter(OAuthSignatureMethodKey, signatureType));
parameters.Add(new QueryParameter(OAuthConsumerKeyKey, consumerKey));
foreach (String key in customParameters.Keys)
{
parameters.Add(new QueryParameter(key, UrlEncode(customParameters[key])));
}
if (!string.IsNullOrEmpty(token))
{
parameters.Add(new QueryParameter(OAuthTokenKey, token));
}
parameters.Sort(new QueryParameterComparer());
normalizedUrl = string.Format("{0}://{1}", url.Scheme, url.Host);
if (!((url.Scheme == "http" && url.Port == 80) || (url.Scheme == "https" && url.Port == 443)))
{
normalizedUrl += ":" + url.Port;
}
normalizedUrl += url.AbsolutePath;
normalizedRequestParameters = NormalizeRequestParameters(parameters);
StringBuilder signatureBase = new StringBuilder();
signatureBase.AppendFormat("{0}&", httpMethod.ToUpper());
signatureBase.AppendFormat("{0}&", UrlEncode(normalizedUrl));
signatureBase.AppendFormat("{0}", UrlEncode(normalizedRequestParameters));
return signatureBase.ToString();
}
/// <summary>
/// Generate the signature value based on the given signature base and hash algorithm
/// </summary>
/// <param name="signatureBase">The signature based as produced by the GenerateSignatureBase method or by any other means</param>
/// <param name="hash">The hash algorithm used to perform the hashing. If the hashing algorithm requires initialization or a key it should be set prior to calling this method</param>
/// <returns>A base64 string of the hash value</returns>
public static string GenerateSignatureUsingHash(string signatureBase, HashAlgorithm hash)
{
return ComputeHash(hash, signatureBase);
}
/// <summary>
/// Helper function to compute a hash value
/// </summary>
/// <param name="hashAlgorithm">The hashing algoirhtm used. If that algorithm needs some initialization, like HMAC and its derivatives, they should be initialized prior to passing it to this function</param>
/// <param name="data">The data to hash</param>
/// <returns>a Base64 string of the hash value</returns>
private static string ComputeHash(HashAlgorithm hashAlgorithm, string data)
{
if (hashAlgorithm == null)
{
throw new ArgumentNullException("hashAlgorithm");
}
if (string.IsNullOrEmpty(data))
{
throw new ArgumentNullException("data");
}
byte[] dataBuffer = System.Text.Encoding.ASCII.GetBytes(data);
byte[] hashBytes = hashAlgorithm.ComputeHash(dataBuffer);
return Convert.ToBase64String(hashBytes);
}
/// <summary>
/// Generates a signature using the specified signatureType
/// </summary>
/// <param name="url">The full url that needs to be signed including its non OAuth url parameters</param>
/// <param name="consumerKey">The consumer key</param>
/// <param name="consumerSecret">The consumer seceret</param>
/// <param name="token">The token, if available. If not available pass null or an empty string</param>
/// <param name="tokenSecret">The token secret, if available. If not available pass null or an empty string</param>
/// <param name="httpMethod">The http method used. Must be a valid HTTP method verb (POST,GET,PUT, etc)</param>
/// <param name="signatureType">The type of signature to use</param>
/// <returns>A base64 string of the hash value</returns>
public static string GenerateSignature(Uri url, string consumerKey, string consumerSecret, string token, string tokenSecret, string httpMethod, string timeStamp, string nonce, SignatureTypes signatureType, SortedList<String, String> parameters, out string normalizedUrl, out string normalizedRequestParameters)
{
normalizedUrl = null;
normalizedRequestParameters = null;
switch (signatureType)
{
case SignatureTypes.PLAINTEXT:
return HttpUtility.UrlEncode(string.Format("{0}&{1}", consumerSecret, tokenSecret));
case SignatureTypes.HMACSHA1:
string signatureBase = GenerateSignatureBase(url, consumerKey, token, tokenSecret, httpMethod, timeStamp, nonce, HMACSHA1SignatureType, parameters, out normalizedUrl, out normalizedRequestParameters);
HMACSHA1 hmacsha1 = new HMACSHA1();
hmacsha1.Key = Encoding.ASCII.GetBytes(string.Format("{0}&{1}", UrlEncode(consumerSecret), string.IsNullOrEmpty(tokenSecret) ? "" : UrlEncode(tokenSecret)));
return GenerateSignatureUsingHash(signatureBase, hmacsha1);
case SignatureTypes.RSASHA1:
throw new NotImplementedException();
default:
throw new ArgumentException("Unknown signature type", "signatureType");
}
}
//
///生成用于生成签名的签名库
///
///需要签名的完整url,包括其非OAuth url参数
///消费者密钥
///令牌(如果可用)。如果不可用,请传递null或空字符串
///令牌密钥(如果可用)。如果不可用,请传递null或空字符串
///使用的http方法。必须是有效的HTTP方法谓词(POST、GET、PUT等)
///签名类型。要使用默认值,请使用OAuthBase.SignatureType。
///签名库
公共静态字符串GenerateSignatureBase(Uri url、字符串使用者、字符串令牌、字符串令牌机密、字符串httpMethod、字符串时间戳、字符串nonce、字符串signatureType、SortedList customParameters、out string normalizedUrl、out string normalizedRequestParameters)
{
if(标记==null)
{
token=string.Empty;
}
if(tokenSecret==null)
{
tokenSecret=string.Empty;
}
if(string.IsNullOrEmpty(consumerKey))
{
抛出新的ArgumentNullException(“consumerKey”);
}
if(string.IsNullOrEmpty(httpMethod))
{
抛出新ArgumentNullException(“httpMethod”);
}
if(string.IsNullOrEmpty(signatureType))
{
抛出新ArgumentNullException(“signatureType”);
}
normalizedUrl=null;
normalizedRequestParameters=null;
列表参数=GetQueryParameters(url.Query);
Add(新的QueryParameter(OAuthVersionKey,OAuthVersion));
Add(新的查询参数(OAuthNonceKey,nonce));
Add(新的QueryParameter(OAuthTimestampKey,timeStamp));
添加(新的查询参数(OAuthSignatureMethodKey,signatureType));
添加(新的查询参数(OAuthConsumerKey,consumerKey));
foreach(customParameters.Keys中的字符串键)
{
添加(新的QueryParameter(key,UrlEncode(customParameters[key]));
}
如果(!string.IsNullOrEmpty(令牌))
{
Add(新的QueryParameter(OAuthTokenKey,token));
}
Sort(新的QueryParameterComparer());
normalizedUrl=string.Format(“{0}://{1}”,url.Scheme,url.Host);
如果(!((url.Scheme==“http”和&url.Port==80)| |(url.Scheme==“https”和&url.Port==443)))
{
normalizedUrl+=“:”+url.Port;
}
normalizedUrl+=url.AbsolutePath;
normalizedRequestParameters=normalizerRequestParameters(参数);
StringBuilder signatureBase=新建StringBuilder();
AppendFormat(“{0}&”,httpMethod.ToUpper());
AppendFormat(“{0}&”,UrlEncode(normalizedUrl));
AppendFormat(“{0}”,UrlEncode(normalizedRequestParameters));
返回signatureBase.ToString();
}
///
///根据给定的签名基和哈希算法生成签名值
///
///基于GenerateSignatureBase方法或任何其他方法生成的签名
///用于执行哈希运算的哈希算法。如果散列算法需要初始化或密钥,则应在调用此方法之前进行设置
///哈希值的base64字符串
公共静态字符串GenerateSignatureUsingHash(字符串签名库,HashAlgorithm hash)
{
返回ComputeHash(哈希、签名库);
}
///
///用于计算哈希值的Helper函数
///
///使用的哈希算法。如果该算法需要一些初始化,如HMAC及其派生,则应在将其传递给此函数之前对其进行初始化
///要散列的数据
///哈希值的Base64字符串
私有静态字符串计算哈希(哈希算法,字符串数据)
{
if(hashAlgorithm==null)
{
抛出新的ArgumentNullException(“hashAlgorithm”);
}
if(string.IsNullOrEmpty(数据))
{
抛出新的异常(“数据”);
}
byte[]dataBuffer=System.Text.Encoding.ASCII.GetBytes(数据);
byte[]hashBytes=hashAlgorithm.ComputeHash(dataBuffer);
返回Convert.ToBase64String(hashBytes);
}
///
///使用指定的signatureType生成签名
///
///需要签名的完整url,包括其非OAuth url参数
///消费者密钥
///消费者安全
///令牌(如果可用)。如果不可用,请传递null或空字符串
///令牌密钥(如果可用)。如果不可用,请传递null或空字符串
///使用的http方法。必须是有效的HTTP方法谓词(POST、GET、PUT等)
///要使用的签名类型
///哈希值的base64字符串
公共静态字符串GenerateSignature(Uri url、字符串consumerKey、字符串ConsumerCret、字符串令牌、字符串令牌Secret、字符串httpMethod、字符串时间戳、字符串nonce、signatureType signatureType、SortedList参数、输出字符串normalizedUrl、输出字符串normalizedRequestParameters)
{
normalizedUrl=null;
normalizedRequestParameters=null;
开关(signatureType)
{
case signatureType.PLAINTEXT:
返回HttpUtility.UrlEncode(string.Form