I have been having trouble getting my CORS requests to work between an asp net core API and an Angular 8 app using a generated Nswag typescript client.
Just now I figured out why. My origin is "null"
. Not http://localhost:4200
as I expected it to be.
So when I added "null" as an allowed origin everything works. But I want to understand why, and what implications this would have if I left let "null" be an allowed origin in production.
Currently my code looks like this.
appsettings.json
"CORS-Settings": {
"Allow-Origins": [
"http://localhost",
"http://localhost:4200",
"https://localhost",
"https://localhost:4200",
"null" // <-- if I remove this my localhost browser gets denied by CORS
],
"Allow-Methods": [ "OPTIONS", "GET", "HEAD", "POST", "PUT", "DELETE" ]
}
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
// other stuff
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceScopeFactory scopeFactory, ILoggerFactory loggerFactory)
{
var allowedMethods = Configuration.GetSection("CORS-Settings:Allow-Methods").Get<string[]>();
var allowedOrigins = Configuration.GetSection("CORS-Settings:Allow-Origins").Get<string[]>();
app.UseCors(
options => options
.WithOrigins(allowedOrigins)
.WithMethods(allowedMethods)
);
// other stuff
}
So why do i need to add "null" as an allowed origin, and is this harmful if it were to make it to production?
If it makes any difference, I'm using firefox v72.0.1.