123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- using Microsoft.AspNetCore.Http;
- using NPOI.SS.Formula.Functions;
- using System.Text.Encodings.Web;
- using System.Text.Json;
- using System.Text.Unicode;
- namespace OASystem.API.Middlewares
- {
- /// <summary>
- /// 设置时间端访问All接口
- /// </summary>
- public class TimeRestrictionMiddleware
- {
- private readonly RequestDelegate _next;
- private readonly DateTime _startDateTime;
- private readonly DateTime _endDateTime;
- public TimeRestrictionMiddleware(RequestDelegate next, DateTime startDateTime, DateTime endDateTime)
- {
- _next = next;
- _startDateTime = startDateTime;
- _endDateTime = endDateTime;
- }
- public async Task InvokeAsync(HttpContext context)
- {
- var currentDateTime = DateTime.Now;
- if (currentDateTime >= _startDateTime && currentDateTime <= _endDateTime)
- {
- await _next(context);
- }
- else
- {
- if (context.Request.Method == "OPTIONS")
- {
- context.Response.Headers.Add("Access-Control-Allow-Origin", "http://example.com");
- context.Response.Headers.Add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
- context.Response.Headers.Add("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With");
-
- }
- context.Response.ContentType = "application/json";
- context.Response.StatusCode = 201;
- var response = context.Response;
- var errorResponse = new
- {
- code = 201,
- msg = "NO ACCESS!",
- data = "",
- count = 0
- };
- await context.Response.WriteAsync(System.Text.Json.JsonSerializer.Serialize(errorResponse));
- }
- }
- }
- }
|