zoukankan      html  css  js  c++  java
  • net core 3.1 跨域 Cors 找不到 “Access-Control-Allow-Origin”

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

    首先在ConfigureServices添加

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

    然后新增CorsMiddleware 类

    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);
            }
        }
    

    然后在Configure 使用中间件

     app.UseMiddleware<CorsMiddleware>();
    
  • 相关阅读:
    python安装
    实现node服务器
    VSCode集成tomcat及使用方法
    CommonJS规范
    Bootstrap 笔记
    vue笔记
    jgGrid模板添加
    vue组件中的data
    解析DNS
    StaticResource
  • 原文地址:https://www.cnblogs.com/ouyangkai/p/12531981.html
Copyright © 2011-2022 走看看