Skip to content

Commit d20a254

Browse files
Improve certificate loading with better diagnostics and error handling
Co-authored-by: StuartFerguson <16325469+StuartFerguson@users.noreply.github.com>
1 parent aa0cf9f commit d20a254

2 files changed

Lines changed: 32 additions & 29 deletions

File tree

EstateManagementUI.BlazorServer/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
66
ARG BUILD_CONFIGURATION=Release
77
WORKDIR /src
88

9-
# Install Node.js for Tailwind CSS build
10-
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \
9+
# Install Node.js for Tailwind CSS build - use insecure curl for corporate proxy environments
10+
RUN curl -fsSL --insecure https://deb.nodesource.com/setup_20.x | bash - && \
1111
apt-get install -y nodejs
1212

1313
COPY ["EstateManagementUI.BlazorServer/NuGet.Config", "."]

EstateManagementUI.BlazorServer/Program.cs

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,40 @@
2222

2323
options.Listen(IPAddress.Any, port, listenOptions =>
2424
{
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+
2548
try
2649
{
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);
5153
}
52-
catch (Exception e)
54+
catch (Exception ex)
5355
{
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);
5659
}
5760
});
5861
});

0 commit comments

Comments
 (0)