1
- using System . Diagnostics ;
2
- using System . Security . Cryptography . X509Certificates ;
1
+ using HttpSys ;
3
2
using Microsoft . AspNetCore . Server . HttpSys ;
4
3
5
4
var builder = WebApplication . CreateBuilder ( args ) ;
6
5
builder . Logging . ClearProviders ( ) ;
7
6
7
+ Console . WriteLine ( $ "args: { string . Join ( " " , args ) } ") ;
8
+ Console . WriteLine ( ) ;
9
+
8
10
var writeCertValidationEventsToConsole = bool . TryParse ( builder . Configuration [ "certValidationConsoleEnabled" ] , out var certValidationConsoleEnabled ) && certValidationConsoleEnabled ;
9
11
var statsEnabled = bool . TryParse ( builder . Configuration [ "statsEnabled" ] , out var connectionStatsEnabledConfig ) && connectionStatsEnabledConfig ;
10
12
var mTlsEnabled = bool . TryParse ( builder . Configuration [ "mTLS" ] , out var mTlsEnabledConfig ) && mTlsEnabledConfig ;
11
13
var tlsRenegotiationEnabled = bool . TryParse ( builder . Configuration [ "tlsRenegotiation" ] , out var tlsRenegotiationEnabledConfig ) && tlsRenegotiationEnabledConfig ;
12
14
var listeningEndpoints = builder . Configuration [ "urls" ] ?? "https://localhost:5000/" ;
15
+ var httpsIpPort = listeningEndpoints . Split ( ";" ) . First ( x => x . Contains ( "https" ) ) . Replace ( "https://" , "" ) ;
13
16
14
17
#pragma warning disable CA1416 // Can be launched only on Windows (HttpSys)
15
18
builder . WebHost . UseHttpSys ( options =>
19
22
} ) ;
20
23
#pragma warning restore CA1416 // Can be launched only on Windows (HttpSys)
21
24
22
- var app = builder . Build ( ) ;
25
+ var app = builder . Build ( ) ;
23
26
24
27
app . MapGet ( "/hello-world" , ( ) =>
25
28
{
43
46
44
47
if ( mTlsEnabled )
45
48
{
49
+ var hostAppLifetime = app . Services . GetService < IHostApplicationLifetime > ( ) ;
50
+ hostAppLifetime ! . ApplicationStopping . Register ( OnShutdown ) ;
51
+
52
+ void OnShutdown ( )
53
+ {
54
+ try
55
+ {
56
+ NetShWrapper . DisableHttpSysMutualTls ( ipPort : httpsIpPort ) ;
57
+ }
58
+ catch
59
+ {
60
+ Console . WriteLine ( "Failed to disable HTTP.SYS mTLS settings" ) ;
61
+ throw ;
62
+ }
63
+ }
64
+
46
65
try
47
66
{
48
- ConfigureHttpSysForMutualTls ( ) ;
67
+ NetShWrapper . EnableHttpSysMutualTls ( ipPort : httpsIpPort ) ;
49
68
}
50
- catch ( Exception ex )
69
+ catch
51
70
{
52
- throw new Exception ( $ "Http.Sys configuration for mTLS failed. Current dir: { Directory . GetCurrentDirectory ( ) } ", innerException : ex ) ;
71
+ Console . WriteLine ( $ "Http.Sys configuration for mTLS failed") ;
72
+ throw ;
53
73
}
54
74
}
55
75
105
125
Console . WriteLine ( "Application started." ) ;
106
126
await app . WaitForShutdownAsync ( ) ;
107
127
108
- void ConfigureHttpSysForMutualTls ( )
109
- {
110
- Console . WriteLine ( "Setting up mTLS for http.sys" ) ;
111
-
112
- var certificate = new X509Certificate2 ( "../testCert.pfx" , "testPassword" , X509KeyStorageFlags . MachineKeySet | X509KeyStorageFlags . Exportable ) ;
113
- using ( var store = new X509Store ( StoreName . My , StoreLocation . LocalMachine ) )
114
- {
115
- store . Open ( OpenFlags . ReadWrite ) ;
116
- store . Add ( certificate ) ;
117
- store . Close ( ) ;
118
- }
119
-
120
- string certThumbprint = certificate . Thumbprint ;
121
- string appId = Guid . NewGuid ( ) . ToString ( ) ;
122
-
123
- string command = $ "http add sslcert ipport=0.0.0.0:5000 certhash={ certThumbprint } appid={{{appId}}} clientcertnegotiation=enable";
124
- ProcessStartInfo processInfo = new ProcessStartInfo ( "netsh" , command )
125
- {
126
- RedirectStandardOutput = true ,
127
- RedirectStandardError = true ,
128
- UseShellExecute = false ,
129
- CreateNoWindow = true
130
- } ;
131
-
132
- using Process process = Process . Start ( processInfo ) ! ;
133
- string output = process . StandardOutput . ReadToEnd ( ) ;
134
- string error = process . StandardError . ReadToEnd ( ) ;
135
- process . WaitForExit ( ) ;
136
-
137
- if ( process . ExitCode != 0 )
138
- {
139
- throw new InvalidOperationException ( $ "Failed to configure http.sys: { error } ") ;
140
- }
141
-
142
- Console . WriteLine ( "Configured http.sys settings for mTLS" ) ;
143
- }
0 commit comments