Estou tendo problemas com CORS policy. Pulbliquei um site no IIS e não coloquei host name (acessava pelo IP). Quando coloquei o host name (www.nome-do-site.com) o sistema parou de funcionar e o problema era:
Access to XMLHttpRequest at 'http://back' from origin 'http://front' 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.
No backend, o Startup.cs está codificado assim no método ConfigureServices:
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddCors(o => o.AddPolicy("MyPolicy",
builder => builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
.Build())
);
services.AddMvcCore(options => { options.RespectBrowserAcceptHeader = true; }).AddJsonFormatters();
services.AddSingleton(_ => Configuration);
services.AddAutoMapper();
services
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.RequireHttpsMetadata = !_iHostingEnvironment.IsDevelopment();
options.SaveToken = true;
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = Configuration["Jwt:Issuer"],
ValidAudience = Configuration["Jwt:Issuer"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Jwt:Key"]))
};
});
services.Configure<IISServerOptions>(options =>
{
options.AutomaticAuthentication = false;
});
DomainServices(services);
Repositories(services);
Factory(services);
ExternalService(services);
}
E assim no método Configure
public void Configure(IApplicationBuilder app)
{
if (_iHostingEnvironment.IsDevelopment())
app.UseDeveloperExceptionPage();
else
app.UseHsts();
app.UseCors("MyPolicy");
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvc(routes => routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}")
);
}
E mesmo assim está dando esse problema de CORS. Lembrando antes de mudar de "http://localhost/login" para "http://www.nome-do-site.com/login" não tinha esse problema.