Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

490
Visualizações
CORS PST request works in IIS Express but not IIS

I am having a CORS issue on an IIS web server. I am able to run the below code on my local dev machine running IIS Express (changing appropriate ports and URLs) but I am getting a cors error when I try an access the app once its deployed. The error (I know all over the internet) is

Access to fetch at 'https://example.com:5001/api/Person/PostPerson' from origin 'https://example.com:44337' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

I am working on a .NET core 5.0 C# Web API app project. Below is my Person model that I am trying to POST.

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

Below is my person controller

[Route("api/[controller]/[action]")]
[ApiController]
public class PersonController : ControllerBase
{
    private JsonSerializerSettings _jsonSettings;

    [EnableCors("mypolicy")]
    [HttpPost]
    public string PostPerson([FromBody]Person person)
    {
        return JsonConvert.SerializeObject("success", _jsonSettings);
    }
}

Below is the startup.cs methods.

ConfigureServices

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();
        services.AddCors(options =>
        {
            options.AddDefaultPolicy(
                builder => builder.WithOrigins("https://localhost:44363").AllowAnyHeader());
            options.AddPolicy("mypolicy", builder =>
                builder.WithOrigins("https://example.com:44337").AllowAnyHeader());
        });

Configure method

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseRouting();
            app.UseCors();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers().RequireCors("mypolicy");
            });
        }

I also checked connectivity to the api from my laptop using Postman

enter image description here

I appreciate your time, Thanks!

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

remove all your cors code and place this at the top of ConfigureServices

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

replace yours with this

app.UseRouting();

app.UseCors("AllowAnyOrigins");

app.UseAuthorization();

only if this code is working you can try to replace builder.AllowAnyOrigin() with this

 builder.WithOrigins("http://localhost:44363",
                       "https://example.com")
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda