title: In ASP.NET, Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header
body: <p>I am using a Web Core API and have set up CORS as follows;</p>
  
  <pre><code>    public void ConfigureServices(IServiceCollection services)
      {
          services.AddCors();
          ...
      }
  
  public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
          {
  var url = Configuration["origenUrl"];
              var header = "Content-Type";
              app.UseCors(
                  options =&gt; options.WithOrigins(url).WithHeaders(header).AllowAnyMethod().AllowCredentials()
              );
          }
  </code></pre>
  
  <p>This setup works fine for Get Requests.
  But for my Put request;</p>
  
  <pre><code>   $.ajax({
          url: url,
          method: "PUT",
          xhrFields: { withCredentials: true }
      })
          .done(callback)
          //.fail(errorMessage);
          .fail(function (jqXHR, textStatus, errorThrown) {
              alert("Something went wrong: " + textStatus + " " + errorThrown);
              errorCallback();
          });
  </code></pre>
  
  <p>I get this error message;</p>
  
  <blockquote>
    <p>XMLHttpRequest cannot load <a href="http://localhost:17972/api/fault/1/close" rel="nofollow noreferrer">http://localhost:17972/api/fault/1/close</a>.</p>
    
    <p>Response to preflight request doesn't pass access control check: No
    'Access-Control-Allow-Origin' header is present on the requested
    resource. Origin '<a href="http://localhost:12528" rel="nofollow noreferrer">http://localhost:12528</a>' is therefore not allowed
    access. The response had HTTP status code 401.</p>
  </blockquote>
  
  <p>From Fiddler my http request is;</p>
  
  <blockquote>
    <p>OPTIONS <a href="http://localhost:17972/api/fault/10/close" rel="nofollow noreferrer">http://localhost:17972/api/fault/10/close</a> HTTP/1.1</p>
    
    <p>Accept: <em>/</em></p>
    
    <p>Origin: <a href="http://localhost:12528" rel="nofollow noreferrer">http://localhost:12528</a> </p>
    
    <p>Access-Control-Request-Method: PUT</p>
    
    <p>Access-Control-Request-Headers: accept </p>
    
    <p>UA-CPU: AMD64 </p>
    
    <p>Accept-Encoding: gzip, deflate </p>
    
    <p>User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64;Trident/7.0; rv:11.0) like Gecko </p>
    
    <p>Host: localhost:17972 </p>
    
    <p>Content-Length: 0 </p>
    
    <p>DNT: 1 </p>
    
    <p>Connection: Keep-Alive</p>
    
    <p>Pragma: no-cache</p>
  </blockquote>
  
  <p>So how do I fix this?</p>
  
  <p>EDIT
  I have also tried this code just to get it working, but I get the same error;</p>
  
  <pre><code> public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
          {
              //var url = Configuration["originUrl"];
              //app.UseCors(
              //    options =&gt; options.WithOrigins(url).AllowAnyHeader().AllowAnyMethod().AllowCredentials()
              //);
              app.UseCors(
                  options =&gt; options.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod().AllowCredentials()
              );
              app.UseMvc();
  }
  </code></pre>