@@ -81,6 +81,33 @@ public static void SetTestCertBinding(string ipPort, bool enableClientCertNegoti
8181 Console . WriteLine ( "Configured binding for testCert for http.sys" ) ;
8282 }
8383
84+ public static bool TrySelfSignCertificate ( string ipPort , out string certThumbprint )
85+ {
86+ certThumbprint = string . Empty ;
87+ try
88+ {
89+ // Extract the IP address from ipPort
90+ string ipAddress = ipPort . Split ( ':' ) [ 0 ] ;
91+
92+ // Generate a self-signed certificate using PowerShell
93+ string command = $ "New-SelfSignedCertificate -CertStoreLocation cert:\\ LocalMachine\\ My -DnsName { ipAddress } ";
94+ string output = ExecutePowershellCommand ( command ) ;
95+
96+ // Extract the thumbprint from the output
97+ var lines = output . Split ( "\r \n " , StringSplitOptions . RemoveEmptyEntries ) ;
98+ var lastLine = lines [ ^ 1 ] ;
99+ certThumbprint = lastLine . Split ( " " , StringSplitOptions . RemoveEmptyEntries ) [ 0 ] ;
100+
101+ Console . WriteLine ( $ "Self-signed certificate for { ipAddress } ") ;
102+ return true ;
103+ }
104+ catch ( Exception ex )
105+ {
106+ Console . WriteLine ( "Failed to self-sign the certificate: " + ex . Message ) ;
107+ return false ;
108+ }
109+ }
110+
84111 public static void SetCertBinding ( string ipPort , string certThumbprint , string appId = null , bool enableClientCertNegotiation = false )
85112 {
86113 var negotiateClientCert = enableClientCertNegotiation ? "enable" : "disable" ;
@@ -90,8 +117,12 @@ public static void SetCertBinding(string ipPort, string certThumbprint, string a
90117 }
91118 string command = $ "http add sslcert ipport={ ipPort } certstorename=MY certhash={ certThumbprint } appid={{{appId}}} clientcertnegotiation={ negotiateClientCert } ";
92119 ExecuteNetShCommand ( command ) ;
120+ Console . WriteLine ( $ "Performed cert bindign for { ipPort } ") ;
93121 }
94122
123+ private static string ExecutePowershellCommand ( string command , bool alwaysLogOutput = false )
124+ => ExecuteCommand ( "powershell.exe" , command , alwaysLogOutput ) ;
125+
95126 private static string ExecuteNetShCommand ( string command , bool alwaysLogOutput = false )
96127 => ExecuteCommand ( "netsh" , command , alwaysLogOutput ) ;
97128
0 commit comments