AuthorizationFilter.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. string content = "";
  27. if(context.HttpContext.Request.Method.ToLower() == "get")
  28. {
  29. content = context.HttpContext.GetQueryString();
  30. content = GetParam(content);
  31. if(!string.IsNullOrEmpty(content))
  32. {
  33. Dictionary<string, object> dic = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(content);
  34. string queryString = "";
  35. var parameters = context.ActionDescriptor.Parameters;
  36. foreach(var parameter in parameters)
  37. {
  38. string parameterName = parameter.Name;
  39. Type objectType = parameter.ParameterType;
  40. if(objectType.FullName != "System.String")
  41. {
  42. System.Reflection.Assembly assembly = System.Reflection.Assembly.GetAssembly(objectType);
  43. var entry = assembly.CreateInstance(objectType.FullName);
  44. Type type = entry.GetType();
  45. System.Reflection.PropertyInfo[] propertyInfos = type.GetProperties();
  46. for (int i = 0; i < propertyInfos.Length; i++)
  47. {
  48. foreach (string key in dic.Keys)
  49. {
  50. if (propertyInfos[i].Name == key)
  51. {
  52. object value = dic[key];
  53. string ParameterType = propertyInfos[i].GetMethod.ReturnParameter.ParameterType.Name;
  54. if (ParameterType == "Int32")
  55. {
  56. if(value == null || value == "") value = "0";
  57. value = Convert.ToInt32(value);
  58. }
  59. else if (ParameterType == "Decimal")
  60. {
  61. if(value == null || value == "") value = "0";
  62. value = Convert.ToDecimal(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. path += value;
  95. request.Path = new PathString(path);
  96. request.RouteValues["id"] = value;
  97. }
  98. else
  99. {
  100. content = context.HttpContext.GetBody();
  101. if(!string.IsNullOrEmpty(content))
  102. {
  103. Dictionary<string, object> dic = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(content);
  104. string queryString = "{";
  105. var parameters = context.ActionDescriptor.Parameters;
  106. foreach(var parameter in parameters)
  107. {
  108. string parameterName = parameter.Name;
  109. Type objectType = parameter.ParameterType;
  110. if(objectType.FullName != "System.String")
  111. {
  112. System.Reflection.Assembly assembly = System.Reflection.Assembly.GetAssembly(objectType);
  113. var entry = assembly.CreateInstance(objectType.FullName);
  114. Type type = entry.GetType();
  115. System.Reflection.PropertyInfo[] propertyInfos = type.GetProperties();
  116. for (int i = 0; i < propertyInfos.Length; i++)
  117. {
  118. foreach (string key in dic.Keys)
  119. {
  120. if (propertyInfos[i].Name == key)
  121. {
  122. object value = dic[key];
  123. string ParameterType = propertyInfos[i].GetMethod.ReturnParameter.ParameterType.Name;
  124. if (ParameterType == "Int32")
  125. {
  126. if(value == null || value == "") value = "0";
  127. value = Convert.ToInt32(value);
  128. }
  129. else if (ParameterType == "Decimal")
  130. {
  131. if(value == null || value == "") value = "0";
  132. value = Convert.ToDecimal(value);
  133. }
  134. else if (ParameterType == "Int64[]")
  135. {
  136. value = Tools.SpitLongArrary(Newtonsoft.Json.JsonConvert.SerializeObject(value).Replace("[", "").Replace("]", "").Trim('"'), ',');
  137. }
  138. else if (ParameterType == "Int32[]")
  139. {
  140. value = Tools.SpitIntArrary(Newtonsoft.Json.JsonConvert.SerializeObject(value).Replace("[", "").Replace("]", "").Trim('"'), ',');
  141. }
  142. else if (ParameterType == "List`1")
  143. {
  144. string val = Newtonsoft.Json.JsonConvert.SerializeObject(value).Replace("[", "").Replace("]", "").Trim('"');
  145. value = Tools.SpitLongArrary(val, ',').ToList();
  146. }
  147. if(value.ToString() == "-1") value = -1;
  148. if(value.ToString() == "[]") value = "";
  149. queryString += "\"" + key + "\":\"" + value.ToString() + "\",";
  150. break;
  151. }
  152. }
  153. }
  154. }
  155. }
  156. queryString = queryString.TrimEnd(',');
  157. queryString += "}";
  158. request.Body = new MemoryStream(Encoding.UTF8.GetBytes(queryString));
  159. }
  160. }
  161. }
  162. // public string Decrypt(string str)
  163. // {
  164. // str = str.Trim('"');
  165. // str = Encoding.UTF8.GetString(Convert.FromBase64String(str));
  166. // return Dbconn.AesDecrypt(str, Base.GlobalConstant.ApiKey, Base.GlobalConstant.ApiIv);
  167. // }
  168. public string GetParam(string content)
  169. {
  170. string result = "{";
  171. if(content.StartsWith("?")) content = content.Substring(1);
  172. string[] data = content.Split('&');
  173. foreach(string sub in data)
  174. {
  175. string[] subData = sub.Split('=');
  176. result += "\"" + subData[0] + "\":\"" + subData[1] + "\",";
  177. }
  178. result += "}";
  179. return result;
  180. }
  181. }
  182. }