title: Handling CORS Preflight in Asp.net Web API
body: <p>I have three applications in my architecture.<br>
  They are on the same server but having different port numbers. </p>
  
  <pre><code>A - Token Application (port 4444) - Asp.net WebApi
  B - API Application   (port 3333) - Asp.net WebApi
  C - UI Application    (port 2222) - AngularJS App.
  </code></pre>
  
  <p>The application flow is like below</p>
  
  <p>1- The UI project gets the token from Token Application (It requires Windows Auth.) 
  Ex : <code>awxrsdsaWeffs12da</code></p>
  
  <p>2- UI application puts this token to a custom header which is named as "accessToken"</p>
  
  <p>Ex : accessToken : <code>awxrsdsaWeffs12da</code></p>
  
  <p>3- UI application sends a request to API Application
  Ex: <code>http:myaddress:3333/api/TheRestServiceHere</code></p>
  
  <p>UI application gets 401 Error. 
  Which sends OPTIONS method. (I guess preflight issue)</p>
  
  <p>In my web api project I enabled Cors like this below. </p>
  
  <pre><code>public static void Register(HttpConfiguration config)
  {
              ....
  
              //CORS
              var cors = new EnableCorsAttribute("*", "*", "*");
              config.EnableCors(cors);
  
              ....
  }
  </code></pre>
  
  <p>Config</p>
  
  <pre><code>   public static class WebApiConfig
      {
          public static void Register(HttpConfiguration config)
          {
  
              //CORS
              var cors = new EnableCorsAttribute("*", "*", "*");
              config.EnableCors();
  
  
              // Web API routes
              config.MapHttpAttributeRoutes();
  
              config.Routes.MapHttpRoute(
                  name: "DefaultApi",
                  routeTemplate: "api/{controller}/{id}",
                  defaults: new { id = RouteParameter.Optional }
              );
  
  
              var json = config.Formatters.JsonFormatter;
              json.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.None;
              json.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
              json.SerializerSettings.Formatting = Formatting.None;
              json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
  
              config.Formatters.Remove(config.Formatters.XmlFormatter);
          }
      }
  </code></pre>
  
  <p>So I am looking for a solution to call API application (B) controllers
  and get 200 :)</p>
  
  <p>Regards</p>