首先在ConfigureServices添加

public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors(options =>
            {
                options.AddPolicy("any", builder =>
                {
                    //builder.AllowAnyOrigin() //允许任何来源的主机访问
                    builder
                    
                    .WithOrigins("http://*.*.*.*")//.SetIsOriginAllowedToAllowWildcardSubdomains()//设置允许访问的域

                    .AllowAnyMethod()

                    .AllowAnyHeader()

                    .AllowCredentials();//

                });

            });
            services.AddControllers();
        }

然后新增 

public class CorsMiddleware
    {
        private readonly RequestDelegate _next;
        public CorsMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        public async Task Invoke(HttpContext context)
        {
            if (!context.Response.Headers.ContainsKey("Access-Control-Allow-Origin"))
            {
                context.Response.Headers.Add("Access-Control-Allow-Origin", "*");
            }
            await _next(context);
        }
    }

然后 使用中间件

 app.UseMiddleware<CorsMiddleware>();

net core 3.1 跨域 Cors 找不到 “Access-Control-Allow-Origin”

相关文章:

  • 2018-12-17
  • 2022-12-23
  • 2021-11-30
  • 2022-01-18
  • 2022-12-23
  • 2022-12-23
  • 2021-09-19
猜你喜欢
  • 2022-12-23
  • 2021-07-27
  • 2021-05-22
  • 2021-09-29
  • 2022-12-23
相关资源
相似解决方案