平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WebStatusMiddleware.cs 1.4 KiB

2 months ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. 
  2. //
  3. namespace SafeCampus.Web.Core;
  4. /// <summary>
  5. /// 网站开启状态 状态中间件
  6. /// </summary>
  7. public class WebStatusMiddleware
  8. {
  9. private readonly RequestDelegate _next;
  10. public WebStatusMiddleware(RequestDelegate next)
  11. {
  12. _next = next;
  13. }
  14. public async Task InvokeAsync(HttpContext context)
  15. {
  16. var path = context.Request.Path.Value;//获取请求路径
  17. // 检查请求路径是否以 "/biz" 开头
  18. if (path.Length > 1 && !path.Contains('.') && !path.StartsWith("/sys", StringComparison.OrdinalIgnoreCase))
  19. {
  20. // 通过 context.RequestServices 解析
  21. var configService = context.RequestServices.GetService<IConfigService>();
  22. // 获取网站状态
  23. var webStatus = await configService.GetByConfigKey(CateGoryConst.CONFIG_SYS_BASE, SysConfigConst.SYS_WEB_STATUS);
  24. // 如果网站状态为禁用,则返回 443 状态码
  25. if (webStatus.ConfigValue == CommonStatusConst.DISABLED)
  26. {
  27. context.Response.StatusCode = 423;
  28. return;
  29. }
  30. }
  31. await _next(context);
  32. }
  33. }
  34. /// <summary>
  35. /// 中间件拓展类
  36. /// </summary>
  37. public static class WebStatusMiddlewareExtensions
  38. {
  39. public static IApplicationBuilder UseWebStatus(this IApplicationBuilder builder)
  40. {
  41. return builder.UseMiddleware<WebStatusMiddleware>();
  42. }
  43. }