AuthorizationFilter.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. Console.WriteLine(content);
  33. content = GetParam(content, "value");
  34. Console.WriteLine(content);
  35. content = HttpUtility.UrlDecode(content);
  36. content = Decrypt(content);
  37. content = HttpUtility.UrlDecode(content);
  38. Console.WriteLine(content);
  39. if(!string.IsNullOrEmpty(content))
  40. {
  41. Dictionary<string, object> dic = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(content);
  42. string queryString = "";
  43. var parameters = context.ActionDescriptor.Parameters;
  44. foreach(var parameter in parameters)
  45. {
  46. string parameterName = parameter.Name;
  47. Type objectType = parameter.ParameterType;
  48. if(objectType.FullName != "System.String")
  49. {
  50. System.Reflection.Assembly assembly = System.Reflection.Assembly.GetAssembly(objectType);
  51. var entry = assembly.CreateInstance(objectType.FullName);
  52. Type type = entry.GetType();
  53. System.Reflection.PropertyInfo[] propertyInfos = type.GetProperties();
  54. for (int i = 0; i < propertyInfos.Length; i++)
  55. {
  56. foreach (string key in dic.Keys)
  57. {
  58. if (propertyInfos[i].Name == key)
  59. {
  60. object value = dic[key];
  61. string ParameterType = propertyInfos[i].GetMethod.ReturnParameter.ParameterType.Name;
  62. if (ParameterType == "Int32")
  63. {
  64. if(value == null || value == "") value = "0";
  65. value = Convert.ToInt32(value);
  66. }
  67. else if (ParameterType == "Int64[]")
  68. {
  69. value = Tools.SpitLongArrary(Newtonsoft.Json.JsonConvert.SerializeObject(value).Replace("[", "").Replace("]", "").Trim('"'), ',');
  70. }
  71. else if (ParameterType == "Int32[]")
  72. {
  73. value = Tools.SpitIntArrary(Newtonsoft.Json.JsonConvert.SerializeObject(value).Replace("[", "").Replace("]", "").Trim('"'), ',');
  74. }
  75. else if (ParameterType == "List`1")
  76. {
  77. string val = Newtonsoft.Json.JsonConvert.SerializeObject(value).Replace("[", "").Replace("]", "").Trim('"');
  78. value = Tools.SpitLongArrary(val, ',').ToList();
  79. }
  80. if(value.ToString() == "-1") value = -1;
  81. if(value.ToString() == "[]") value = "";
  82. queryString += key + "=" + value.ToString() + "&";
  83. break;
  84. }
  85. }
  86. }
  87. }
  88. }
  89. request.QueryString = new QueryString("?" + queryString.TrimEnd('&'));
  90. }
  91. }
  92. else if(context.HttpContext.Request.Method.ToLower() == "delete")
  93. {
  94. string path = request.Path.Value;
  95. string value = path.Substring(path.LastIndexOf("/") + 1);
  96. path = path.Substring(0, path.LastIndexOf("/") + 1);
  97. value = Decrypt(value);
  98. path += value;
  99. request.Path = new PathString(path);
  100. request.RouteValues["id"] = value;
  101. }
  102. else
  103. {
  104. content = context.HttpContext.GetBody();
  105. content = Decrypt(content);
  106. //{"username":"admin","password":"000000"}
  107. request.Body = new MemoryStream(Encoding.UTF8.GetBytes(content));
  108. //验证登录接口
  109. if(request.Path.Value.EndsWith("/oauth2/token"))
  110. {
  111. var scope = request.Query["scope"].ToString();
  112. var grantType = request.Query["grant_type"].ToString();
  113. // bool checkLogin = SysLoginService.CheckLogin(scope, grantType, context.HttpContext.GetToken().Replace("Basic ", ""));
  114. // if(!checkLogin)
  115. // {
  116. // string msg = $"请求访问失败,无法访问系统资源";
  117. // context.Result = new JsonResult(ApiResult.Error(ResultCode.DENY, msg));
  118. // }
  119. }
  120. }
  121. }
  122. }
  123. public string Decrypt(string str)
  124. {
  125. return "";
  126. // str = str.Trim('"');
  127. // str = Encoding.UTF8.GetString(Convert.FromBase64String(str));
  128. // return Dbconn.AesDecrypt(str, Base.GlobalConstant.ApiKey, Base.GlobalConstant.ApiIv);
  129. }
  130. public string GetParam(string content, string key)
  131. {
  132. if(content.StartsWith("?")) content = content.Substring(1);
  133. string[] data = content.Split('&');
  134. foreach(string sub in data)
  135. {
  136. if(sub.StartsWith(key + "="))
  137. {
  138. return sub.Substring(sub.IndexOf("=") + 1);
  139. }
  140. }
  141. return "";
  142. }
  143. }
  144. }