I have an Angular 9 frontend and a VB.NET Web API 2 backend.
When running both locally it works fine, however when they are deployed all the POST requests are getting blocked by CORS. (GET works fine, though that seems to be simply because they do not trigger the preflight check and so CORS is not relevant. Please correct me if I'm wrong on this assumption!)
The frontend is at mydomain.com and the backend on a subdomain at api.mydomain.com. They are both hosted on the same server (Godaddy shared Windows hosting, IIS 8 via Plesk)
I have this in my web.config:
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="GET, PUT, POST, DELETE, OPTIONS" />
<add name="Access-Control-Allow-Headers" value="Content-Type, Accept, authorization" />
</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>
And this in Global.asax:
Protected Sub Application_BeginRequest()
If Request.Headers.AllKeys.Contains("Origin") AndAlso Request.HttpMethod = "OPTIONS" Then
Response.Flush()
End If
End Sub
When sending a POST request I get these in the console:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.mydomain.com/xyz. (Reason: CORS preflight response did not succeed). Status code: 405.
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.mydomain.com/xyz. (Reason: CORS request did not succeed). Status code: (null).
If I check the headers in the Network tab in browser dev tools, the "Origin" header in the request has the same value as the "access-control-allow-origin" header in the response.
Why is this not working, even though the allowed origin is set to "*"? (Changing that will be the next fun problem...)
It seems to be a common issue as I have read through umpteen SA questions and other articles but still cannot find what's missing!