2
A
回答
3
如果是OAuth的那麼簽名是你希望的
- OAuthVersion
- OAuthNonce
- 時間戳
- ConsumerKey
- 是SignatureMethod(如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");
}
}
但檢查出http://oauth.net/core/1.0a/瞭解詳情。
+1
生成基地,然後創建哈希,這是你的簽名 – 2011-12-20 06:00:06
+0
是否有必要包含所有這些信息的組合? – 2014-10-15 12:12:35
+0
是的。必須按規範中的規定包含所有內容,因爲這是OAuth服務器將如何檢查簽名,方法是在另一端執行相同的計算。 – 2014-10-15 15:27:30
相關問題
- 1. ClickOnce簽名 - 實際簽名是什麼?
- 2. 智威湯遜。爲什麼它比oAuth更好,簽名是什麼?
- 3. Dropbox OAuth令牌
- 4. OAuth簽名
- 5. 什麼是簽名提交?
- 6. 什麼是oauth域
- 7. OAuth無效簽名
- 8. 爲什麼我無法使用OAuth連接到Dropbox?
- 9. 爲什麼Twitter返回「無法驗證oauth簽名和令牌?」
- 10. Twitter OAuth - 錯誤的簽名錯誤 - 我錯過了什麼?
- 11. 爲什麼我生成的OAuth簽名與Twitter的不同?
- 12. OAuth中的OOB是什麼?
- 13. 什麼是id_token google oauth
- 14. 「OAuth Provider」究竟是什麼?
- 15. 什麼是Twitter中的OAuth
- 16. Dropbox API中的quota_info.datastores是什麼?
- 17. 什麼是PHP中的參數簽名?
- 18. 什麼是`auto classMemberFunction() - > void {}`簽名?
- 19. 簽名過程的網址是什麼?
- 20. .F4V的文件簽名是什麼?
- 21. 什麼是...在方法簽名
- 22. 什麼是運行時簽名?
- 23. java中的jar簽名是什麼?
- 24. 什麼是::在簽名中的含義?
- 25. 什麼是代碼簽名身份?
- 26. 在Windows中籤名exe是什麼?
- 27. BillingSecurity.verifyPurchase - 什麼是signedData和簽名?
- 28. TwitterApiClient:什麼是POST的回調簽名?
- 29. connect/expressjs中的「簽名」cookie是什麼?
- 30. 什麼是JCE代碼簽名CA?
如果你仍然有問題可能考慮使用現有的庫,如DropNet? (http://dkdevelopment.net/what-im-doing/dropnet/) – dkarzon 2012-01-03 03:14:19
最後,我決定去開發定義的OAuth庫 – 2012-01-05 18:04:22