SignalR with certificates

I want to create a simple SignalR application. Using http everything works fine, but when I try to use https and certificates, the application does not work.

Currently, the only thing I want to do is get the client certificate on the server.

Server (authentication handler):

public class ClientCertificateAuthenticationHandler : AuthenticationHandler<ClientCertificateAuthenticationOptions> { protected override Task<AuthenticationTicket> AuthenticateCoreAsync() { var cert = Context.Get<X509Certificate>("ssl.ClientCertificate"); if (cert == null) { return Task.FromResult<AuthenticationTicket>(null); } try { Options.Validator.Validate(cert); } catch { return Task.FromResult<AuthenticationTicket>(null); } return null; } } 

Client (hub)

 var connection = new HubConnection("https://localhost:8080/"); connection.AddClientCertificate(X509Certificate.CreateFromCertFile("c1.cer")); IHubProxy myHub = connection.CreateHubProxy("MyHub"); connection.Start().Wait(); 

By doing this, on the server, when I do var cert = Context.Get<X509Certificate>("ssl.ClientCertificate"); I get null .

So what am I doing wrong?

+5
source share
1 answer

You must also tell IIS to use a client certificate. In web.config do:

 <location path="signalr"> <system.webServer> <security> <access sslFlags="SslNegotiateCert" /> </security> </system.webServer> </location> 

my working code

 var cert = ServiceCallHelper.GetClientCertificate(); var url = new Uri(Settings.CoreUrl); var str = String.Format("https://{0}/cert_signalr", url.Host); var hub = new HubConnection(str, false); hub.AddClientCertificate(cert); 

and

 object cert = null; if (!hub.Context.Request.Environment.TryGetValue("ssl.ClientCertificate", out cert) || !(cert is X509Certificate2)) { s_logger.WarnFormat("Hub {0} called without certificate or cookie", hub.Context.Request.ToString()); throw new Exception("not authenticated"); } 
+4
source

Source: https://habr.com/ru/post/1204409/


All Articles