using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;

namespace Core.Api
{
    /// <summary>
    ///Cross-domain middleware
    /// </summary>
    public class CorsMiddleware
    {
        private readonly RequestDelegate _next;

        /// <summary>
        ///When the pipe executes to the middleware, the next middleware's RequestDelegate requests the delegate, and other parameters, if any, are also injected
        /// </summary>
        /// <param name="next">Next handler</param>
        public CorsMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        /// <summary>
        ///Custom middleware logic to execute
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task Invoke(HttpContext context)
        {
            context.Response.Headers.Add("Access-Control-Allow-Origin"."*");
            context.Response.Headers.Add("Access-Control-Allow-Headers", context.Request.Headers["Access-Control-Request-Headers"]);
            context.Response.Headers.Add("Access-Control-Allow-Methods"."GET, POST, PUT, DELETE, OPTIONS");

            // If the request is for OPTIONS, the request is returned directly
            if(context.Request.Method.ToUpper() ! ="OPTIONS")
                await _next(context);// Pass the context in to execute the next middleware}}}Copy the code
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseMiddleware<CorsMiddleware>()/ / across domains
}
Copy the code