平安校园
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.
 
 
 
 
 
 

56 lines
1.4 KiB

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