You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.5KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Security.Cryptography;
  7. namespace TelpoPush.Common
  8. {
  9. public class Utils
  10. {
  11. public static MultipartFormDataContent GetMultipartFormDataContent(Dictionary<string, object> dic, string appId, ref Dictionary<string, object> outDic)
  12. {
  13. MultipartFormDataContent mfdc = new MultipartFormDataContent();
  14. StringBuilder sb = new StringBuilder();
  15. if (dic != null && dic.Count > 0)
  16. {
  17. var dicOrderBy = dic.OrderBy(z => z.Key);
  18. foreach (KeyValuePair<string, object> kv in dicOrderBy)
  19. {
  20. sb.Append($"{kv.Key}={kv.Value.ToString()}&");
  21. mfdc.Add(new StringContent(kv.Value.ToString()), kv.Key);//参数, 内容在前,参数名称在后
  22. }
  23. }
  24. string signStr = $"{sb.ToString().Trim('&')}{appId}";
  25. byte[] bytes = Encoding.UTF8.GetBytes(signStr);
  26. byte[] hash = SHA256.Create().ComputeHash(bytes);
  27. StringBuilder builder = new StringBuilder();
  28. for (int i = 0; i < hash.Length; i++)
  29. {
  30. builder.Append(hash[i].ToString("X2"));
  31. }
  32. string sign = builder.ToString().ToLower();
  33. dic.Add("sign", sign);
  34. mfdc.Add(new StringContent(sign), "sign");//参数, 内容在前,参数名称在后
  35. outDic = dic;
  36. return mfdc;
  37. }
  38. }
  39. }