|
22 | 22 |
|
23 | 23 | options.Listen(IPAddress.Any, port, listenOptions => |
24 | 24 | { |
| 25 | + // Enable support for HTTP1 and HTTP2 |
| 26 | + listenOptions.Protocols = HttpProtocols.Http1AndHttp2; |
| 27 | + |
| 28 | + // Configure Kestrel to use a certificate from a local .PFX file for hosting HTTPS |
| 29 | + var certificatePath = Path.Combine(AppContext.BaseDirectory, "Certificates"); |
| 30 | + Console.WriteLine($"Looking for certificates in: {certificatePath}"); |
| 31 | + Console.WriteLine($"AppContext.BaseDirectory: {AppContext.BaseDirectory}"); |
| 32 | + Console.WriteLine($"Current Directory: {Directory.GetCurrentDirectory()}"); |
| 33 | + |
| 34 | + if (!Directory.Exists(certificatePath)) |
| 35 | + { |
| 36 | + throw new InvalidOperationException($"Certificates folder not found at: {certificatePath}"); |
| 37 | + } |
| 38 | + |
| 39 | + var certificateFiles = Directory.GetFiles(certificatePath, "*.pfx"); |
| 40 | + if (certificateFiles.Length == 0) |
| 41 | + { |
| 42 | + throw new InvalidOperationException($"No .pfx certificate file found in {certificatePath}"); |
| 43 | + } |
| 44 | + |
| 45 | + var certificateFile = certificateFiles.First(); |
| 46 | + Console.WriteLine($"Loading certificate from: {certificateFile}"); |
| 47 | + |
25 | 48 | try |
26 | 49 | { |
27 | | - // Enable support for HTTP1 and HTTP2 |
28 | | - listenOptions.Protocols = HttpProtocols.Http1AndHttp2; |
29 | | - |
30 | | - // Configure Kestrel to use a certificate from a local .PFX file for hosting HTTPS |
31 | | - var certificatePath = Path.Combine(AppContext.BaseDirectory, "Certificates"); |
32 | | - if (Directory.Exists(certificatePath)) |
33 | | - { |
34 | | - var certificateFiles = Directory.GetFiles(certificatePath, "*.pfx"); |
35 | | - if (certificateFiles.Length > 0) |
36 | | - { |
37 | | - var certificateFile = certificateFiles.First(); |
38 | | - Console.WriteLine($"Certificate File: {certificateFile}"); |
39 | | - var certificate = new X509Certificate2(certificateFile, "password"); |
40 | | - listenOptions.UseHttps(certificate); |
41 | | - } |
42 | | - else |
43 | | - { |
44 | | - Console.WriteLine("No certificate file found in Certificates folder"); |
45 | | - } |
46 | | - } |
47 | | - else |
48 | | - { |
49 | | - Console.WriteLine($"Certificates folder not found at: {certificatePath}"); |
50 | | - } |
| 50 | + var certificate = new X509Certificate2(certificateFile, "password"); |
| 51 | + Console.WriteLine($"Certificate loaded successfully. Subject: {certificate.Subject}"); |
| 52 | + listenOptions.UseHttps(certificate); |
51 | 53 | } |
52 | | - catch (Exception e) |
| 54 | + catch (Exception ex) |
53 | 55 | { |
54 | | - Console.WriteLine($"Error configuring HTTPS: {e.Message}"); |
55 | | - throw; |
| 56 | + Console.WriteLine($"Error loading certificate: {ex.Message}"); |
| 57 | + Console.WriteLine($"Stack trace: {ex.StackTrace}"); |
| 58 | + throw new InvalidOperationException($"Failed to load certificate from {certificateFile}: {ex.Message}", ex); |
56 | 59 | } |
57 | 60 | }); |
58 | 61 | }); |
|
0 commit comments