@@ -81,6 +81,33 @@ public static void SetTestCertBinding(string ipPort, bool enableClientCertNegoti
81
81
Console . WriteLine ( "Configured binding for testCert for http.sys" ) ;
82
82
}
83
83
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
+
84
111
public static void SetCertBinding ( string ipPort , string certThumbprint , string appId = null , bool enableClientCertNegotiation = false )
85
112
{
86
113
var negotiateClientCert = enableClientCertNegotiation ? "enable" : "disable" ;
@@ -90,8 +117,12 @@ public static void SetCertBinding(string ipPort, string certThumbprint, string a
90
117
}
91
118
string command = $ "http add sslcert ipport={ ipPort } certstorename=MY certhash={ certThumbprint } appid={{{appId}}} clientcertnegotiation={ negotiateClientCert } ";
92
119
ExecuteNetShCommand ( command ) ;
120
+ Console . WriteLine ( $ "Performed cert bindign for { ipPort } ") ;
93
121
}
94
122
123
+ private static string ExecutePowershellCommand ( string command , bool alwaysLogOutput = false )
124
+ => ExecuteCommand ( "powershell.exe" , command , alwaysLogOutput ) ;
125
+
95
126
private static string ExecuteNetShCommand ( string command , bool alwaysLogOutput = false )
96
127
=> ExecuteCommand ( "netsh" , command , alwaysLogOutput ) ;
97
128
0 commit comments