title: ASP .NET Core use multiple CORS policies
body: <p>I am trying to setup 2 CORS policies.  One as an api default and the other to use on <code>Controllers</code> as I need them.  The reason I want to do this is because I have an endpoint that takes in an object with email info and sends an email (to use with the contact me box on my webpage) and have it only accept requests from my domain.</p>
  
  <p>My <code>startup.cs</code> file snippet:</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
          {
              services.AddCors(options =&gt;
              {
                  options.AddPolicy("Example",
                      builder =&gt; builder.WithOrigins("http://www.example.com"));
                  options.AddPolicy("AllowAll",
                      builder =&gt; builder.AllowAnyOrigin());
              });
  
              services.AddMvc();
              //other configure stuff
          }
  
  
   public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
      {
          app.UseCors(builder =&gt;
          {
              builder.AllowAnyHeader();
              builder.AllowAnyMethod();
              builder.WithOrigins("AllowAll");
          });
  
          app.UseMvcWithDefaultRoute();
      }
  </code></pre>
  
  <p>My <code>emailcontroller.cs</code> file:</p>
  
  <pre><code>using System.Threading.Tasks;
  using MyAPI.Models;
  using MyAPI.Services;
  using Microsoft.AspNetCore.Cors;
  using Microsoft.AspNetCore.Mvc;
  
  namespace MyAPI.Controllers
  {
      [Produces("application/json")]
      [Route("api/Email")]
      [EnableCors("Example")]
      public class EmailController : Controller
      {
          private readonly IEmailSender _emailSender;
  
          public EmailController(IEmailSender emailSender)
          {
              _emailSender = emailSender;
          }
  
          [HttpPost]
          public async Task Post([FromBody] Email email)
          {
              await _emailSender.SendEmailAsync(email);
          }
      }
  
  }
  </code></pre>
  
  <p>Javascript used to send email:</p>
  
  <pre><code>function sendEmail(email)
  {
      var urlToApi = "http://&lt;ipToApi&gt;:5000/api";
      $.ajax({
              method: "POST",
              contentType: "application/json; charset=utf-8",
              data: JSON.stringify(email),     
              url: urlToApi + "/email/",
              success: function(data) {  
                  console.log(data);
                  console.log('probably sent');
              },
              error: function(jqXHR, textStatus, errorThrown){
                  console.log(textStatus);
                  alert("There was like, an error doing that");
              }
          });
  }
  </code></pre>
  
  <p>This is what I get trying to send from <a href="http://www.example.com" rel="noreferrer">http://www.example.com</a></p>
  
  <pre><code>XMLHttpRequest cannot load http://&lt;ipToApi&gt;:5000/api/email/. 
  Response to preflight request doesn't pass access control check: 
  No 'Access-Control-Allow-Origin' header is present on the requested
   resource. Origin 'http://www.example.com' is therefore not allowed access.
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>This works:</p>
  
  <pre><code>services.AddCors(options =&gt;
              {
                  options.AddPolicy("Example",
                      builder =&gt; builder.WithOrigins("http://www.example.com")
                                          .AllowAnyHeader()
                                          .AllowAnyMethod());
                  options.AddPolicy("AllowAll",
                      builder =&gt; builder.AllowAnyOrigin()
                                          .AllowAnyHeader()
                                          .AllowAnyMethod());
              });
  </code></pre>