title: CORS Preflight Request Issue on .Net Core API running locally on IIS Express
body: <p>We are developing an application using a .Net Core (3.1.1) API and a react front-end. The application uses Windows Authentication.   We are running the API framework-dependant on IIS.  We initially had CORS issues but got everything working on IIS on the server by installing the CORS module and doing a transformation to the web.config on deploy.  We also got basic CORS running on IIS Express by adding a <code>&lt;customheaders&gt;</code> section to the applicationhost.config file for Visual Studio.  However, we are stuck on the preflight requests for PUT and DELETE actions when running locally on IIS Express.  </p>
  
  <p>Here is my lauchSettings.json file: </p>
  
  <pre><code>{
    "iisSettings": {
      "windowsAuthentication": true,
      "anonymousAuthentication": false,
      "iisExpress": {
        "applicationUrl": "http://localhost:xxxxx",
        "sslPort": xxxxx
      }
    },
    "$schema": "http://json.schemastore.org/launchsettings.json",
    "profiles": {
      "IIS Express": {
        "commandName": "IISExpress",
        "launchBrowser": true,
        "launchUrl": "api/values",
        "environmentVariables": {
          "ASPNETCORE_ENVIRONMENT": "Development"
        }
      },
      "API": {
        "commandName": "Project",
        "launchBrowser": true,
        "launchUrl": "api/values",
        "environmentVariables": {
          "ASPNETCORE_ENVIRONMENT": "Development"
        },
        "applicationUrl": "http://localhost:xxxxx;https://localhost:xxxxx"
      }
    }
  </code></pre>
  
  <p>Here is the CORS section of our applicationhost.config file:</p>
  
  <pre><code>     &lt;customHeaders&gt;
          &lt;clear /&gt;
          &lt;add name="X-Powered-By" value="ASP.NET" /&gt;
          &lt;remove name="Access-Control-Allow-Origin" /&gt;
          &lt;add name="access-control-allow-origin" value="http://localhost:3000" /&gt;
          &lt;add name="access-control-allow-headers" value="*" /&gt;
          &lt;add name="access-control-allow-credentials" value="true" /&gt;
          &lt;add name="access-control-allow-methods" value="get, post, put, delete, options" /&gt;
          &lt;add name="access-control-max-age" value="600" /&gt;
        &lt;/customHeaders&gt;
  </code></pre>
  
  <p>As I said, this works great for GET &amp; POST, but isn't working for PUT or DELETE.  We get the error: </p>
  
  <pre><code>Access to fetch at 'https://localhost:xxxx/api/blah/blahblah' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
  </code></pre>
  
  <p>I tried installing the CORS module locally (which fixed the issue on the server with IIS) but it requires the IIS 7 engine or above to run.  I think that the basic issue is that the preflight request is being sent anonymously, and we have anonymous authentication disabled so we can use Windows Authentication - but short of rewriting the application to handle CORS and authentication in the application instead of letting IIS handle it - do I have any option for IIS Express?</p>
  
  <p>Here are some resources I used to get me this far:
  <a href="https://davidsekar.com/asp-net/cors-development-in-localhost" rel="nofollow noreferrer">https://davidsekar.com/asp-net/cors-development-in-localhost</a>, 
  <a href="https://docs.microsoft.com/en-us/aspnet/core/security/authentication/windowsauth?view=aspnetcore-3.1&amp;tabs=visual-studio" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/aspnet/core/security/authentication/windowsauth?view=aspnetcore-3.1&amp;tabs=visual-studio</a>,
  <a href="https://blogs.iis.net/iisteam/introducing-iis-cors-1-0" rel="nofollow noreferrer">https://blogs.iis.net/iisteam/introducing-iis-cors-1-0</a>,
  <a href="http://blog.jonathanchannon.com/2013/09/16/enabling-cors-in-iisexpress/" rel="nofollow noreferrer">http://blog.jonathanchannon.com/2013/09/16/enabling-cors-in-iisexpress/</a>,</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I may have solved it by adding the following to the applicationhost.json file in the <code>system.WebServer</code> section: </p>
  
  <pre><code>&lt;rewrite&gt;
              &lt;globalRules&gt;
                  &lt;rule name="Preflight" patternSyntax="Wildcard" stopProcessing="true"&gt;
                      &lt;match url="*" /&gt;
                      &lt;conditions&gt;
                          &lt;add input="{REQUEST_METHOD}" pattern="OPTIONS" /&gt;
                      &lt;/conditions&gt;
                      &lt;action type="CustomResponse" statusCode="200" statusReason="Preflight" statusDescription="Preflight" /&gt;
                  &lt;/rule&gt;
              &lt;/globalRules&gt;
          &lt;/rewrite&gt;
  </code></pre>
  
  <p>This basically just returns a custom 200 response to all OPTIONS requests.  Wouldn't be ideal for production, but since this is just for running IIS Express locally, it should be fine.  I'll post another update after more testing to confirm.</p>
  
  <p><strong>UPDATE 2</strong>
  That did, in fact, correct the issue.  Hope this helps save someone else some time.... </p>