首页 文章

SignalR与客户端证书认证

提问于
浏览
1

我在IIS上托管服务,使用SignalR与客户端 Build 持久连接 .

我正在尝试实现客户端证书身份验证,以便服务器可以根据他们发送的证书验证客户端是否为有效客户端 . 我正在使用Signalr的PersistentConnection .

客户端:我创建一个Connection对象,然后创建一个X.509证书,我通过以下方式添加到连接:

var connection = new Connection("localhost:8060/TheService/connect");
connection.AddClientCertificate(certificate); 
connection.Start().Wait();

当客户端尝试连接时,我想验证它提供的证书是否是有效证书 . 所以在服务器端,我覆盖了SignalR的PersistentConnection中的AuthorizeRequest()方法:

public class MyConnection : PersistentConnection
    {
        protected override Task OnReceived(IRequest request, string connectionId, string data)
        {
            return Connection.Broadcast("Server Received: " + data);
        }

        protected override bool AuthorizeRequest(IRequest request)
        {
            INameValueCollection headers = request.Headers;

            foreach (KeyValuePair<string, string> entry in headers)
            {
                Console.WriteLine("Key: {0}, Value: {1}", entry.Key, entry.Value);
                Console.WriteLine("");
            }

            return true; 
        }
}

我的问题是:如何在AuthorizeRequest方法中检索客户端证书?我需要SignalR以某种方式获得对证书的访问权限,以便我可以验证它AuthorizeRequest,然后返回false(错误证书),或返回true(有效证书) .

谢谢!

1 回答

  • 1

    您应该能够从作为参数获得的请求中获取证书 . HttpClientCertificate cert = request.GetHttpContext().Request.ClientCertificate;

相关问题