I am currently writing an angular app which is communicated with a server part developed with Web API 2.
Some times I want to add a specific header in the response of the query. I have enabled CORS.
When the call is made from the same origin, no problem, the value is in the response header and I can threat it in the angular side. When the call is made from another origin, the header name is not available in the response.
In both cases, when I inspect the queries through the developer tools in Chrome, I see the headers in the response.
Any idea from what I am doing wrong? Is there something special to do in the Angular part?
For test purposes, I simply do that in Web API:
public static void Register(HttpConfiguration config)
{
var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);
.....
// rest of the implementation (Routes, etc...)
}
UPDATE
I have written a ICorsPolicyProvider :
public class IkCorsPolicyProvider : ICorsPolicyProvider
{
private CorsPolicy CreateCorsPolicy()
{
CorsPolicy policy = new CorsPolicy
{
AllowAnyMethod = true,
AllowAnyHeader = false,
AllowAnyOrigin = false
};
// Some code to get the client allowed origins
//.....
//
// set the allowed origins to the policy
foreach (string allowedOrigin in allowedOrigins)
{
policy.Origins.Add(allowedOrigin);
}
policy.Headers.Add("content-type");
policy.Headers.Add(UserContext.Key);
policy.ExposedHeaders.Add("content-type");
policy.ExposedHeaders.Add(UserContext.Key);
return policy;
}
public Task<CorsPolicy> GetCorsPolicyAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
CorsPolicy policy = CreateCorsPolicy();
return Task.FromResult(policy);
}
}
Then in Register:
public static void Register(HttpConfiguration config)
{
config.EnableCors(new IkCorsPolicyProvider());
.....
// rest of the implementation (Routes, etc...)
}