Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.5k views
in Technique[技术] by (71.8m points)

asp.net - Azure API - How do I setup CORS

I have REST API, that is hosted in Azure. If I make request in interactive console with GET method ('/api/pets'), request goes through just fine. But when I make POST request (POST '/api/pets'), CORS error appears.

Response in interacive console throws this error: Response in interacive console throws this error

startup.cs

        public void ConfigureServices(IServiceCollection services)
        {
        
            .
            .
            .

            services.AddCors(options => options.AddPolicy("CorsPolicy",
                builder =>
                {
                    builder
                        .AllowAnyOrigin()
                        .AllowAnyHeader()
                        .AllowAnyMethod();
                }));
            .
            .
            .
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {

            app.UseCors("CorsPolicy");
            .
            .
            .
        }

All API's CORS policy

<policies>
    <inbound>
        <cors>
            <allowed-origins>
                <origin>*</origin>
            </allowed-origins>
            <allowed-methods preflight-result-max-age="300">
                <method>GET</method>
                <method>POST</method>
                <method>PUT</method>
                <method>DELETE</method>
                <method>HEAD</method>
                <method>OPTIONS</method>
                <method>PATCH</method>
                <method>TRACE</method>
            </allowed-methods>
            <allowed-headers>
                <header>*</header>
            </allowed-headers>
            <expose-headers>
                <header>*</header>
            </expose-headers>
        </cors>
    </inbound>
    <backend>
        <forward-request />
    </backend>
    <outbound />
    <on-error />
</policies>

I also tried setting CORS policy on azure portal only in specific API, but it doesn't work Everything works fine locally.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Can you try configure this using the portal?

  1. Open your API in the Azure API Management section of the Azure portal
  2. Select All operations, or a single operation
  3. On the right, choose Inbound processing > Add policy
  4. You will get a list of prefab policy templates. Choose the "CORS" one and configure it at will:

UPDATE:

Can you modify the XML as below

 <cors>
     <allowed-origins>
        <origin>*</origin>
     </allowed-origins>
     <allowed-methods>
        <method>*</method>
      </allowed-methods>
         <allowed-headers>
      <header>*</header>
         </allowed-headers>
       <expose-headers>
           <header>*</header>
       </expose-headers>
  </cors>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...