UsersController.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.AspNetCore.Http;
  6. using Microsoft.Extensions.Logging;
  7. using Microsoft.Extensions.Options;
  8. using Microsoft.AspNetCore.Authorization;
  9. using System.Web;
  10. using MySystem.Models;
  11. using LitJson;
  12. using Library;
  13. namespace MySystem.Areas.Api.Controllers.v1
  14. {
  15. [Area("Api")]
  16. [Route("Api/v1/[controller]/[action]")]
  17. public class UsersController : BaseController
  18. {
  19. public UsersController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
  20. {
  21. }
  22. #region 通用-获取Token
  23. // [Authorize]
  24. public JsonResult GetToken(string value)
  25. {
  26. // value = DesDecrypt(value);
  27. JsonData data = JsonMapper.ToObject(value);
  28. string AppId = data["AppId"].ToString(); //AppId
  29. string AppSecret = data["AppSecret"].ToString(); //AppSecret
  30. Dictionary<string, object> Obj = new Dictionary<string, object>();
  31. Users query = db.Users.FirstOrDefault(m => m.AppId == AppId && m.AppSecret == AppSecret);
  32. if(query != null)
  33. {
  34. Obj.Add("Token", function.MD5_32(AppId + "G9&E@952")); //用户令牌
  35. }
  36. else
  37. {
  38. return Json(new AppResultJson() { Status = "-1", Info = "获取失败,AppId或AppSecret有误", Data = Obj });
  39. }
  40. return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
  41. }
  42. #endregion
  43. #region 检查签名是否合法,合法返回1,不合法返回提示信息
  44. /// <summary>
  45. /// 检查签名是否合法,合法返回1,不合法返回提示信息
  46. /// </summary>
  47. /// <param name="value">请求的参数(json字符串)</param>
  48. /// <param name="signField">要签名的字段</param>
  49. /// <returns></returns>
  50. private string CheckSign(string value, string[] signField)
  51. {
  52. JsonData json = JsonMapper.ToObject(value);
  53. Dictionary<string, string> dic = new Dictionary<string, string>();
  54. for (int i = 0; i < signField.Length; i++)
  55. {
  56. dic.Add(signField[i], json[signField[i]].ToString());
  57. }
  58. string sign = json["sign"].ToString(); //客户端签名字符串
  59. return new Sign().sign(dic, sign);
  60. }
  61. #endregion
  62. }
  63. }