我有一个继承PersistentConnection
的类。当我覆盖OnConnected
时,我会检查传入的一些查询字符串参数,以确保用户已通过身份验证。如果不是,我抛出异常,但仍然认为客户端是连接的。如何从连接的客户端列表中删除客户端?
public class NotificationConnection : PersistentConnection
{
protected override Task OnConnected(IRequest request, string connectionId)
{
if (String.IsNullOrWhiteSpace(request.QueryString["example"]))
throw new SecurityException("whatever");
return base.OnConnected(request, connectionId);
}
protected override Task OnDisconnected(IRequest request, string connectionId)
{
return base.OnDisconnected(request, connectionId);
}
}
答案 0 :(得分:2)
考虑更改您的设计以使用signalr公开的方法来验证用户是否经过身份验证,并且他们对持久连接拥有权利
protected override bool AuthorizeRequest(IRequest request)
{
return request.User != null && request.User.Identity.IsAuthenticated;
}
答案 1 :(得分:1)
为什么不直接向客户端发送消息告诉它断开连接? e.g。
在服务器上。
if (String.IsNullOrWhiteSpace(request.QueryString["example"]))
{
Connection.Send(connectionId, "Close");
}
然后在JS客户端做类似的事情;
connection.received(function(data) {
if ( data === "Close" ){
connection.stop();
// send the user to another page with window.location or warn them that their connection has been stopped.
}
});
在.net客户端上;
connection.Received += data =>
{
if ( data == "Close" )
{
connection.stop();
}
};