Search code examples
.netamazon-ec2client-certificatesx509certificate2mmc

Client certificate time out in AWS EC2 instance


X509Certificate2 certificate = new X509Certificate2(@"Certificate/certificate.p12", "password", X509KeyStorageFlags.MachineKeySet);
    
   var handler = new HttpClientHandler();
   handler.ClientCertificateOptions = ClientCertificateOption.Manual;
   handler.SslProtocols = SslProtocols.Tls | SslProtocols.Tls12 | SslProtocols.Tls11;
   handler.ClientCertificates.Add(certificate);
   var client = new HttpClient(handler);
    
    
   var request = new HttpRequestMessage
       {
         Method = method,
         RequestUri = new Uri("endpoint" + url, UriKind.Absolute),
         Content = content
       };
   var result = await client.SendAsync(request);
   var responseStr = await result.Content.ReadAsStringAsync();

This method works in the local environment but when I publish this to the AWS ec2 env, I get this error A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

I have also added client certificate to the current user and local machine in ec2 using mmc.

Running OS in ec2 windows server 2019 Datacenter (Build 17763).


Solution

  • This issue is caused by ec2 instance server configuration. After i change security groups inbound and outbound rules to below settings it worked fine.

    Inbound rule

    • IPv4 All ICMP - IPv4 ICMP All 0.0.0.0/0
    • IPv4 All TCP TCP 0 - 65535 0.0.0.0/0
    • IPv4 RDP TCP 3389 0.0.0.0/0
    • IPv4 HTTPS TCP 443 0.0.0.0/0
    • IPv4 HTTP TCP 80 0.0.0.0/0

    Outbound rule

    • IPv4 All traffic All All 0.0.0.0/0
    • IPv4 All TCP TCP 0 - 65535 0.0.0.0/0
    • IPv4 All ICMP - IPv4 ICMP All