Missing token 'access-control-allow-headers' in CORS header 'Access-Control-Allow-Headers' from CORS preflight channel

后端 未结 3 903
粉色の甜心
粉色の甜心 2020-12-15 05:39

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

3条回答
  •  一生所求
    2020-12-15 06:37

    I tried in my .net c# mvc app and client app in angular 8 but it is not working. IN web.config, i added

    
      
        
        
        
      
    
    

    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 SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
        {
    
            if (request == null || request.RequestUri == null)
            {
                return null;
            }
            // Write your Authentication code here
            IEnumerable 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

提交回复
热议问题