title: Trouble with CORS Policy and .NET Core 3.1
body: <p>I'm not sure what I'm missing, but can't seem to get my CORS Policy working with .NET Core 3.1 and Angular 8 client-side.</p>
  
  <p><code>Startup.cs</code>:</p>
  
  <pre class="lang-cs prettyprint-override"><code>        public void ConfigureServices(IServiceCollection services)
          {
              // ...
  
              // Add CORS policy
              services.AddCors(options =&gt;
              {
                  options.AddPolicy("foo",
                  builder =&gt;
                  {
                      // Not a permanent solution, but just trying to isolate the problem
                      builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader();
                  });
              });
  
              services.AddControllers();
          }
  
          public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
          {
              if (env.IsDevelopment())
              {
                  app.UseDeveloperExceptionPage();
              }
  
              app.UseHttpsRedirection();
  
              // Use the CORS policy
              app.UseCors("foo");
  
              app.UseRouting();
  
              app.UseAuthorization();
  
              app.UseEndpoints(endpoints =&gt;
              {
                  endpoints.MapControllers();
              });
          }
  </code></pre>
  
  <p>Error Message Client-side:</p>
  
  <pre><code>Access to XMLHttpRequest at 'https://localhost:8082/api/auth/' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
  </code></pre>
  
  <h1>UPDATE:</h1>
  
  <p>Although I <em>was</em> configuring CORS incorrectly (and the accepted answer below did in fact help with that) the root of issue was unrelated.  For additional context, the app was working completely fine when running the API and Angular app using the CLI - I was only having this issue after deploying them both to a web server.</p>
  
  <p>The <em>"actual"</em> issue ended up being related to the SQL connection, which I only discovered after adding flat-file error logging to the API and running a SQL Server trace to find that the app wasn't able to connect to SQL at all.  </p>
  
  <p>I would normally expect this to just return a 500 and I would have realized the issue in a matter of 10 seconds - however the CORS mis-configuration meant a 500 was never actually being returned because the CORS middleware failed first.  <strong>This was immensely frustrating to say the absolute least!</strong> . However I want to add that here in case others find themselves in this situation, as I was "chasing the wrong rabbit," if you will.  <strong>After fixing the CORS configuration, I realized the actual issue was entirely unrelated to CORS.</strong></p>
  
  <h1>TL;DR; - Sometimes "Non-CORS" .NET Server-side Errors Can Be Returned as CORS Errors If CORS policies Aren't Set Correctly</h1>
  
  <p>References:</p>
  
  <p><a href="https://docs.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-3.1#cors-with-named-policy-and-middleware" rel="noreferrer">https://docs.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-3.1#cors-with-named-policy-and-middleware</a></p>
  
  <p><a href="https://medium.com/swlh/cors-headers-with-dot-net-core-3-5c9dfc664785" rel="noreferrer">https://medium.com/swlh/cors-headers-with-dot-net-core-3-5c9dfc664785</a></p>