I have two VS projects : one exposing MVC5 controllers, the other being an angular client. I want the angular client to be able to query the controllers. I read numerous thr
Oftentimes, the threads that I read were suggesting several unecessary configuration steps, which created confusion. It's actually very simple...
For the simple purpose of sending a cross site request, from an angular client, to an ASP controller :
The only mandatory modification is to add this in the server's web.config
<system.webServer>
<httpProtocol>
<customHeaders>
<clear />
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type"/>
</customHeaders>
</httpProtocol>
</system.webServer>
The problem is, some browsers don’t yet allow the *
wildcard for Access-Control-Allow-Headers
. Specifically, Firefox 69 and earlier doesn’t. See https://bugzilla.mozilla.org/show_bug.cgi?id=1309358.
So to ensure you get correct behavior in all browsers, the Access-Control-Allow-Headers
value you send back should explicitly list all the header names you actually need to access from your frontend code; e.g., in the case in the question: Access-Control-Allow-Headers: Content-Type
.
A way you can make that happen without needing to hardcode all the header names is: Have your server-side code take the value of the Access-Control-Request-Headers
request header the browser sends, and just echo that into the value of the Access-Control-Allow-Headers
response header your server sends back.
Or else use some existing library to CORS-enable your server. Echoing the Access-Control-Request-Headers
request-header value into the Access-Control-Allow-Headers
response-header value is something most CORS libraries will typically do for you.
I tried in my .net c# mvc app and client app in angular 8 but it is not working. IN web.config, i added
<httpProtocol>
<customHeaders>
<clear />
<add name="Access-Control-Allow-Origin" value="http://localhost:4200" />
<add name="Access-Control-Allow-Headers" value="Content-Type"/>
</customHeaders>
</httpProtocol>
In global.axax.cs file also, i added
void MvcApplication_AuthenticateRequest(object sender, EventArgs e)
{
Context.Response.AddHeader("Access-Control-Allow-Origin", "http://localhost:4200");
}
but it is not working. In chrome in response header it does show as :
Access-Control-Allow-Headers: Content-Type Access-Control-Allow-Origin: http://localhost:4200 Access-Control-Allow-Origin: http://localhost:4200
but in request header it shows as
Access-Control-Request-Headers: access-control-allow-origin,content-type,x-iphoneclientid
whereas it needs to have like
x-iphoneclientid : 8E72FF50-548B
since x-iphoneclientid is the token i validate in my filter class as
protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
if (request == null || request.RequestUri == null)
{
return null;
}
// Write your Authentication code here
IEnumerable<string> monsterApiKeyHeaderValues = null;
// Checking the Header values
if (request.Headers.TryGetValues("x-iphoneclientid", out monsterApiKeyHeaderValues))
and above condition does not find so it rejects it and it does not reach to controller.
I even have put following in my controller
[EnableCors("", "", "*")]
can you guide please
Thanks