简体   繁体   中英

Configure IIS to use CORS in ASP.Net Core API

I have created an ASP.Net Core API. I want to deploy it on my server, and then access it from my local host. My server uses IIS. Based on this document, I added cors in my Startup.cs like this:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddCors(options =>
        {
            options.AddPolicy(name: MyAllowSpecificOrigins,
                            builder =>
                            {
                                builder.WithOrigins("http://localhost:3000")
                                    .AllowCredentials();
                            
                            });
        });
        ...

    }
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
          ...
          app.UseCors(MyAllowSpecificOrigins);
          ...
    }

and then I uploaded my API to the host that uses IIS. But when I tried to access it, I got a 404 error. After a while, I figured out I should configure IIS to accept cors too. So based on this document I added these lines to the web.config file in the root of published files directory:

<cors enabled="true">
    <add origin="http://localhost:3000" allowCredentials="true">
    <allowMethods>
        <add method="GET" />
        <add method="HEAD" />
        <add method="POST" />
        <add method="PUT" /> 
        <add method="DELETE" />        
         <add method="OPTION" />        
    </allowMethods>
  </add>
</cors>

and the error resolved. But now, whenever I publish my project, the web.config goes back to default form (without cors tag) and I have to add cors configuration to the file manually. Where should I apply IIS cors module configuration in the project (not published files) so that whenever I publish the project, the web.config file be created automatically with cors config. I guess it might need some configuration in appsettings.json file but I have no idea how to do it.

The problem was that I didn't have a web.config in my project. I added this file in the root of my project and put this code in it:

    <?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <cors enabled="true">
        <add origin="http://localhost:3000" allowCredentials="true">
          <allowMethods>
            <add method="GET" />
            <add method="HEAD" />
            <add method="POST" />
            <add method="PUT" />
            <add method="DELETE" />
            <add method="OPTION" />
          </allowMethods>
        </add>
      </cors>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="dotnet" arguments=".\PaykanPars.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
    </system.webServer>
  </location>
  <system.web>
    <compilation tempDirectory="C:\Inetpub\vhosts\paykanpars.com\tmp" />
  </system.web>
</configuration>

and this fixed the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM