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.

102 lines
3.8 KiB

2 years ago
  1. using EasyBL.WebApi.WebApi;
  2. using EasyNet;
  3. using Entity.Sugar;
  4. using SqlSugar.Base;
  5. using System;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Web;
  9. namespace EasyBL.WebApi.Common
  10. {
  11. public class SignExtension
  12. {
  13. /// <summary>
  14. /// 驗證身份
  15. /// </summary>
  16. /// <param name="c">todo: describe c parameter on VerifyIdentity</param>
  17. /// <param name="header">todo: describe header parameter on VerifyIdentity</param>
  18. /// <returns></returns>
  19. public static bool VerifyIdentity(HttpContext c, APISoapHeader header)
  20. {
  21. var bValid = true;
  22. try
  23. {
  24. if (header == null)
  25. {
  26. var sOrgid = c.Request.Headers["orgid"];
  27. var sUserid = c.Request.Headers["userid"];
  28. var sToken = c.Request.Headers["token"];
  29. var sSignature = c.Request.Headers["signature"];
  30. if (string.IsNullOrWhiteSpace(sSignature))
  31. {
  32. var oTicket = (OTB_SYS_TicketAuth)HttpRuntimeCache.Get(sOrgid + sUserid);
  33. if (oTicket == null)
  34. {
  35. var db = SugarBase.GetIntance();
  36. oTicket = db.Queryable<OTB_SYS_TicketAuth>().Single(it => it.Token == sToken);
  37. }
  38. if (oTicket == null || oTicket.Token != sToken || (oTicket.IsVerify == "Y" && oTicket.ExpireTime < DateTime.Now))
  39. {
  40. bValid = false;
  41. }
  42. }
  43. else
  44. {
  45. if (string.IsNullOrWhiteSpace(sSignature))
  46. {
  47. bValid = false;
  48. }
  49. else
  50. {
  51. var sTimestamp = c.Request.Headers["timestamp"];
  52. var sNonce = c.Request.Headers["nonce"];
  53. bValid = SignExtension.TokenVerify(sTimestamp, sNonce, sToken, sSignature);
  54. }
  55. }
  56. }
  57. return bValid;
  58. }
  59. catch (Exception)
  60. {
  61. return false;
  62. }
  63. }
  64. /// <summary>
  65. /// 验证加密签名
  66. /// </summary>
  67. /// <param name="timeStamp">todo: describe timeStamp parameter on TokenVerify</param>
  68. /// <param name="nonce">todo: describe nonce parameter on TokenVerify</param>
  69. /// <param name="token">todo: describe token parameter on TokenVerify</param>
  70. /// <param name="signature">todo: describe signature parameter on TokenVerify</param>
  71. /// <returns></returns>
  72. public static bool TokenVerify(string timeStamp, string nonce, string token, string signature)
  73. {
  74. var hash = System.Security.Cryptography.MD5.Create();
  75. //拼接簽名數據
  76. var signStr = token + timeStamp + nonce;
  77. //將字符串中字符按升序排序
  78. var sortStr = string.Concat(signStr.OrderBy(c => c));
  79. var bytes = Encoding.UTF8.GetBytes(sortStr);
  80. //使用MD5加密
  81. var md5Val = hash.ComputeHash(bytes);
  82. //把二進制轉化為大寫的十六進制的
  83. var result = new StringBuilder();
  84. foreach (var c in md5Val)
  85. {
  86. result.Append(c.ToString("X2"));
  87. }
  88. return result.ToString().ToUpper() == signature;
  89. }
  90. // like Ivj6eZRx40MTx2ZvnG8nA
  91. public static string CreateToken()
  92. {
  93. var token = Guid.NewGuid().ToString();
  94. token = SecurityUtil.SHA256(token);
  95. return token;
  96. }
  97. }
  98. }