|
2 | 2 | using System.Net.Security;
|
3 | 3 | using System.Security.Cryptography.X509Certificates;
|
4 | 4 | using Microsoft.AspNetCore.Authentication.Certificate;
|
| 5 | +using Microsoft.AspNetCore.Connections.Features; |
| 6 | +using Microsoft.AspNetCore.Http.Features; |
5 | 7 | using Microsoft.AspNetCore.Server.HttpSys;
|
6 | 8 | using Microsoft.AspNetCore.Server.Kestrel.Core;
|
7 | 9 | using Microsoft.AspNetCore.Server.Kestrel.Https;
|
8 | 10 |
|
9 | 11 | var builder = WebApplication.CreateBuilder(args);
|
10 | 12 | builder.Logging.ClearProviders();
|
11 | 13 |
|
12 |
| -var writeCertValidationEventsToConsole = bool.TryParse(builder.Configuration["certValidationConsoleEnabled"], out var certValidationConsoleEnabled) && certValidationConsoleEnabled; |
| 14 | +// behavioral |
13 | 15 | var mTlsEnabled = bool.TryParse(builder.Configuration["mTLS"], out var mTlsEnabledConfig) && mTlsEnabledConfig;
|
14 | 16 | var tlsRenegotiationEnabled = bool.TryParse(builder.Configuration["tlsRenegotiation"], out var tlsRenegotiationEnabledConfig) && tlsRenegotiationEnabledConfig;
|
15 |
| -var statsEnabled = bool.TryParse(builder.Configuration["statsEnabled"], out var connectionStatsEnabledConfig) && connectionStatsEnabledConfig; |
16 | 17 | var listeningEndpoints = builder.Configuration["urls"] ?? "https://localhost:5000/";
|
17 | 18 |
|
| 19 | +// debug |
| 20 | +var writeCertValidationEventsToConsole = bool.TryParse(builder.Configuration["certValidationConsoleEnabled"], out var certValidationConsoleEnabled) && certValidationConsoleEnabled; |
| 21 | +var statsEnabled = bool.TryParse(builder.Configuration["statsEnabled"], out var connectionStatsEnabledConfig) && connectionStatsEnabledConfig; |
| 22 | +var logRequestDetails = bool.TryParse(builder.Configuration["logRequestDetails"], out var logRequestDetailsConfig) && logRequestDetailsConfig; |
| 23 | + |
18 | 24 | if (mTlsEnabled && tlsRenegotiationEnabled)
|
19 | 25 | {
|
20 | 26 | Console.WriteLine("mTLS and tlsRenegotiation require different clientCertMode setup. Using TLS Renegotiation by default.");
|
@@ -81,6 +87,27 @@ bool AllowAnyCertificateValidationWithLogging(X509Certificate2 certificate, X509
|
81 | 87 | return true;
|
82 | 88 | }
|
83 | 89 |
|
| 90 | +if (logRequestDetails) |
| 91 | +{ |
| 92 | + var logged = false; |
| 93 | + Console.WriteLine("Registered request details logging middleware"); |
| 94 | + app.Use(async (context, next) => |
| 95 | + { |
| 96 | + if (!logged) |
| 97 | + { |
| 98 | + logged = true; |
| 99 | + |
| 100 | + var tlsHandshakeFeature = context.Features.GetRequiredFeature<ITlsHandshakeFeature>(); |
| 101 | + |
| 102 | + Console.WriteLine("Request details:"); |
| 103 | + Console.WriteLine("-----"); |
| 104 | + Console.WriteLine("TLS: " + tlsHandshakeFeature.Protocol); |
| 105 | + Console.WriteLine("-----"); |
| 106 | + } |
| 107 | + await next(); |
| 108 | + }); |
| 109 | +} |
| 110 | + |
84 | 111 | if (statsEnabled)
|
85 | 112 | {
|
86 | 113 | Console.WriteLine("Registered stats middleware");
|
|
0 commit comments