这个问题不是这的翻版,虽然上面提到的问题有一些非常有趣的答案,帮助我更好地理解了我的问题,但它不能满足我的需要。让我解释一下我的情况。
我有一个asp.net mvc网站,具有通知功能,并通过signalR和SQL实时更新数据。用户身份验证是通过使用Identity 2.0完成的。只允许授权用户查看更新的数据/通知。此外,通知/更新因用户而异。我通过实现自定义UserId提供程序和使用标识的UserId实现了这一点。
现在我想实现以下目标。
在这种情况下,最好的行动方针是什么?
更新,因为最初的问题被搁置了,因为没有指定细节,所以下面是我想要得到建议的细节。
我有一个在localhost上运行的asp.net mvc网站:54603,以下是家庭控制器的操作
public ActionResult Index()
{
HttpContext.Response.AppendHeader("Access-Control-Allow-Origin", "*");
return View();
}索引视图上有一些signalR功能。下面是允许CORS的signalR启动配置,因为我的目标是向客户端公开索引页及其全部功能(运行php等)。
app.Map("/signalr", map =>
{
map.UseCors(CorsOptions.AllowAll);
var hubConfiguration = new HubConfiguration
{
// You can enable JSONP by uncommenting line below.
// JSONP requests are insecure but some older browsers (and some
// versions of IE) require JSONP to work cross domain
// EnableJSONP = true
};
map.RunSignalR(hubConfiguration);
});我已经创建了notifcationHub,下面是我在前端的代码(使用代理)。
<script>
$(function () {
// Reference the auto-generated proxy for the hub.
$.connection.notificationHub.url = 'http://localhost:54603/signalr';
var notification = $.connection.notificationHub;
// Client side method for receiving the list of notifications on the connected event from the server
notification.client.refreshNotification = function (data) {
$("#notificationTab").empty();
$("#cntNotifications").text(data.length);
for (var i = 0; i < data.length; i++) {
$("#notificationTab").append("<tr> <td> " + data[i].Id + "</td> <td>" + data[i].Text + "</td> <td>" + data[i].CreatedDate + "</td></tr>");
}
}
//Client side method which will be invoked from the Global.asax.cs file.
notification.client.addLatestNotification = function (data) {
$("#cntNotifications").text($("#cntNotifications").text() + 1);
$("#notificationTab").append("<tr> <td> " + data.Id + "</td> <td>" + data.Text + "</td> <td>" + data.CreatedDate + "</td></tr>");
}
// Start the connection.
$.connection.hub.start().done(function () {
//When the send button is clicked get the text and user name and send it to server.
$("#btnSend").click(function () {
notification.server.sendNotification($("#text").val(), $("#userName").val());
});
console.log($.connection.hub.id);
}).fail(function (data) { console.log('Could not connect' + data); });
});
</script>Html如下所示
@{
ViewBag.Title = "Home Page";
Layout = null;
}
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/signalr/jquery.signalr-2.2.0.min.js"></script>
<script src="@Url.Content("~/signalr/hubs")"></script>
@*<script src="~/signalr/hubs"></script>*@
<div style="width: 70%; padding: 20px">
<div class="panel panel-primary">
<div class="panel-heading">
<! – To show notification count-->
<div style="float: left" class="panel-title">Notifications</div>
<div style="float: right" class="badge" id="cntNotifications"></div>
<div style="clear: both"></div>
</div>
<div class="panel-body">
<! – To show All the notifications-->
<table class="table table-striped table-hover ">
<thead>
<tr>
<th>#</th>
<th>Text</th>
<th>Created Date</th>
</tr>
</thead>
<tbody id="notificationTab"></tbody>
</table>
</div>
</div>
<! – Add panel notification to send notification, Make sure that user enters the user id of the domain they are logged into -->
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Create Notifications</h3>
</div>
<div class="panel-body">
<div class="form-group">
<label class="control-label" for="focusedInput">Notification Text</label>
<input class="form-control" id="text" type="text" value="">
</div>
<div class="form-group">
<label class="control-label" for="focusedInput">Send To</label>
<input class="form-control" id="userName" type="text" value="">
</div>
<a id="btnSend" style="cursor: pointer" class="btn btn-primary">Send Notification</a>
</div>
</div>
</div>页面按照预期运行良好,连接id正在登录到控制台。

现在我已经创建了另一个html页面,下面是它的代码
<html>
<head>
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/signalr/jquery.signalr-2.2.0.min.js"></script>
</head>
<body>
<aside>
<div class="col-lg-2">
<ul>
<li>this</li>
<li>is</li>
<li>aside</li>
</ul>
</div>
</aside>
<article>
<div class="col-lg-10">
<div id="something">
</div>
</div>
</article>
<script src="http://localhost:54603/signalr/hubs"></script>
<script>
$(document).ready(function () {
$.ajax({
url: "http://localhost:54603/Home/Index",
dataType: 'html',
crossDomain: true,
cache:true
}).done(function (data) {
$('#something').html(data);
});
});
</script>
</body>
</html>Ajax请求正在返回html,但是signalR会抛出错误。下面是控制台消息。
获得http://localhost:54603/Home/Index 200 OK 1.18s 无法连接错误:协商请求时出错。
我做错了什么导致了这个错误?
发布于 2015-10-13 13:28:51
很难准确地确定你需要做什么,因为这个问题相当广泛,但我希望这些观点在某种程度上有所帮助(编辑):
SignalR跨域在客户端使用JavaScript,在服务器端使用CORS,因此您将在这方面进行讨论。您可以轻松地创建一个JavaScript文件,该文件可以连接到SignalR通道,而不需要对PHP代码进行任何更改,只需将JavaScript包含在客户端的页面中。
您可以很容易地在您的服务器上创建一个脚本,客户端可以在其页面上引用该脚本,从而将所需的所有内容都拖到客户端的页面上。因为脚本是从您自己的服务器上执行的,所以我不认为您需要担心CORS,因为它不是真正的“跨站点脚本”,因为执行请求的代码将由您的服务器/域“拥有”。
至于身份验证,如果只有您的脚本需要访问身份验证,我认为这意味着您也可以使用域的身份验证(cookie等)。不过,我得去查一下那个。最终,您会使用HTTPs请求从包含的脚本对域进行身份验证吗?
下面是一些有用的线程,它们可能有助于解决这个问题:
https://stackoverflow.com/questions/33102704
复制相似问题