Açıklama Yok

WeiBoHelper.cs 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace CallCenter.Utility
  8. {
  9. public class WeiBoHelper
  10. {
  11. public static string CallBackUrl; //成功授权后的回调地址
  12. public static string AppKey; //应用的APPID
  13. public static string AppSercet; //应用的APPKEY
  14. public static string AccessToken; //应用的access_token
  15. /// <summary>
  16. /// 获取配置
  17. /// </summary>
  18. public WeiBoHelper()
  19. {
  20. CallBackUrl = Configs.GetValue("WeiBoCallBackUrl");
  21. AppKey = Configs.GetValue("WeiBoAppKey");
  22. AppSercet = Configs.GetValue("WeiBoAppSecret");
  23. AccessToken = Configs.GetValue("WeiBoAccessToken");
  24. }
  25. #region SHA1加密
  26. /// <summary>
  27. /// SHA1加密 (HASH算法没有解密)
  28. /// </summary>
  29. /// <param name="Source_String"></param>
  30. /// <returns></returns>
  31. public static string SHA1_Encrypt(string Source_String)
  32. {
  33. byte[] StrRes = Encoding.Default.GetBytes(Source_String);
  34. HashAlgorithm iSHA = new SHA1CryptoServiceProvider();
  35. StrRes = iSHA.ComputeHash(StrRes);
  36. StringBuilder EnText = new StringBuilder();
  37. foreach (byte iByte in StrRes)
  38. {
  39. EnText.AppendFormat("{0:x2}", iByte);
  40. }
  41. return EnText.ToString();
  42. }
  43. #endregion
  44. /// <summary>
  45. /// 返回值是echostr的值
  46. /// </summary>
  47. /// <param name="signature"></param>
  48. /// <param name="nonce"></param>
  49. /// <param name="timestamp"></param>
  50. /// <returns></returns>
  51. public static bool ValidateSHA(string signature, string nonce, string timestamp)
  52. {
  53. string App_Sercet = Configs.GetValue("WeiBoAppSecret");
  54. //SHA1Encrypt sha1 = new SHA1Encrypt();
  55. if (signature == null || nonce == null || timestamp == null)
  56. {
  57. return false;
  58. }
  59. string sign = SHA1_Encrypt(getSignContent(nonce, timestamp, App_Sercet));
  60. if (!signature.Equals(sign))
  61. {
  62. return false;
  63. }
  64. return true;
  65. }
  66. /// <summary>
  67. /// 字典顺序升序构造签名串
  68. /// </summary>
  69. /// <param name="nonce"></param>
  70. /// <param name="timestamp"></param>
  71. /// <param name="app_secret"></param>
  72. /// <returns></returns>
  73. public static string getSignContent(string nonce, string timestamp, string app_secret)
  74. {
  75. Dictionary<string, string> dics = new Dictionary<string, string>();
  76. dics.Add("nonce", nonce);
  77. dics.Add("timestamp", timestamp);
  78. dics.Add("app_secret", app_secret);
  79. var vDic = (from objDic in dics orderby objDic.Key descending select objDic);
  80. StringBuilder str = new StringBuilder();
  81. foreach (KeyValuePair<string, string> kv in vDic)
  82. {
  83. string pvalue = kv.Value;
  84. str.Append(pvalue + "");
  85. }
  86. string result = str.ToString();
  87. return result;
  88. }
  89. }
  90. }