Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi folks,

I have a .net core api and front end is angular. when i deploy the API in server i am getting below cors issue. i have implemented so many things as per google results. but still didn't get the solution. please help to close this.

Access to fetch at 'api end point' from origin 'https://webapp.io' 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.


What I have tried:

1) Enabled the cors policy in .net core globally
2) Applied headers in is (method, origin,x-powered-by )
3) Enabled the cors with default origin
Posted
Updated 23-Nov-20 18:45pm

1 solution

Your API is not returning a CORS header.

Since we can't see your code, we can't tell you why. All we can tell you is what the error message is already telling you. The response from your API does not contain an Access-Control-Allow-Origin header.
 
Share this answer
 
Comments
sencsk 24-Nov-20 14:02pm    
Below is my code for reference in startup file.

Configureservice:
services.AddCors(options =>
{
options.AddPolicy("AllowAll",
builder =>
{
builder.WithOrigins("https://webapp.io/", "https://www.webapp.io/")
.SetIsOriginAllowed((host) => true)
.AllowAnyHeader()
.AllowAnyMethod();
});
});
//CORS

services.AddControllers();


Configure:
public async void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
//var builder = new Microsoft.Extensions.Configuration.ConfigurationBuilder();
//builder.SetBasePath(env.ContentRootPath)
// .AddJsonFile("configuration.json", optional: false, reloadOnChange: true)
// .AddEnvironmentVariables();

//Configuration = builder.Build();
//CORS
//app.UseCors(builder => builder.AllowAnyHeader().AllowAnyMethod().SetIsOriginAllowed((host) => true).AllowCredentials());
app.UseCors("AllowAll");
app.UseMiddleware<corsmiddleware>();
// END CORS

await app.UseOcelot();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
loggerFactory.AddLog4Net();
}
}
Richard Deeming 25-Nov-20 3:59am    
Enable Cross-Origin Requests (CORS) in ASP.NET Core | Microsoft Docs[^]
"The call to UseCors must be placed after UseRouting, but before UseAuthorization."
Your UseCors call is placed before UseRouting.

You also seem to have some additional middleware, which may be conflicting:
app.UseMiddleware<corsmiddleware>();


NB: Your CORS headers will allow requests from any site hosted on the webapp.io domain, with or without the www. prefix.
sencsk 26-Nov-20 1:10am    
Still no luck. I have tried all 3 implementations based on the shared url.i am attaching the startup.cs https://drive.google.com/file/d/1YN83mkgXWbeR26cz-D3tWNID-Zrqic_x/view?usp=sharing

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900