简体   繁体   English

在 IIS 10 中发布我的项目后出现 Cors 错误

[英]Cors error after publishing my project in IIS 10

after publishing my project in IIS I'm having the cors strict-origin error, but I've already configured everything I could.在 IIS 中发布我的项目后,我遇到了 cors 严格来源错误,但我已经配置了我能做的一切。

Below is my configuration下面是我的配置

I'm using Netcore 6.0我正在使用 Netcore 6.0

    private static void ConfigureServices(WebApplicationBuilder builder)
    {
        // Add services to the container.
        builder.Services.AddControllers();
        builder.Services.AddEndpointsApiExplorer();
        builder.Services.AddSwaggerGen();

        ConfigureService.ConfigureDependenciesService(builder);
        ConfigureService.ConfigureControllerService(builder);
        ConfigureRepository.ConfigureDependenciesRepository(builder);
        ConfigureAuthService.ConfigureJWT(builder);
        ConfigureCultureService.ConfigureCulture(builder);
        ConfigureEmailService.ConfigureSmtpClient(builder);

        builder.Services.AddCors(opt =>
        {
            opt.AddPolicy("CorsPolicy", builder => builder
                .AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader());
        });
    }

    private static void Configure(WebApplication app)
    {
        // Configure the HTTP request pipeline.
        if (app.Environment.IsDevelopment())
        {
            app.UseSwagger();
            app.UseSwaggerUI();
        }

        app.UseAuthentication();
        var localizeOptions = app.Services.GetService<IOptions<RequestLocalizationOptions>>();
        app.UseRequestLocalization(localizeOptions.Value);

        //app.UseHttpsRedirection();
        app.UseCors("CorsPolicy");

        app.UseAuthentication();
        app.UseRouting();
        app.UseAuthorization();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }

Config web.config IIS FOLDER配置 web.config IIS 文件夹

Please check if you have cors enabled in iis, if not, you can enable cors by adding configuration in your web.config file, here is the configuration: Please check if you have cors enabled in iis, if not, you can enable cors by adding configuration in your web.config file, here is the configuration:

<system.webServer>
  <httpProtocol>
    <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Methods" value="GET,POST,OPTIONS" />
        <add name="Access-Control-Allow-Headers" value="Content-Type" />
    </customHeaders>
  </httpProtocol>
</system.webServer>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM