IIS hijacks CORS Preflight OPTIONS request

后端 未结 12 1063
说谎
说谎 2020-11-27 03:57

I am making a CORS POST request and setting the Content-Type header to json. This triggers a Preflight OPTIONS request to fire (this is good and expected)

This OPTIO

相关标签:
12条回答
  • 2020-11-27 04:38

    I tried all the mentioned posts but nothing worked for me, then i shifted my ASP.Net Web API 2 service to windows server 2012 (IIS 8.5) and same service worked without any changes. So issue was specific to IIS 7.5 on windows 7 machine.

    0 讨论(0)
  • 2020-11-27 04:40

    I have installed Microsoft.AspNet.WebApi.Cors & Microsoft.Owin.Cors for my oWin based WebAPI and added app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); at config like below:

    public class Startup : IStartup, IAppStartup
    {
        public void Configuration(IAppBuilder app)
        {
            var config = this.GetInjectionConfiguration();
            BootstrapperWebApi bootstrapperWebApi = (BootstrapperWebApi)this.GetBootstrapperWebApi(config);
    
            bootstrapperWebApi.Initialize(true)
            .EnableLogging()
            .DisableWebApiDefaultExceptionHandler();
    
            WebApiConfig.Register(config);
    
            app.UseOwinExceptionHandler();
    
            app.Use<LoggerMiddleware>();
    
            app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
            //others stuff
    
        }
    
    0 讨论(0)
  • 2020-11-27 04:41

    Check if URLScan tool is installed on IIS. When so check following section:

    
    ;
    ; The verbs (aka HTTP methods) listed here are those commonly
    ; processed by a typical IIS server.
    ;
    ; Note that these entries are effective if "UseAllowVerbs=1"
    ; is set in the [Options] section above.
    ;
    
    GET
    HEAD
    POST
    OPTIONS
    
    0 讨论(0)
  • 2020-11-27 04:47

    I tried all of the above suggestions as well as others I found on SO and what mattered in my situation was we had Request Filtering enabled on IIS and the OPTIONS HTTP Verb was not in the list of allowed verbs. Once I added it I was able to sort out the rest of it.

    0 讨论(0)
  • 2020-11-27 04:49

    In our case it was request filtering in IIS disabling OPTIONS verb at the root web application level. Open up IIS Manager, click on root application, click on Request Filtering, if OPTIONS appears in list either remove or Allow Verb. Wish I had checked this first as lots of wasted time.

    0 讨论(0)
  • 2020-11-27 04:51

    I had the same issue and the following web.config settings fixed it for me.

        <modules runAllManagedModulesForAllRequests="false">
          <remove name="FormsAuthenticationModule" />
        </modules>
        <handlers>
          <remove name="OPTIONSVerbHandler" />
          <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
          <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
        </handlers>
    

    I was then able to handle CORS OPTIONS requests manually in Application_BeginRequest.

    I was originally using the library detailed in this blog post for handling CORS requests. The product I'm working on requires that runAllManagedModulesForAllRequests be set to false, though. This is why I had to set up a custom implementation, but if you don't have that requirement you should give that library a try. It worked great when I was able to have runAllManagedModulesForAllRequests set to true.

    0 讨论(0)
提交回复
热议问题