AuthorizeMiddleware.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using Microsoft.AspNetCore.Authorization;
  2. using System.Text;
  3. using WMS.BZModels.Dto.HJ.WareHouseDtos;
  4. using WMS.BZUtil;
  5. using WMS.BZWeb.Extensions;
  6. using WMS.Info;
  7. using WMS.Util;
  8. namespace WMS.BZWeb.Middleware
  9. {
  10. public class AuthorizeMiddleware
  11. {
  12. private readonly RequestDelegate _next;
  13. private readonly IOperator _operator;
  14. public AuthorizeMiddleware(RequestDelegate next, IOperator ioperator)
  15. {
  16. _next = next;
  17. _operator = ioperator;
  18. }
  19. /// <summary>
  20. /// 执行
  21. /// </summary>
  22. /// <param name="httpContext">请求连接</param>
  23. /// <returns></returns>
  24. public async Task Invoke(HttpContext httpContext)
  25. {
  26. //string url = SetUrl(httpContext);
  27. var endpoint = httpContext.GetEndpoint();
  28. if (httpContext.Request.IsAjax() && endpoint != null && endpoint.Metadata.GetMetadata<IAllowAnonymous>() == null)
  29. {
  30. // 获取请求值
  31. if (!httpContext.Request.Headers["token"].IsEmpty())
  32. {
  33. string token = httpContext.Request.Headers["token"].ToString();
  34. var res = _operator.DecodeToken(token);
  35. if (res == "TokenExpiredException")
  36. {
  37. await RespondWithJson(httpContext.Response, new ResInfo { code = EResponseCode.nologin, info = "登录信息过期" });
  38. return;
  39. }
  40. else if (res == "SignatureVerificationException")
  41. {
  42. await RespondWithJson(httpContext.Response, new ResInfo { code = EResponseCode.nologin, info = "非法密钥" });
  43. return;
  44. }
  45. else
  46. {
  47. var payload = res.ToObject<Payload>();
  48. WebUtil.SetItem("account", payload.Account);
  49. WebUtil.SetItem("userId", payload.UserId);
  50. WebUtil.SetItem("userName", payload.UserName);
  51. WebUtil.SetItem("WarehouseNo", payload.WarehouseNo);
  52. WebUtil.SetItem("EncryptTokenNo", payload.EncryptTokenNo);
  53. }
  54. }
  55. else
  56. {
  57. await RespondWithJson(httpContext.Response, new ResInfo { code = EResponseCode.nologin, info = "权限验证失败" });
  58. return;
  59. }
  60. }
  61. else
  62. {
  63. if (httpContext.Request.Query.ContainsKey("lrmcode"))
  64. {
  65. string mouldeCode = httpContext.Request.Query["lrmcode"];
  66. WebUtil.SetItem("mouldeCode", mouldeCode);
  67. }
  68. if (httpContext.Request.Query.ContainsKey("lraccount"))
  69. {
  70. string account = httpContext.Request.Query["lraccount"];
  71. WebUtil.SetItem("account", account);
  72. }
  73. }
  74. await _next(httpContext);
  75. return;
  76. }
  77. /// <summary>
  78. /// 设置url地址
  79. /// </summary>
  80. /// <param name="httpContext">请求上下文</param>
  81. /// <returns></returns>
  82. private string SetUrl(HttpContext httpContext)
  83. {
  84. string url = httpContext.Request.Path + httpContext.Request.QueryString.Value;
  85. WebUtil.SetItem("currentUrl", url);
  86. return url;
  87. }
  88. /// <summary>
  89. /// 返回请求信息
  90. /// </summary>
  91. /// <param name="response">返回头</param>
  92. /// <param name="data">数据</param>
  93. /// <returns></returns>
  94. private async Task RespondWithJson(HttpResponse response, object data)
  95. {
  96. response.StatusCode = 200;
  97. response.ContentType = "application/json;charset=utf-8";
  98. await response.WriteAsync(data.ToJson(), new UTF8Encoding(false));
  99. }
  100. }
  101. public class MiddlewareEx
  102. {
  103. private readonly RequestDelegate _next;
  104. public MiddlewareEx(RequestDelegate next)
  105. {
  106. _next = next;
  107. }
  108. /// <summary>
  109. /// 执行
  110. /// </summary>
  111. /// <param name="httpContext">请求连接</param>
  112. /// <returns></returns>
  113. public async Task Invoke(HttpContext httpContext)
  114. {
  115. await _next(httpContext);
  116. return;
  117. }
  118. }
  119. }