Files
Yi.Admin/Yi.Framework/Yi.Framework.WebCore/MiddlewareExtend/PreOptionRequestMiddleware.cs
2021-10-10 17:30:31 +08:00

50 lines
1.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace CC.ElectronicCommerce.WebCore.MiddlewareExtend
{
/// <summary>
/// Axios会触发需要做个状态返回还需要指定跨域信息这里放在网关了
///
/// OPTIONS请求即预检请求可用于检测服务器允许的http方法。当发起跨域请求时由于安全原因触发一定条件时浏览器会在正式请求之前自动先发起OPTIONS请求即CORS预检请求服务器若接受该跨域请求浏览器才继续发起正式请求。
/// </summary>
public class PreOptionRequestMiddleware
{
private readonly RequestDelegate _next;
public PreOptionRequestMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
if (context.Request.Method.ToUpper() == "OPTIONS")
{
//context.Response.Headers.Add("Access-Control-Allow-Origin", "http://localhost:8070");
//context.Response.Headers.Add("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,PATCH,OPTIONS");
//context.Response.Headers.Add("Access-Control-Allow-Headers", "*");
context.Response.StatusCode = 200;
return;
}
await _next.Invoke(context);
}
}
/// <summary>
/// 扩展中间件
/// </summary>
public static class PreOptionsRequestMiddlewareExtensions
{
public static IApplicationBuilder UsePreOptionsRequest(this IApplicationBuilder app)
{
return app.UseMiddleware<PreOptionRequestMiddleware>();
}
}
}