我们在 IIS 上有两个网站,分别用于 angular_client 和 web_api,托管捆绑包已安装,并且已授予 IUSR、网络、网络服务和Everyone 等用户的权限。
每个站点的导航单独工作,即在 API 中,调用http://localhost:51975/api/用户,结果是所有用户的列表。
问题是 Angular 登录页面无法与 API 通信来验证用户身份:
OPTIONS http://localhost:51975/api/user/authenticate net::ERR_CONNECTION_REFUSED
我尝试将与API关联的IP修改为静态IP,即10.1.x.y,然后告诉Angular调用http://10.1.x.y:51975/api/user/authenticate反而。
在 API 中,我启用了将日志发送到浏览器以及日志文件夹,它显示的唯一日志是:
Hosting environment: Production
Content root path: C:\inetpub\wwwroot\api
Now listening on: http://127.0.0.1:39834
Application started. Press Ctrl+C to shut down.
Application is shutting down...
为什么它在其他端口而不是关联的 51975 上侦听?
为什么 Angular 到 API 的连接被拒绝?
为什么应用程序关闭?
这是执行此操作的正确方法吗?
UPDATE 1
在服务器上ONLY,应用程序运行良好,配置如下:
Angular 位于 10.x.y.z:4200,
API 10.x.y.z:51975,
问题是,当我尝试使用公共IP或主机名时,我仍然只能访问Angular,而不能访问Web API,我尝试为API分配主机名,但它还不起作用!
UPDATE 2
我还在我的主机名域上允许了端口 4200 和 51975。
UPDATE 3
我在 IIS 上托管 Web API,API 和 Angular 位于同一服务器上,但位于不同的网站上。
我删除了有关修复 CORS 的声明,因为我在浏览器控制台上仍然有警告,这是错误:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://{public IP}:51975/api/user/authenticate. (Reason: CORS request did not succeed).[Learn More]
Object { headers: {…}, status: 0, statusText: "Unknown Error", url: "http://{public IP}:51975/api/user/authenticate", ok: false, name: "HttpErrorResponse",
message: "Http failure response for http://{public IP}:51975/api/user/authenticate: 0 Unknown Error", error: error }
OPTIONS Http failure response for http://{public IP}:51975/api/user/authenticate net::ERR_CONNECTION_TIMED_OUT
关于CORS,这是我的配置,在本地运行良好:
//In ConfigureServices method
services.AddCors();
//In Configure method
app.UseCors(options => options.WithOrigins("http://localhost:4200/").AllowAnyMethod().AllowAnyHeader());
谢谢