I have a controller where i enabled cors
[EnableCors(Startup.CORS_POLICY_EXTERNAL)]
public class MasterController : Controller
{
[HttpGet]
public IActionResult TestaConnessione()
{
return Ok();
}
}
but with a simple fetch:
fetch("url/MasterController/TestaConnessione")
caller get header CORS “Access-Control-Allow-Origin” missing
but if the caller use iframe all works fine:
<iframe name="ifrReport" id="ifrReport"></iframe>
<script type="text/javascript">
(function () {
let form, input;
form = document.createElement("form");
form.action = "URL";
form.target = "ifrReport";
form.method = "POST";
document.body.appendChild(form);
form.submit();
})();
</script>
that's the startup config:
public const string CORS_POLICY_EXTERNAL = "CORS_POLICY_EXTERNAL";
public const string CORS_POLICY_LOCAL = "CORS_POLICY_LOCAL";
services.AddCors(options =>
{
options.AddPolicy(name: CORS_POLICY_EXTERNAL,
builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
options.AddPolicy(name: CORS_POLICY_LOCAL,
builder =>
{
builder.WithOrigins("http://localhost")
.AllowAnyMethod()
.AllowAnyHeader();
});
});
app.UseRouting();
app.UseCors();
app.UseAuthentication();
app.UseMiddleware<AuthenticationMiddleware>();
var options = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(options.Value);
app.UseAuthorization();