【发布时间】:2020-07-01 18:50:12
【问题描述】:
我的 signalR 集线器可以在安全端口 44311 上的 VS2019 调试模式下正常运行,但是一旦我将其发布到我的开发盒上的本地 IIS,../hub/negotiate 就会失败并出现 CORS policy 异常。
ex.在调试模式下,IIS 托盘
信号器 Hub 启动正常:
现在进入我部署 signalR Hub 项目的本地 Window 10 IIS 设置:
- 我设置了 https 绑定
- 我成功设置了 SSL 证书,并设置了需要 SSL 复选框:
- 我重新启动我的 IIS 网站,然后浏览 44311 进行测试
但果然,我的应用无法连接到 HUB:
Access to XMLHttpRequest at 'https://localhost:44311/hub/negotiate?negotiateVersion=1' from origin 'http://localhost:4200' has been blocked by CORS policy:
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
POST https://localhost:44311/hub/negotiate?negotiateVersion=1 net::ERR_FAILED
然而在我的代码中我注入了CORS:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Connections;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NotificationHub.Hubs;
namespace NotificationHub
{
public class Startup
{
readonly string MyAllowedSpecificOrigins = "_myAllowedSpecificOrigins";
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy(name: MyAllowedSpecificOrigins,
builder => {
builder.WithOrigins("https://localhost:4200", "localhost:4200")
.SetIsOriginAllowedToAllowWildcardSubdomains()
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials()
.SetIsOriginAllowed((host) => true);
}
);
});
services.AddSignalR(hubOptions => {
hubOptions.EnableDetailedErrors = true;
hubOptions.KeepAliveInterval = System.TimeSpan.FromMinutes(1000);
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseHttpsRedirection();
app.UseRouting();
app.UseCors(MyAllowedSpecificOrigins);
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
// app.UseSession();
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<Notifications>("/hub", options => {
options.Transports = HttpTransportType.WebSockets | HttpTransportType.LongPolling;
});
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Notification HUB has started!");
});
});
}
}
}
目前我不确定这是编码问题(即未正确设置 CORS)还是部署问题。
感谢您的帮助。
谢谢。
更新
根据以下建议的答案,我的 CORS 问题在部署到 IIS 后得到解决:
- 为 IIS 安装 cors:https://www.iis.net/downloads/microsoft/iis-cors-module
- 在
<customHeaders>下面添加到web.config
但是,在 IIS 部署模式下,我仍然无法成功连接到集线器 (http://localhost:55271/hub/negotiate?negotiateVersion=1 throws 404 not found);但是调试工作正常。
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://localhost:4200" />
<add name="Access-Control-Allow-Credentials" value="true" />
<add name="Access-Control-Request-Headers" value="User-Agent,Content-Type,Authorization,X-RequestDigest,X-ClientService-ClientTag,XMLHttpRequest,x-requested-with" />
<add name="Access-Control-Allow-Headers" value="User-Agent,Content-Type,Authorization,X-RequestDigest,X-ClientService-ClientTag,XMLHttpRequest,x-requested-with" />
<add name="Access-Control-Request-Method" value="GET,POST,HEAD,OPTIONS" />
</customHeaders>
</httpProtocol>
【问题讨论】:
-
您的问题解决了吗?如果您的问题得到解决,那么我请求您将有用的建议标记为答案。这将帮助面临同样问题的其他人。如果您的问题仍然存在,请尝试参考社区成员提供的解决方案。
-
@JalpaPanchal - 你是对的。但是发生的事情是,在尝试使用 ssl 和 digi 证书部署它后,我破坏了我的整个开发解决方案。然后7月4日假期来了,我终于重建了我的开发环境。我将不得不在本地部署方面尽快重新审视这个问题。
标签: deployment iis-7 signalr signalr-hub