I created the following...
- One backend .NET Core POST API running on IIS
- Two UI apps created with Angular CLI and running with the port 8888 and 9999 respectively
- I have enabled CORS in .NET Core application
While integrating CORS in .NET Core, I have added the following in startup.cs
Startup.cs
public class Startup
{
readonly string _allowSpecificOrigins = "_allowOrigin";
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddCors(policy =>
{
policy.AddPolicy(name: _allowSpecificOrigins, options => options.WithOrigins("http://localhost:9999").AllowAnyHeader().AllowAnyMethod());
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseCors(options => options.WithOrigins("http://localhost:9999").AllowAnyHeader().AllowAnyMethod());
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers().RequireCors(_allowSpecificOrigins);
});
}
}
And CORS is perfectly working as expected. For testing, I have created two angular applications which are running with the two different port 8888 and 9999. PORT 9999 is configured and allowed in the startup.cs to be accessible the API. So, from the PORT 9999, application can access the API while the application with the PORT 8888, could not access the API due to CORS error as you can see the responses.
API response when calling from the url http://localhost:8888
API response when calling from the url http://localhost:9999
Now problem is that, I can still access the same API from POSTMAN after overwriting all the headers
- Accept
- Accept-Encoding
- Connection
- Referer
- or even User-Agent
Please look at the response received from POSTMAN
Any help is greatly appreciated. Thank you.