zoukankan      html  css  js  c++  java
  • NET5 WebApi 解决跨域问题

    第一步:在appsettings.json文件中配置跨域访问域名,如:

    {
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft": "Warning",
          "Microsoft.Hosting.Lifetime": "Information"
        }
      },
      "AllowedHosts": "*",
      "CorsPaths": {
        "OriginOne": "http://xxxx.com" //跨域请求网址,跨域添加多个
      }
    }

    第二步:Nuget添加Microsoft.AspNetCore.Cors引用

    第三步:Startup类添加全局变量:

    public readonly string anyAllowSpecificOrigins = "any";//解决跨域

    第四步:Startup类中的ConfigureServices方法里添加配置跨域处理cors,代码如下:

    public void ConfigureServices(IServiceCollection services)
            {
    
                services.AddControllers();
                services.AddSwaggerGen(c =>
                {
                    c.SwaggerDoc("v1", new OpenApiInfo { Title = "Web", Version = "v1" });
                });
    
                services.AddControllers(options =>
                {
                    options.Filters.Add(typeof(ApiExceptionFilter));
                });
    
                //解决跨域
                services.AddCors(options =>
                {
                    options.AddPolicy(anyAllowSpecificOrigins, corsbuilder =>
                    {
                        var corsPath = Configuration.GetSection("CorsPaths").GetChildren().Select(p => p.Value).ToArray();
                        corsbuilder.WithOrigins(corsPath)
                        .AllowAnyMethod()
                        .AllowAnyHeader()
                        .AllowCredentials();//指定处理cookie
                    });
                });
            }

    第五步:Startup类中的Configure方法中添加app.UseCors(anyAllowSpecificOrigins),如下代码:

     app.UseRouting();
     app.UseCors(anyAllowSpecificOrigins);//支持跨域:允许特定来源的主机访问
    app.UseAuthorization();

    注意:以上三行代码顺序不能换,否则报错!

    第六步:Startup类中的Configure方法中添加 .RequireCors(anyAllowSpecificOrigins),如下代码:

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                    app.UseSwagger();
                    app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Web v1"));
                }
    
                app.UseHttpsRedirection();
    
                app.UseRouting();
                app.UseCors(anyAllowSpecificOrigins);//支持跨域:允许特定来源的主机访问
                app.UseAuthorization();
    
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllers().RequireCors(anyAllowSpecificOrigins);//支持跨域
                });
            }

    END

  • 相关阅读:
    大数据学习--day10(继承-权限-super-final-多态-组合)
    大数据学习--day09(this、static)
    大数据学习--day08(hnapp 后台系统开发、面向对象)
    大数据学习--day07(冒泡排序、Arrays工具类、方法可变参数)
    大数据学习--day06(Eclipse、数组)
    大数据学习--day05(嵌套循环、方法、递归)
    大数据学习--day04(选择结构、循环结构、大数据java基础面试题)
    大数据学习--day03(运算符、流程控制语句)
    牛客多校训练营第九场 J
    二次剩余(模板)
  • 原文地址:https://www.cnblogs.com/252e/p/14542653.html
Copyright © 2011-2022 走看看