【发布时间】:2019-10-17 02:00:38
【问题描述】:
我们在 IIS 上为 angular_client 和 web_api 提供了两个网站,托管包已安装,并且已授予用户权限,如 IUSR、网络、网络服务和每个人。
到每个站点的导航单独起作用,即在 API 中,调用 http://localhost:51975/api/user 会生成所有用户的列表。
问题是 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 的连接?
为什么应用程序会关闭?
这是正确的做法吗?
更新 1
在服务器 ONLY 上,应用程序工作正常,具有以下配置:
Angular 在 10.x.y.z:4200,
API 10.x.y.z:51975,
问题是当我尝试使用公共 ip 或我的主机名时,我仍然只能访问 angular 而不能访问 Web API,我试图为 api 分配主机名,但它还没有工作!
更新 2
我还允许我的主机名域上的端口 4200 和 51975。
更新 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());
谢谢
【问题讨论】:
-
您能告诉我您如何使用自托管或 IIS 托管您的 Web api 应用程序吗?这两个应用程序主机在同一台服务器上吗?您能否发布有关使用 Angular 应用程序访问 {publichIP 地址}/api/user/authenticate 的详细错误消息?
-
@BrandoZhang 谢谢,现在已经相应更新了。
-
从邮递员或角度应用程序之外的其他方式访问 WebAPI 有任何问题吗?如果不是,如果您在本地运行 Angular 应用程序并将其指向已发布的 webapi,它的行为是否相同?使用这些方法会出现什么错误?
-
您想同时托管您的两个应用程序:front 和 API 在同一个域上,或者您需要单独托管应用程序?如果您想同时托管两者,那么我可以列出您的步骤。
-
@Amir 您能否列出在同一域上托管的步骤? -- 或指出可以找到它们的位置。
标签: asp.net angular iis asp.net-web-api asp.net-core-webapi