title: Angular and WebApi Cors Request from origin has been blocked by CORS policy
body: <p>I have An angular application
  (angular version
  Angular CLI: 8.3.26
  Node: 10.13.0
  OS: win32 x64
  Angular: 8.2.14)</p>
  <p>In environment.ts I have the link with my backend</p>
  <pre class="lang-js prettyprint-override"><code>    export const environment = {
        baseUrlNG: 'http://localhost:4200/',
        baseUrlApi: 'http://localhost:8082/backend/api/'
      }
  </code></pre>
  <p>The backend is a  C# WebApi application.
  I debug it with Visual Studio with this configuration.</p>
  <p><a href="https://i.stack.imgur.com/Rfa6d.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Rfa6d.png" alt="server configuration" /></a></p>
  <p>I installed nuget Microsoft.AspNet.WebApi.Cors and in file WebApiConfig.cs I have this code:</p>
  <pre class="lang-cs prettyprint-override"><code>    public static class WebApiConfig
      {
          [EnableCors(origins: &quot;*&quot;, headers: &quot;*&quot;, methods: &quot;*&quot;)]
          public static void Register(HttpConfiguration config)
          {
              config.EnableCors();
  
              config.MapHttpAttributeRoutes();
  
              config.Routes.MapHttpRoute(
                  name: &quot;DefaultApi&quot;,
                  routeTemplate: &quot;api/{controller}/{language}/{method}/{id}&quot;,
                  defaults: new { method = RouteParameter.Optional, id = RouteParameter.Optional }
  
              );
          }
      }
  </code></pre>
  <p>And In all My backend methods I have this decorator</p>
  <pre class="lang-cs prettyprint-override"><code>    [EnableCors(origins: &quot;*&quot;, headers: &quot;*&quot;, methods: &quot;*&quot;)]
      public class AuthenticationController : ApiController
      {
          [Route(&quot;api/Authentication/{language}/GuestUser&quot;)]
          [HttpPost]
          [Attributes.SiteParameter_Check]
          public IHttpActionResult GuestAuthorization(string language)
          {
              //code with breakpoints never raised.
          }
  </code></pre>
  <p>In angular project I have this post</p>
  <pre class="lang-js prettyprint-override"><code>    function guest(){
        url =  `${environment.baseUrlApi}Authentication/IT/GuestUser`;
        return this.http.post(url, {}, )
          .toPromise()        
          .then(response =&gt; {
                //other code        
          })
      }
  </code></pre>
  <p>I'm getting this error</p>
  <pre><code>Access to XMLHttpRequest at 'http://localhost:8082/backend/api/Authentication/IT/GuestUser' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
  </code></pre>
  <p>Already tried to put the file proxy.conf.json in the src folder of angular project</p>
  <pre class="lang-js prettyprint-override"><code>{
      {
          &quot;/api/*&quot;: {
          &quot;target&quot;: &quot;http://localhost:8082/backend/api&quot;,
          &quot;secure&quot;: false,
          &quot;logLevel&quot;: &quot;debug&quot;
      }
  }
  </code></pre>
  <p>And I put this line in file angular.json</p>
  <pre class="lang-js prettyprint-override"><code>     &quot;serve&quot;: {
            &quot;builder&quot;: &quot;@angular-devkit/build-angular:dev-server&quot;,
            &quot;options&quot;: {
              &quot;browserTarget&quot;: &quot;frontend:build&quot;,
              &quot;proxyConfig&quot;: &quot;src/proxy.conf.json&quot;
        },
  </code></pre>
  <p>this is my web config</p>
  <pre class="lang-xml prettyprint-override"><code>    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;!--
    For more information on how to configure your ASP.NET application, please visit
    http://go.microsoft.com/fwlink/?LinkId=301879
    --&gt;
  &lt;configuration&gt;
      &lt;configSections&gt;
  
          &lt;section name=&quot;entityFramework&quot; type=&quot;System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089&quot; requirePermission=&quot;false&quot; /&gt;
      &lt;/configSections&gt;
  
      &lt;system.web&gt;
          &lt;compilation debug=&quot;true&quot; targetFramework=&quot;4.6.1&quot; /&gt;
          &lt;httpRuntime targetFramework=&quot;4.6.1&quot; executionTimeout=&quot;1200&quot; maxRequestLength=&quot;1048576&quot; useFullyQualifiedRedirectUrl=&quot;false&quot; minFreeThreads=&quot;8&quot; minLocalRequestFreeThreads=&quot;4&quot; appRequestQueueLimit=&quot;100&quot; /&gt;
      &lt;/system.web&gt;
      
      &lt;system.webServer&gt;
         &lt;httpProtocol&gt;
           &lt;customHeaders&gt;
             &lt;add name=&quot;Access-Control-Allow-Origin&quot; value=&quot;*&quot; /&gt;
             &lt;add name=&quot;Access-Control-Allow-Headers&quot; value=&quot;Content-Type&quot; /&gt;
             &lt;add name=&quot;Access-Control-Allow-Methods&quot; value=&quot;GET, POST, PUT, DELETE, OPTIONS&quot; /&gt;
           &lt;/customHeaders&gt;
         &lt;/httpProtocol&gt;
         
          &lt;validation validateIntegratedModeConfiguration=&quot;false&quot; /&gt;
          &lt;directoryBrowse enabled=&quot;true&quot; /&gt;
          
          &lt;handlers&gt;
              &lt;remove name=&quot;ExtensionlessUrlHandler-Integrated-4.0&quot; /&gt;
              &lt;remove name=&quot;OPTIONSVerbHandler&quot; /&gt;
              &lt;remove name=&quot;TRACEVerbHandler&quot; /&gt;
              &lt;add name=&quot;ExtensionlessUrlHandler-Integrated-4.0&quot; path=&quot;*.&quot; verb=&quot;*&quot; type=&quot;System.Web.Handlers.TransferRequestHandler&quot; preCondition=&quot;integratedMode,runtimeVersionv4.0&quot; /&gt;
          &lt;/handlers&gt;
      &lt;/system.webServer&gt;
  
      &lt;system.web.extensions&gt;
          &lt;scripting&gt;
              &lt;webServices&gt;
                  &lt;jsonSerialization maxJsonLength=&quot;2147483647&quot; /&gt;
              &lt;/webServices&gt;
          &lt;/scripting&gt;
      &lt;/system.web.extensions&gt;
      
      &lt;connectionStrings&gt;
          ---all my connection strings
      &lt;/connectionStrings&gt;
      &lt;entityFramework&gt;
          &lt;defaultConnectionFactory type=&quot;System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework&quot; /&gt;
          &lt;providers&gt;
              &lt;provider invariantName=&quot;System.Data.SqlClient&quot; type=&quot;System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer&quot; /&gt;
          &lt;/providers&gt;
      &lt;/entityFramework&gt;
      
  &lt;/configuration&gt;
  </code></pre>
  <p>What I'm missing?</p>
  <p>Thank you very much</p>