title: Enable CORS In MVC Controller
body: <p>I need to access <em>localhost:5002/api/login</em> through <em>localhost:9000</em>.</p>
  
  <p>I have tried almost all the solutions available online.
  But I keep getting the following error.</p>
  
  <p>How to present the 'Access-Control-Allow-Origin' on the requested resource.</p>
  
  <p>XMLHttpRequest cannot load <strong><em>localhost:5002/api/login</em></strong>. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin <strong><em>'localhost:9000'</em></strong> is therefore not allowed access.</p>
  
  <p>I have tried adding this to the Web.config</p>
  
  <blockquote>
  <pre><code>&lt;httpProtocol&gt;
    &lt;customHeaders&gt;
      &lt;clear /&gt;
      &lt;add name="Access-Control-Allow-Origin" value="*" /&gt;
    &lt;/customHeaders&gt;
  &lt;/httpProtocol&gt; 
  </code></pre>
  </blockquote>
  
  <p>And In the server side StartUp.cs File I have added the following as well</p>
  
  <p>public void ConfigureServices(IServiceCollection services)</p>
  
  <pre><code>    {  
          services.AddCors(options =&gt;
  
          {
              options.AddPolicy("CorsPolicy",
                  builder =&gt; builder.AllowAnyOrigin()
                  .AllowAnyMethod()
                  .AllowAnyHeader()
                  .AllowCredentials());
          });
          services.AddMvc();
      }
  </code></pre>
  
  <p>public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IUowProvider uowProvider)</p>
  
  <pre><code>    {
          app.UseCors("CorsPolicy");
          app.UseMvc();
      }
  </code></pre>