There are many resources online describing how to configure CORS in a .net web application. I have not been able to make anything work.
Here is the current configuration of my API application:
Installed nuget Microsoft.AspNet.WebApi.Cors
updated WebApiConfig.cs:
public static void Register(HttpConfiguration config) {
var cors = new EnableCorsAttribute(origins: "*", headers: "*", methods: "*"); config.EnableCors(cors); config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { action = RouteParameter.Optional, id = RouteParameter.Optional } ); }
I thought the above would be enough to make CORS work for the entire application. It did not. I added the following to web.config:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="*" />
</customHeaders>
</httpProtocol>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
This did not work either.
I added the CORS decorator to each controller:
namespace ABC_API.Controllers
{
[EnableCors(origins: "*", headers: "*", methods: "*")]
public class ABCController : ApiController
This did not work either.
I am attempting to utilize Intuit's QuickBooks API and receive the following CORS Missing Allow Origin error when attempting to get authorized:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource
at https://appcenter.intuit.com/connect/oauth2?client_id=ABkU7xzdcdsg1vSsKZ7NKZeypTST0EmnyvFhSNPMJM3NShYd5r&response_type
=code&scope=com.intuit.quickbooks.accounting&redirect_
uri=http%3A%2F%2Flocalhost%2FDW_QB_API%2Fapi%2Fauth&
state=e2543d40145e4eb30dbc4da099501d13aca48bc30b6529ad6af74acaae330581.
(Reason: CORS header ‘Access-Control-Allow-Origin’ missing)
What am i missing?