【发布时间】:2013-09-24 10:20:04
【问题描述】:
使用 SignalR V2 RC1 我有一个 SignalR 自托管控制台应用程序,它允许位于绑定到 http://*:8080 的 Server1 上的 CORS 连接。
在 Server2 上,我有一个使用 Windows 身份验证的 MVC 4 Intranet 网站。
当用户第一次通过 Server3 上的浏览器点击默认页面时,MVC 控制器会分配一个 cookie。我希望将此 cookie 从浏览器传递到 Server1 上的 SignalR 应用程序。
通过 VS2012 在本地执行此操作时,我可以清楚地看到在浏览器中设置的 cookie,然后在每个连接上传递给自托管应用程序(在 VS2012 的另一个实例中运行)。
在上述服务器上完成后,cookie 不再随 SignalR 请求一起发送。
这个问题似乎不是 IE9 特有的(我知道 IE9 在通过 CORS 连接传递 cookie 时遇到问题,根据 SignalR IE9 Cross Domain request don't work),因为我也在 Firefox 上尝试过,结果相同。
https://github.com/SignalR/SignalR/issues/1735 给我的印象是,关于 CORS 和 cookie 的问题已在 SignalR v2 RC1 中得到解决。那我做错了什么?
我已经通过将 cookie 详细信息放入查询字符串来解决此问题,但我宁愿通过跨域传递 cookie 来使其正常工作。
为了完整起见,SignalR 自托管应用程序配置如下,尽管我猜这确实与客户端发送 cookie 方面发生的事情无关
public class StartupSignalR
{
public void Configuration(IAppBuilder app)
{
app.Map("/signalr", map =>
{
map.UseCors(CorsOptions.AllowAll);
var hubConfiguration = new HubConfiguration
{
EnableJSONP = true
};
map.RunSignalR(hubConfiguration);
});
}
}
客户端js如下
<script src="Scripts/jquery-1.8.2.min.js"></script>
<!--Reference the SignalR library. -->
<script src="Scripts/jquery.signalR-2.0.0-rc1.min.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="http://10.144.20.150:8080/signalr/hubs"></script>
<!--Add script to update the page and send messages.-->
<script src="Scripts/jquery.cookie.js"></script>
<script type="text/javascript">
$(function () {
//Set the hubs URL for the connection
$.connection.hub.url = "http://10.144.20.150:8080/signalr";
$.connection.hub.qs = { 'Token': $.cookie("Token") };
// Declare a proxy to reference the hub.
var chat = $.connection.myHub;
// Create a function that the hub can call to broadcast messages.
chat.client.addMessage = function (name, message) {
// Html encode display name and message.
var encodedName = $('<div />').text(name).html();
var encodedMsg = $('<div />').text(message).html();
// Add the message to the page.
$('#discussion').append('<li><strong>' + encodedName
+ '</strong>: ' + encodedMsg + '</li>');
};
// Get the user name and store it to prepend to messages.
$('#displayname').val(prompt('Enter your name:', ''));
// Set initial focus to message input box.
$('#message').focus();
// Start the connection.
$.connection.hub.start({withCredentials:true}).done(function () {
$('#sendmessage').click(function () {
// Call the Send method on the hub.
chat.server.send($('#displayname').val(), $('#message').val());
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
});
});
});
【问题讨论】:
标签: .net asp.net-mvc-4 cross-domain signalr cors