1

我有一个继承的类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);
    }
}
4

2 回答 2

2

考虑更改您的设计以使用信号器公开的方法来验证用户是否经过身份验证并且他们对持久连接具有权限

    protected override bool AuthorizeRequest(IRequest request)
    {
        return request.User != null && request.User.Identity.IsAuthenticated;
    }
于 2013-08-19T23:10:41.347 回答
1

为什么不直接向客户端发送一条消息,告诉它断开连接?例如

在服务器上。

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();
    }
};
于 2013-08-19T19:26:50.240 回答