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

后端 未结 3 901
粉色の甜心
粉色の甜心 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:24

    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 :

    • No angular interceptors are required.
    • No custom filters on the server side are required.
    • 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>
      
    0 讨论(0)
  • 2020-12-15 06:35

    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.

    0 讨论(0)
  • 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

    <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

    0 讨论(0)
提交回复
热议问题