这是我得到的错误。
无法加载http://example.net/signalr/negotiate?clientProtocol=1.5&connectionData=%5B%7B%22name%22%3A%22chathub%22%7D%5D&_=1539104978557:所请求的资源上不存在“ Access-Control-Allow-Origin”标头。因此,不允许访问来源“ http://fictitiousite.com”。
这是请求。
Request URL: http://example.net/signalr/negotiate?clientProtocol=1.5&connectionData=%5B%7B%22name%22%3A%22chathub%22%7D%5D&_=1539104978557
Request Method: GET
Status Code: 200 OK
Remote Address: 52.187.135.79:80
Referrer Policy: no-referrer-when-downgrade
这是响应:
Cache-Control: no-cache
Content-Type: application/json; charset=UTF-8
Expires: -1
Pragma: no-cache
这是我的启动课程的样子。
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationMode = AuthenticationMode.Active,
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Index")
});
var hubConfiguration = new HubConfiguration();
hubConfiguration.EnableDetailedErrors = true;
hubConfiguration.EnableJavaScriptProxies = true;
hubConfiguration.EnableJSONP = true;
app.MapSignalR("/signalr", hubConfiguration);
}
}
我有网站“ http://fictitiousite.com”向“ http://example.net”发送请求。
我从'http://fictitiousite.com'接受请求没有问题,唯一不起作用的请求是与信号相关的请求。
这些是我的web.config中的定义。
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Headers" value="Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With" />
<add name="Access-Control-Allow-Origin" value="http://fictitiousite.com" />
<add name="Access-Control-Allow-Methods" value="*" />
<add name="Access-Control-Allow-Credentials" value="true" />
</customHeaders>
</httpProtocol>
这是我global.asax中的启动功能。
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
string[] allowedIps = new string[] {
"52.117.111.111",
"::1"
};
Uri MyUrl = Request.Url;
string host = HttpContext.Current.Request.UserHostAddress;
string cTheFile = HttpContext.Current.Request.Path;
if (!cTheFile.EndsWith("Index") )
{
if (!allowedIps.Contains(host) )
{
if (HttpContext.Current.User == null ||
HttpContext.Current.User.Identity == null ||
!HttpContext.Current.User.Identity.IsAuthenticated
)
{
Response.Redirect("~/Index", true);
Response.End();
return;
}
}
}
}
有什么解决方案?为什么与信号器的连接被拒绝?
更新
以下asp.net mvc解决方案不起作用。
解决方案1:
public void Configuration(IAppBuilder app)
{
app.Map("/signalr", map =>
{
map.UseCors(CorsOptions.AllowAll);
var hubConfiguration = new HubConfiguration
{
};
map.RunSignalR(hubConfiguration);
});
}
'UseCors'函数不存在,即编译错误。
解决方案2:
RouteTable.Routes.MapHubs(new HubConfiguration()
{
EnableCrossDomain = true
});
Visual Studio 2017显示该代码已被弃用。
还有什么其他解决方案?
更新2:
我发现我缺少一个包裹。
using Microsoft.Owin.Cors;
这是我的新解决方案。
app.Map("/signalr", map =>
{
// Setup the CORS middleware to run before SignalR.
// By default this will allow all origins. You can
// configure the set of origins and/or http verbs by
// providing a cors options with a different policy.
map.UseCors(CorsOptions.AllowAll);
var hubConfiguration = new HubConfiguration();
hubConfiguration.EnableDetailedErrors = true;
hubConfiguration.EnableJavaScriptProxies = true;
hubConfiguration.EnableJSONP = true;
// Run the SignalR pipeline. We're not using MapSignalR
// since this branch already runs under the "/signalr"
// path.
map.RunSignalR(hubConfiguration);
});
我会尽快对其进行测试并报告。
启动连接后,我收到此奇怪的消息。
jquery.signalR-2.3.0.min.js:9 WebSocket connection `
> to
> 'ws://localhost:64270/signalr/connect?transport=webSockets&clientProtocol=1.5&connectionToken=pHPJmk40WiuD882m3J0bt4cUn9bdIj96CrPg8fntHfjWm8DdrSnaB5UV7HG3LLQfOzRw9ZRBfzd1BMno39bsGaXyuBdkWNK2W%2FPQNKeUuI2ZP0DDRWs50ba5bQx20vXC&connectionData=%5B%7B%22name%22%3A%22chathub%22%7D%5D&tid=2'
> failed: Error during WebSocket handshake: Unexpected response code:
> 403
连接成功建立(允许我发送消息),但出现此错误。
这是我的配置。
app.Map("/signalr", map =>
{
// Setup the CORS middleware to run before SignalR.
// By default this will allow all origins. You can
// configure the set of origins and/or http verbs by
// providing a cors options with a different policy.
map.UseCors(CorsOptions.AllowAll);
var hubConfiguration = new HubConfiguration();
hubConfiguration.EnableDetailedErrors = true;
hubConfiguration.EnableJavaScriptProxies = true;
// hubConfiguration.EnableJSONP = true;
// Run the SignalR pipeline. We're not using MapSignalR
// since this branch already runs under the "/signalr"
// path.
map.RunSignalR(hubConfiguration);
});
这就是我开始连接的方式。
$.connection.hub.start({ withCredentials: false }).done(function () { });
到达该行之前不会引发任何错误。