AuthorizationFilter.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. private readonly ISysLoginService SysLoginService;
  18. public AuthorizationFilter(ISysLoginService sysLoginService)
  19. {
  20. this.SysLoginService = sysLoginService;
  21. }
  22. /// <summary>
  23. /// </summary>
  24. /// <param name="context"></param>
  25. public void OnAuthorization(AuthorizationFilterContext context)
  26. {
  27. var request = context.HttpContext.Request;
  28. string content = "";
  29. if(context.HttpContext.Request.Method.ToLower() == "get")
  30. {
  31. content = context.HttpContext.GetQueryString();
  32. content = content.Substring(content.IndexOf("=") + 1);
  33. content = HttpUtility.UrlDecode(content);
  34. content = Decrypt(content);
  35. if(!string.IsNullOrEmpty(content))
  36. {
  37. Dictionary<string, string> dic = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(content);
  38. string queryString = "";
  39. foreach(string key in dic.Keys)
  40. {
  41. queryString += key + "=" + dic[key] + "&";
  42. }
  43. request.QueryString = new QueryString("?" + queryString.TrimEnd('&'));
  44. }
  45. }
  46. else if(context.HttpContext.Request.Method.ToLower() == "delete")
  47. {
  48. string path = request.Path.Value;
  49. string value = path.Substring(path.LastIndexOf("/") + 1);
  50. path = path.Substring(0, path.LastIndexOf("/") + 1);
  51. value = Decrypt(value);
  52. path += value;
  53. request.Path = new PathString(path);
  54. request.RouteValues["id"] = value;
  55. }
  56. else
  57. {
  58. content = context.HttpContext.GetBody();
  59. content = Decrypt(content);
  60. //{"username":"admin","password":"000000"}
  61. request.Body = new MemoryStream(Encoding.UTF8.GetBytes(content));
  62. //验证登录接口
  63. if(request.Path.Value.EndsWith("/oauth2/token"))
  64. {
  65. var scope = request.Query["scope"].ToString();
  66. var grantType = request.Query["grant_type"].ToString();
  67. bool checkLogin = SysLoginService.CheckLogin(scope, grantType, context.HttpContext.GetToken().Replace("Basic ", ""));
  68. if(!checkLogin)
  69. {
  70. string msg = $"请求访问失败,无法访问系统资源";
  71. context.Result = new JsonResult(ApiResult.Error(ResultCode.DENY, msg));
  72. }
  73. }
  74. }
  75. }
  76. public string Decrypt(string str)
  77. {
  78. str = str.Trim('"');
  79. str = Encoding.UTF8.GetString(Convert.FromBase64String(str));
  80. return Dbconn.AesDecrypt(str, Base.GlobalConstant.ApiKey, Base.GlobalConstant.ApiIv);
  81. }
  82. }
  83. }