在IIS 7.5上运行ASP.NET Core WebApi时没有'Access-Control-Allow-Origin'标头,但在IIS 8.5和10上正常工作

时间:2017-06-28 12:28:30

标签: iis asp.net-core cors iis-7.5

我已经将CORS添加到我的.NET Core WebApi中,并且在IIS 8.5和10上运行时它运行正常,但每当我在IIS 7.5上运行它时,我得到..

  

Fetch API无法加载..对预检请求的响应未通过   访问控制检查:没有'Access-Control-Allow-Origin'标头   出席要求的资源。来源'http://localhost:8080'是   因此不允许访问。响应的HTTP状态代码为500 .. ..

我尝试过从特定的来源切换到允许任何来源以及将其添加到我的web.config:

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

为了让它在IIS 7.5上运行,我需要采取一些额外的步骤吗?

Startup.cs

    public void Configure( IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory )
    {
        ...
        app.UseJwtBearerAuthentication( GetJwtBearerOptions() );
        app.UseCors( ConfigureCors );
        app.UseExceptionHandler( ConfigureExceptionHandler );
        app.UseMvc();
    }

    private static void ConfigureCors( CorsPolicyBuilder builder )
    {
        builder
            .AllowAnyOrigin()
            //.WithOrigins( Configuration.GetSection( "Client" )["Url"] )
            .AllowAnyHeader()
            .AllowAnyMethod();
    }

    public void ConfigureServices( IServiceCollection services )
    {
        // Add framework services
        services.AddOptions();
        services.AddMemoryCache();
        services.AddSingleton<IConfiguration>( Configuration );
        services.AddCors(); // obsolete?
        services.AddMvc( SetupMvcOptions )
            .AddJsonOptions( SetupMvcJsonOptions );
        services.Configure<JwtIssuerOptions>( ConfigureJwtIssuerOptions );
    }

的Web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <!--
    Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380
  -->
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
    </handlers>
    <aspNetCore processPath=".\Web.Services.exe" arguments="" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false" />
  </system.webServer>
</configuration>
<!--ProjectGuid: 90bd8dc9-484b-42c7-8074-e02397de9689-->

0 个答案:

没有答案