1
- using Microsoft . AspNetCore . Mvc ;
1
+ using System . Diagnostics ;
2
+ using System . Security . Cryptography . X509Certificates ;
2
3
using Microsoft . AspNetCore . Server . HttpSys ;
3
4
4
5
var builder = WebApplication . CreateBuilder ( args ) ;
7
8
var writeCertValidationEventsToConsole = bool . TryParse ( builder . Configuration [ "certValidationConsoleEnabled" ] , out var certValidationConsoleEnabled ) && certValidationConsoleEnabled ;
8
9
var statsEnabled = bool . TryParse ( builder . Configuration [ "statsEnabled" ] , out var connectionStatsEnabledConfig ) && connectionStatsEnabledConfig ;
9
10
var mTlsEnabled = bool . TryParse ( builder . Configuration [ "mTLS" ] , out var mTlsEnabledConfig ) && mTlsEnabledConfig ;
11
+ var tlsRenegotiationEnabled = bool . TryParse ( builder . Configuration [ "tlsRenegotiation" ] , out var tlsRenegotiationEnabledConfig ) && tlsRenegotiationEnabledConfig ;
10
12
var listeningEndpoints = builder . Configuration [ "urls" ] ?? "https://localhost:5000/" ;
11
13
12
14
#pragma warning disable CA1416 // Can be launched only on Windows (HttpSys)
40
42
}
41
43
42
44
if ( mTlsEnabled )
45
+ {
46
+ ConfigureHttpSysForMutualTls ( ) ;
47
+ }
48
+
49
+ if ( tlsRenegotiationEnabled )
43
50
{
44
51
// this is an http.sys middleware to get a cert
45
52
Console . WriteLine ( "Registered client cert validation middleware" ) ;
89
96
Console . WriteLine ( "--------------------------------" ) ;
90
97
91
98
Console . WriteLine ( "Application started." ) ;
92
- await app . WaitForShutdownAsync ( ) ;
99
+ await app . WaitForShutdownAsync ( ) ;
100
+
101
+ void ConfigureHttpSysForMutualTls ( )
102
+ {
103
+ var certificate = new X509Certificate2 ( "testCert.pfx" , "testPassword" , X509KeyStorageFlags . MachineKeySet | X509KeyStorageFlags . Exportable ) ;
104
+ using ( var store = new X509Store ( StoreName . My , StoreLocation . LocalMachine ) )
105
+ {
106
+ store . Open ( OpenFlags . ReadWrite ) ;
107
+ store . Add ( certificate ) ;
108
+ store . Close ( ) ;
109
+ }
110
+
111
+ string certThumbprint = certificate . Thumbprint ;
112
+ string appId = Guid . NewGuid ( ) . ToString ( ) ;
113
+
114
+ string command = $ "http add sslcert ipport=0.0.0.0:5000 certhash={ certThumbprint } appid={{{appId}}} clientcertnegotiation=enable";
115
+ ProcessStartInfo processInfo = new ProcessStartInfo ( "netsh" , command )
116
+ {
117
+ RedirectStandardOutput = true ,
118
+ RedirectStandardError = true ,
119
+ UseShellExecute = false ,
120
+ CreateNoWindow = true
121
+ } ;
122
+
123
+ using Process process = Process . Start ( processInfo ) ! ;
124
+ string output = process . StandardOutput . ReadToEnd ( ) ;
125
+ string error = process . StandardError . ReadToEnd ( ) ;
126
+ process . WaitForExit ( ) ;
127
+
128
+ if ( process . ExitCode != 0 )
129
+ {
130
+ throw new InvalidOperationException ( $ "Failed to configure http.sys: { error } ") ;
131
+ }
132
+
133
+ Console . WriteLine ( "Configured http.sys settings for mTLS" ) ;
134
+ }
0 commit comments