【发布时间】:2018-01-13 11:39:00
【问题描述】:
我正在开发一个应用程序,我的服务器在 Azure 平台上作为 Api 应用程序运行,我想使用 signalR 我收到此错误
XMLHttpRequest cannot load http://sitename.net/signalr/negotiate?clientProtocol=1.5&connectionData=%5B%5D&_=1501957673711. The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'. Origin 'http://localhost:35609' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
我的服务器是使用owin启动的,这里是配置
app.Map("/signalr", map =>
{
map.UseCors(CorsOptions.AllowAll);
var hubConfiguration = new HubConfiguration {};
hubConfiguration.EnableJSONP = true;
map.RunSignalR(hubConfiguration);
});
在我的 webConfig 中我尝试了这个
var cors = new EnableCorsAttribute("*", "*", "*")
{
SupportsCredentials = true
};
config.EnableCors(cors);
但这并不能解决错误。 如果有人知道我缺少什么,请帮助..谢谢
在我的客户端我使用这样的 angularjs
var connection = null;
this.initialize = function () {
connection = $.hubConnection("http://name.azurewebsites.net/signalr"{ useDefaultPath: false });
this.proxy = connection.createHubProxy('myHub');
connection.start().done(function () {
console.log("started");
}).fail(function (result) {
console.log(result);
});
};
在我的 azure api 应用程序中,我已将我的本地域添加到允许的 CORS。我可以做其他事情,但我不能使用signalR
【问题讨论】:
-
是的,它是一样的,但它在 Nodejs 中,我不知道如何阅读 Nodejs,我怎样才能使它在我的解决方案中有用......
-
CORS 和 Access-Control-Allow-Credentials 标头只能通过代码添加,并确保不使用 Azure 门户中的 CORS 设置,因为它会覆盖代码中的设置。详情请查看stackoverflow.com/questions/36860423/…
标签: angularjs azure signalr asp.net-web-api2 azure-web-app-service