Angular 信号器在IIS上不工作(CORS问题错误405)
我开发并安装了AspNetCore应用程序。 在我的开发环境中,一切都很好。 我可以与signalR建立集线器连接并使用它而不会出现任何问题。 但是,当我发布到IIS(10.0)时,出现以下错误:Angular 信号器在IIS上不工作(CORS问题错误405),angular,asp.net-core,iis,cors,signalr,Angular,Asp.net Core,Iis,Cors,Signalr,我开发并安装了AspNetCore应用程序。 在我的开发环境中,一切都很好。 我可以与signalR建立集线器连接并使用它而不会出现任何问题。 但是,当我发布到IIS(10.0)时,出现以下错误: 访问位于的XMLHttpRequest'http://localhost:5000/chart/negotiate“起源”http://localhost:8034'已被CORS策略阻止:对飞行前请求的响应未通过访问控制检查:请求的资源上不存在'access control Allow Origin'
访问位于的XMLHttpRequest'http://localhost:5000/chart/negotiate“起源”http://localhost:8034'已被CORS策略阻止:对飞行前请求的响应未通过访问控制检查:请求的资源上不存在'access control Allow Origin'标头。
在Startup.cs的服务器端,我的配置服务方法包含以下代码:
services.AddCors(选项=>
{
options.AddPolicy(“CorsPolicy”,
builder=>builder
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
.AllowAnyOrigin()
);
});
在配置方法中定义信号机路由
app.usesigner(路由=>
{
routes.MapHub(“/图表”);
});
并添加了cors策略
app.UseCors(“CorsPolicy”);
我试过:
- 切换到不同的端口
- 将集线器连接url更改为部署IIS服务器的端口(8034)
WebSocket connection to 'ws://localhost:8034/chart?id=vzjCtGG-Glo_8i-1j7oFDA' failed: Error during WebSocket handshake: Incorrect 'Sec-WebSocket-Accept' header value
- 启用Https
- 将AllowWallOrigin更改为WithOrigin(“,…)
- 将HttpProtocols添加到web.config
在当地,它是一种魅力:
概述
Request URL: http://localhost:5000/chart/negotiate
Request Method: POST
Status Code: 200 OK
Remote Address: 127.0.0.1:5000
Referrer Policy: no-referrer-when-downgrade
**响应头**
Content-Length: 252
Content-Type: application/json
Date: Thu, 31 Oct 2019 01:51:05 GMT
Server: Kestrel
X-Powered-By: ASP.NET
X-SourceFiles: =?UTF-8?B?QzpcQmFzZW1lbnRMYW5BcHBcQmFzZW1lbnRMYW5BcHBcY2hhcnRcbmVnb3RpYXRl?=
请求标题
Provisional headers are shown
Content-Type: text/plain;charset=UTF-8
Origin: http://localhost:5000
Referer: http://localhost:5000/
Sec-Fetch-Mode: cors
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36
X-Requested-With: XMLHttpRequest
在端口8034上发布到IIS时,它不起作用。
概述:
Request URL: http://localhost:5000/chart/negotiate
Request Method: OPTIONS
Status Code: 405 Method Not Allowed
Remote Address: 127.0.0.1:5000
Referrer Policy: no-referrer-when-downgrade
Content-Length: 0
Content-Type: text/plain
Date: Thu, 31 Oct 2019 01:28:35 GMT
Server: Kestrel
X-Powered-By: ASP.NET
X-SourceFiles: =?UTF-8?B?QzpcQmFzZW1lbnRMYW5BcHBcQmFzZW1lbnRMYW5BcHBcY2hhcnRcbmVnb3RpYXRl?=
Provisional headers are shown
Access-Control-Request-Headers: x-requested-with
Access-Control-Request-Method: POST
Origin: http://localhost:8034
Referer: http://localhost:8034/
Sec-Fetch-Mode: cors
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36
响应标题:
Request URL: http://localhost:5000/chart/negotiate
Request Method: OPTIONS
Status Code: 405 Method Not Allowed
Remote Address: 127.0.0.1:5000
Referrer Policy: no-referrer-when-downgrade
Content-Length: 0
Content-Type: text/plain
Date: Thu, 31 Oct 2019 01:28:35 GMT
Server: Kestrel
X-Powered-By: ASP.NET
X-SourceFiles: =?UTF-8?B?QzpcQmFzZW1lbnRMYW5BcHBcQmFzZW1lbnRMYW5BcHBcY2hhcnRcbmVnb3RpYXRl?=
Provisional headers are shown
Access-Control-Request-Headers: x-requested-with
Access-Control-Request-Method: POST
Origin: http://localhost:8034
Referer: http://localhost:8034/
Sec-Fetch-Mode: cors
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36
请求头:
Request URL: http://localhost:5000/chart/negotiate
Request Method: OPTIONS
Status Code: 405 Method Not Allowed
Remote Address: 127.0.0.1:5000
Referrer Policy: no-referrer-when-downgrade
Content-Length: 0
Content-Type: text/plain
Date: Thu, 31 Oct 2019 01:28:35 GMT
Server: Kestrel
X-Powered-By: ASP.NET
X-SourceFiles: =?UTF-8?B?QzpcQmFzZW1lbnRMYW5BcHBcQmFzZW1lbnRMYW5BcHBcY2hhcnRcbmVnb3RpYXRl?=
Provisional headers are shown
Access-Control-Request-Headers: x-requested-with
Access-Control-Request-Method: POST
Origin: http://localhost:8034
Referer: http://localhost:8034/
Sec-Fetch-Mode: cors
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36
这是我的启动设置:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:5000/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"ChartApp": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:5000/"
}
}
}
我不明白为什么在我发布的环境中会出现405异常。
我允许来自所有来源的请求,因此我希望响应头包含“访问控制允许凭据”=“true”
我缺少什么?发送请求的服务器未正确配置为处理选项请求。该配置与CORS配置不同。尝试使用Postman或curl或其他非浏览器工具向该服务器发送选项请求,您将得到相同的405方法Not Allowed error。现在的答案可能有帮助,也可能没有帮助。