AppKeyController.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using System;
  2. using System.Linq;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. using Lottomat.Application.Entity.SystemManage;
  6. using Lottomat.Application.Busines.SystemManage;
  7. using Lottomat.Util;
  8. using Lottomat.Util.WebControl;
  9. using System.Web.Mvc;
  10. using Lottomat.Utils.Security;
  11. namespace Lottomat.Application.Admin.Areas.SystemManage.Controllers
  12. {
  13. /// <summary>
  14. /// 版 本 1.0
  15. /// Copyright (c) 2016-2017
  16. /// 创 建:超级管理员
  17. /// 日 期:2017-10-25 16:17
  18. /// 描 述:系统接口密钥管理
  19. /// </summary>
  20. public class AppKeyController : MvcControllerBase
  21. {
  22. private AppKeyBLL appkeybll = new AppKeyBLL();
  23. #region 视图功能
  24. /// <summary>
  25. /// 列表页面
  26. /// </summary>
  27. /// <returns></returns>
  28. [HttpGet]
  29. public ActionResult Index()
  30. {
  31. return View();
  32. }
  33. /// <summary>
  34. /// 表单页面
  35. /// </summary>
  36. /// <returns></returns>
  37. [HttpGet]
  38. public ActionResult Form()
  39. {
  40. return View();
  41. }
  42. #endregion
  43. #region 获取数据
  44. /// <summary>
  45. /// 获取列表
  46. /// </summary>
  47. /// <param name="pagination">分页参数</param>
  48. /// <param name="queryJson">查询参数</param>
  49. /// <returns>返回分页列表Json</returns>
  50. [HttpGet]
  51. public ActionResult GetPageListJson(Pagination pagination, string queryJson)
  52. {
  53. var watch = CommonHelper.TimerStart();
  54. var data = appkeybll.GetPageList(pagination, queryJson);
  55. var jsonData = new
  56. {
  57. rows = data,
  58. total = pagination.total,
  59. page = pagination.page,
  60. records = pagination.records,
  61. costtime = CommonHelper.TimerEnd(watch)
  62. };
  63. return ToJsonResult(jsonData);
  64. }
  65. /// <summary>
  66. /// 获取列表
  67. /// </summary>
  68. /// <param name="queryJson">查询参数</param>
  69. /// <returns>返回列表Json</returns>
  70. [HttpGet]
  71. public ActionResult GetListJson(string queryJson)
  72. {
  73. var data = appkeybll.GetList(queryJson);
  74. return ToJsonResult(data);
  75. }
  76. /// <summary>
  77. /// 获取实体
  78. /// </summary>
  79. /// <param name="keyValue">主键值</param>
  80. /// <returns>返回对象Json</returns>
  81. [HttpGet]
  82. public ActionResult GetFormJson(string keyValue)
  83. {
  84. var data = appkeybll.GetEntity(keyValue);
  85. return ToJsonResult(data);
  86. }
  87. /// <summary>
  88. /// 获取AppKey和校验密钥
  89. /// </summary>
  90. /// <returns></returns>
  91. [HttpGet]
  92. public ActionResult GetAppKey()
  93. {
  94. string[] res = GetSignToken();
  95. var obj = new
  96. {
  97. AppKey = res[0],
  98. AppSecret = res[1]
  99. };
  100. return ToJsonResult(obj);
  101. }
  102. #endregion
  103. #region 提交数据
  104. /// <summary>
  105. /// 删除数据
  106. /// </summary>
  107. /// <param name="keyValue">主键值</param>
  108. /// <returns></returns>
  109. [HttpPost]
  110. [ValidateAntiForgeryToken]
  111. [AjaxOnly]
  112. public ActionResult RemoveForm(string keyValue)
  113. {
  114. appkeybll.RemoveForm(keyValue);
  115. return Success("删除成功。");
  116. }
  117. /// <summary>
  118. /// 保存表单(新增、修改)
  119. /// </summary>
  120. /// <param name="keyValue">主键值</param>
  121. /// <param name="entity">实体对象</param>
  122. /// <returns></returns>
  123. [HttpPost]
  124. [ValidateAntiForgeryToken]
  125. [AjaxOnly]
  126. public ActionResult SaveForm(string keyValue, AppKeyEntity entity)
  127. {
  128. appkeybll.SaveForm(keyValue, entity);
  129. return Success("操作成功。");
  130. }
  131. #endregion
  132. #region 私有方法
  133. /// <summary>
  134. /// 生成AppKey
  135. /// </summary>
  136. /// <returns></returns>
  137. private string[] GetSignToken()
  138. {
  139. //签名信息
  140. string tokenStr = CommonHelper.GetGuid();
  141. //密钥
  142. string tokenKey = CommonHelper.GetGuid();
  143. //加密处理
  144. string first = ToBase64Hmac(tokenStr, tokenKey);
  145. //AppKey
  146. string last = DESEncrypt.Encrypt(Md5Helper.MD5(first, 32)).ToUpper();
  147. //生成校验密钥
  148. string check = CommonHelper.GetGuid();
  149. //降序排序
  150. string o = (last + check).ToUpper();
  151. string temp = string.Concat(o.OrderByDescending(c => c));
  152. //得到密钥
  153. string sec = DESEncrypt.Encrypt(Md5Helper.MD5(temp, 16)).ToUpper();
  154. return new[] { last, sec };
  155. }
  156. /// <summary>
  157. /// HMACSHA1算法加密并返回ToBase64String
  158. /// </summary>
  159. /// <param name="strText">签名参数字符串</param>
  160. /// <param name="strKey">密钥参数</param>
  161. /// <returns>返回一个签名值(即哈希值)</returns>
  162. private static string ToBase64Hmac(string strText, string strKey)
  163. {
  164. HMACSHA1 hmacsha1 = new HMACSHA1(Encoding.UTF8.GetBytes(strKey), true);
  165. byte[] byteText = hmacsha1.ComputeHash(Encoding.UTF8.GetBytes(strText));
  166. //ES+TPCa+UT+Sb8PORoIT36M63fs=
  167. string res = System.Convert.ToBase64String(byteText, Base64FormattingOptions.None).ToUpper();
  168. return res;
  169. }
  170. #endregion
  171. }
  172. }