ASP.NET Core CORS request blocked; why doesn't my API apply the right headers?

前端 未结 4 1480
粉色の甜心
粉色の甜心 2021-01-02 10:26

Trying to set up CORS with authentication. I have a Web API site up at http://localhost:61000 and a consuming web application up at http://localhost:62000. In the Web API S

4条回答
  •  旧时难觅i
    2021-01-02 11:02

    In my case the CORS headers were lost because I was making an "application/json" content type request, and in CORS this type of request send first an OPTIONS method, after that, the regular POST is requested. But the OPTIONS was been managed by a Middleware code in my .Net Core pipeline with something like this:

            if (context.Request.Method == "OPTIONS")
            {
                context.Response.StatusCode = (int)HttpStatusCode.OK;
                await context.Response.WriteAsync(string.Empty);
            } 
    

    Once I remove the middleware those requests were flawless attended.

提交回复
热议问题