简体   繁体   中英

CORS setup ASP.NET Core and Angular web api

I'm developing a web application using Angular client-side and ASP.NET Core server-side.

The frontend is configured to run on port 4200 while the backend runs on port 5000 of the same address. The development environment is a virtual machine (hosted in VirtualBox) running Debian and while I perform requests from frontend to backend from the development machine everything works perfectly.

But when I forward requests from the PC hosting Virtualbox (Windows10) the request says "Connection time out" if run on Chrome and "CORS error" if run on Firefox.

Server side I've enabled CORS in ConfigureServices :

services.AddCors(c =>
    c.AddPolicy("AllowOrigin", options => options.WithOrigins("http://192.168.0.88:4200")
.AllowAnyHeader()
.AllowAnyMethod));

and in Configure :

app.UseRouting();
app.UseCors(c => c.WithOrigins("http://192.168.0.88:4200")
.AllowAnyHeader()
.AllowAnyMethod());

I can't understand why I'm getting a CORS error if those CORS policies are apparently configured.

Any clue?

You have a bug, you can try this, maybe it will work, if you are lucky, since Cors is a very tricky staff

app.UseRouting();
app.UseCors("AllowOrigin");

//app.UserAuthorization();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM