wtm 出现跨域问题,无法js访问 postman可以访问

发布于 2020-12-28 14:07:44

部署到iis上面,还是出现了跨域

public static IHostBuilder CreateWebHostBuilder(string[] args)

    {

        return
            Host.CreateDefaultBuilder(args)
            .ConfigureLogging((hostingContext, logging) =>
            {
                logging.ClearProviders();
                logging.AddConsole();
                logging.AddDebug();
                logging.AddWTMLogger();
            })
             .ConfigureWebHostDefaults(webBuilder =>
             {
                 webBuilder.ConfigureServices(x =>
                {
                    x.AddFrameworkService();
                    x.AddLayui();
                    x.AddSwaggerGen(c =>
                    {
                        c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
                        var bearer = new OpenApiSecurityScheme()
                        {
                            Description = "JWT Bearer",
                            Name = "Authorization",
                            In = ParameterLocation.Header,
                            Type = SecuritySchemeType.ApiKey

                        };
                        c.AddSecurityDefinition("Bearer", bearer);
                        var sr = new OpenApiSecurityRequirement();
                        sr.Add(new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference
                            {
                                Type = ReferenceType.SecurityScheme,
                                Id = "Bearer"
                            }
                        }, new string[] { });
                        c.AddSecurityRequirement(sr);
                    });
                    x.AddCors(options =>
                    {
                        options.AddPolicy("all", builder =>
                        {
                            builder.SetIsOriginAllowed(_ => true) //允许任何来源的主机访问
                            .AllowAnyMethod()
                            .AllowAnyHeader()
                            .AllowCredentials();//指定处理cookie
                        });
                    });
                });
                 webBuilder.Configure(x =>
                 {
                     var configs = x.ApplicationServices.GetRequiredService<Configs>();
                     if (configs.IsQuickDebug == true)
                     {
                     }

                     x.UseSwagger();
                     x.UseSwaggerUI(c =>
                     {
                         c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
                     });
                     x.UseFrameworkService();
                     x.UseCors("all");
                 });
             }
             );
    }
    大佬,是还需要加什么东西吗

查看更多

关注者
0
被浏览
253
2 个回答
刘亮
刘亮 2020-12-29
专业编码30年

appsettings文件里可以配置啊

asdf
asdf 2020-12-31

"CorsOptions": {

"EnableAll": true,
"Policy": [
  {
    "Name": "Default",
    "Domain": "http://localhost,https://localhost"
  }
]

},这样子配置不行吗

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览