AuthorizationFilter.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System.Text;
  2. using System.Web;
  3. using Common;
  4. using Extensions;
  5. using Infrastructure;
  6. using Infrastructure.Model;
  7. using Microsoft.AspNetCore.Mvc;
  8. using Microsoft.AspNetCore.Mvc.Filters;
  9. using Services;
  10. //本命名空间暂时先不改,改动比较大2023年9月2日
  11. namespace Filters
  12. {
  13. /// <summary>
  14. /// </summary>
  15. public class AuthorizationFilter : IAuthorizationFilter
  16. {
  17. public AuthorizationFilter()
  18. {
  19. }
  20. /// <summary>
  21. /// </summary>
  22. /// <param name="context"></param>
  23. public void OnAuthorization(AuthorizationFilterContext context)
  24. {
  25. var request = context.HttpContext.Request;
  26. if(!request.Path.Value.ToLower().Contains("noauth/"))
  27. {
  28. string content = "";
  29. if(context.HttpContext.Request.Method.ToLower() == "get")
  30. {
  31. content = context.HttpContext.GetQueryString();
  32. content = GetParam(content, "value");
  33. content = HttpUtility.UrlDecode(content);
  34. content = Decrypt(content);
  35. content = HttpUtility.UrlDecode(content);
  36. if(!string.IsNullOrEmpty(content))
  37. {
  38. Dictionary<string, object> dic = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(content);
  39. string queryString = "";
  40. var parameters = context.ActionDescriptor.Parameters;
  41. foreach(var parameter in parameters)
  42. {
  43. string parameterName = parameter.Name;
  44. Type objectType = parameter.ParameterType;
  45. if(objectType.FullName != "System.String")
  46. {
  47. System.Reflection.Assembly assembly = System.Reflection.Assembly.GetAssembly(objectType);
  48. var entry = assembly.CreateInstance(objectType.FullName);
  49. Type type = entry.GetType();
  50. System.Reflection.PropertyInfo[] propertyInfos = type.GetProperties();
  51. for (int i = 0; i < propertyInfos.Length; i++)
  52. {
  53. foreach (string key in dic.Keys)
  54. {
  55. if (propertyInfos[i].Name == key)
  56. {
  57. object value = dic[key];
  58. string ParameterType = propertyInfos[i].GetMethod.ReturnParameter.ParameterType.Name;
  59. if (ParameterType == "Int32")
  60. {
  61. if(value == null || value == "") value = "0";
  62. value = Convert.ToInt32(value);
  63. }
  64. else if (ParameterType == "Int64[]")
  65. {
  66. value = Tools.SpitLongArrary(Newtonsoft.Json.JsonConvert.SerializeObject(value).Replace("[", "").Replace("]", "").Trim('"'), ',');
  67. }
  68. else if (ParameterType == "Int32[]")
  69. {
  70. value = Tools.SpitIntArrary(Newtonsoft.Json.JsonConvert.SerializeObject(value).Replace("[", "").Replace("]", "").Trim('"'), ',');
  71. }
  72. else if (ParameterType == "List`1")
  73. {
  74. string val = Newtonsoft.Json.JsonConvert.SerializeObject(value).Replace("[", "").Replace("]", "").Trim('"');
  75. value = Tools.SpitLongArrary(val, ',').ToList();
  76. }
  77. if(value.ToString() == "-1") value = -1;
  78. if(value.ToString() == "[]") value = "";
  79. queryString += key + "=" + value.ToString() + "&";
  80. break;
  81. }
  82. }
  83. }
  84. }
  85. }
  86. request.QueryString = new QueryString("?" + queryString.TrimEnd('&'));
  87. }
  88. }
  89. else if(context.HttpContext.Request.Method.ToLower() == "delete")
  90. {
  91. string path = request.Path.Value;
  92. string value = path.Substring(path.LastIndexOf("/") + 1);
  93. path = path.Substring(0, path.LastIndexOf("/") + 1);
  94. value = Decrypt(value);
  95. path += value;
  96. request.Path = new PathString(path);
  97. request.RouteValues["id"] = value;
  98. }
  99. else
  100. {
  101. content = context.HttpContext.GetBody();
  102. content = Decrypt(content);
  103. //{"username":"admin","password":"000000"}
  104. request.Body = new MemoryStream(Encoding.UTF8.GetBytes(content));
  105. //验证登录接口
  106. if(request.Path.Value.EndsWith("/oauth2/token"))
  107. {
  108. var scope = request.Query["scope"].ToString();
  109. var grantType = request.Query["grant_type"].ToString();
  110. // bool checkLogin = SysLoginService.CheckLogin(scope, grantType, context.HttpContext.GetToken().Replace("Basic ", ""));
  111. // if(!checkLogin)
  112. // {
  113. // string msg = $"请求访问失败,无法访问系统资源";
  114. // context.Result = new JsonResult(ApiResult.Error(ResultCode.DENY, msg));
  115. // }
  116. }
  117. }
  118. }
  119. }
  120. public string Decrypt(string str)
  121. {
  122. str = str.Trim('"');
  123. str = Encoding.UTF8.GetString(Convert.FromBase64String(str));
  124. return Dbconn.AesDecrypt(str, Base.GlobalConstant.ApiKey, Base.GlobalConstant.ApiIv);
  125. }
  126. public string GetParam(string content, string key)
  127. {
  128. if(content.StartsWith("?")) content = content.Substring(1);
  129. string[] data = content.Split('&');
  130. foreach(string sub in data)
  131. {
  132. if(sub.StartsWith(key + "="))
  133. {
  134. return sub.Substring(sub.IndexOf("=") + 1);
  135. }
  136. }
  137. return "";
  138. }
  139. }
  140. }