ASP.NET CORS Configuration

Error Message

The server returns: "The 'Access-Control-Allow-Origin' header is present on the requested reosurce"

Solution

Web.config Settings

<appSettings>
  <add key="cors_allowedOrigins" 
       value="http://localhost:8002,http://192.168.0.1:8002" />
</appSettings>

<system.webServer>
  <handlers>
    <remove name="WebDAV" />
    <remove name="OPTIONSVerbHandler" />
  </handlers>
</system.webServer>

CORS Initialization Code

public static class CorsSetup
{
    public static void Configure(HttpConfiguration httpConfig)
    {
        string origins = ConfigurationManager.AppSettings["cors_allowedOrigins"];
        var corsSettings = new EnableCorsAttribute(origins, "*", "*")
        {
            SupportsCredentials = true
        };

        httpConfig.EnableCors(corsSettings);
    }
}

Wildcard Domain Configuration

To allow all domains, configure credentials support as false and use a wildcard orrigin:

var corsSettings = new EnableCorsAttribute("*", "*", "*")
{
    SupportsCredentials = false
};

Tags: ASP.NET cors Web.config WebAPI

Posted on Wed, 12 Aug 2026 16:40:54 +0000 by MatrixGL