Click here to Skip to main content
15,940,246 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have an angular app deployed locally on IIS port 80, and an ASP.net core web API deployed on IIS locally port 8080.

I get the below error from dev toools console
registration:1 Access to XMLHttpRequest at 'http://localhost:8080/api/User/register' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.


Question is ,What may I be missing?

What I have tried:

I have configured CORS as below

builder.Services.AddCors(option =>
{
    option.AddPolicy("AllowMyOrigin", builder =>
    {
        builder.WithOrigins("http://localhost:80");
        //builder.AllowAnyOrigin();
        builder.AllowAnyHeader();
        builder.AllowAnyMethod();
    });
});
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

//app.UseSwagger();
//app.UseSwaggerUI();

app.UseCors("AllowMyOrigin");

app.UseAuthorization();

app.MapControllers();

app.Run();
Posted

1 solution

The Cross-Origin Resource Sharing (CORS) issue is related to the web browser is blocking a web page from making requests to a different domain than the one that served the web page. Ensure that your server is configured to include the appropriate CORS headers. Specifically, include the 'Access-Control-Allow-Origin' header in the response with the value set to 'http://localhost:4200' in your case. Further, you can see the different ways to Fix the CORS Error:
https://medium.com/@dtkatz/3-ways-to-fix-the-cors-error-and-how-access-control-allow-origin-works-d97d55946d9[^]
You can also refer the .NET CORE part in the provided link
https://stackoverflow.com/questions/56328474/origin-http-localhost4200-has-been-blocked-by-cors-policy-in-angular7[^]
 
Share this answer
 

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