Windows 中的自托管 SignalR .Net 服务在同一台服务器上被 CORS 阻止,在其他服务器上工作

Self-hosted SignalR in Windows .Net Service blocked by CORS on same server, works on other servers

我一直在使用从多个生产服务器(现在在 Azure 中)访问的自托管 SignalR Windows 服务 6 年多,没有出现任何问题。我在 Azure 中创建了一个用于开发的相同服务器,但是当我从同一服务器上的浏览器访问 SignalR 时,SignalR 在使用 http:6287 或 https:6286:

时给我以下错误
Access to XMLHttpRequest at 'http://myserver.learn.net:6287/signalr/negotiate?clientProtocol=1.5&xxxxxxx' from origin 'http://myserver.learn.net' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

但是...从其他服务器连接时它可以工作!我使用以下方法开始连接,没有错误:

SignalR = WebApp.Start("http://myserver.learn.net:6287/");
SignalRSSL = WebApp.Start("https://myserver.learn.net:6286/");
(also SignalR = WebApp.Start("*:628x/" for both);

在我的客户端代码中,我包含以下脚本:

<script src="http://myserver.learn.net:6287/signalr/hubs"></script>

当我在相同或不同服务器上的浏览器中输入 url(或 https 版本)时,它会显示 ASP.NET SignalR JavaScript Library v2.3.0-rtm 页面正确!我没有改变地关闭了防火墙,添加了Microsoft.Owin.Host.HttpListener(有人建议)。我还使用 netsh 输入了通配符证书,因此 SignalR 服务可以使用以下方式处理 SSL 连接:

netsh http add sslcert ipport=0.0.0.0:6286 appid={12345678-db90-4b66-8b01-88f7af2e36bf} certhash=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

编辑:我也尝试将 ipport 值更改为服务器的真实内部 IP 以及 public IP,但没有改变。

那么,为什么我不能从同一台服务器访问 SignalR?

我在此处的另一个答案中找到了有效的解决方案。我改变了:

$j.connection.hub.start().done(function () {

收件人:

$j.connection.hub.start({ jsonp: true, xdomain: true }).done(function () {

这对内部和外部客户都有效。 xdomain:true 单独不起作用,但当我添加 jsonp:true 时它起作用了。我不知道为什么,只是它现在可以工作了。