|
Download HTTPS_IMPLEMENTATION_SUMMARY.md from SmartySmile/freespace-freemind: direct link, hf CLI and curl.
- Browser
- Download file 8.87 kB
-
https://huggingface.co/datasets/SmartySmile/freespace-freemind/resolve/main/HTTPS_IMPLEMENTATION_SUMMARY.md
- Command line
-
hf download hf://datasets/SmartySmile/freespace-freemind/HTTPS_IMPLEMENTATION_SUMMARY.md
-
curl -L -o HTTPS_IMPLEMENTATION_SUMMARY.md https://huggingface.co/datasets/SmartySmile/freespace-freemind/resolve/main/HTTPS_IMPLEMENTATION_SUMMARY.md
8.87 kB
PortWatcher HTTPS Security Enhancement - Implementation Summary
Date: March 3, 2026
Status: ✅ Complete
Problem Addressed
The application was generating a warning:
warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3]
Failed to determine the https port for redirect.
This occurred because the HTTPS port configuration was incomplete and the certificate handling was not properly initialized.
Solution Overview
Implemented enterprise-grade HTTPS certificate management with:
- ✅ Automatic port detection and configuration
- ✅ Self-signed certificate generation
- ✅ Certificate validation on startup
- ✅ Automatic certificate renewal (configurable)
- ✅ Certificate status monitoring via REST API
- ✅ Backup and restore capabilities
- ✅ Production certificate support
- ✅ Comprehensive logging
Changes Made
1. Program.cs - Enhanced HTTPS Configuration
- Added explicit Kestrel configuration with port binding
- Proper HSTS (HTTP Strict-Transport-Security) settings
- Certificate service injection
- Background certificate auto-renewal initialization
Key Changes:
builder.WebHost.ConfigureKestrel(serverOptions =>
{
// Explicit port configuration
serverOptions.Listen(System.Net.IPAddress.Any, httpPort);
serverOptions.Listen(System.Net.IPAddress.Any, httpsPort,
listenOptions => listenOptions.UseHttps());
});
2. appsettings.json - Certificate Configuration
Added comprehensive HTTPS settings:
{
"HttpSettings": {
"HttpPort": 5293,
"HttpsPort": 7145
},
"HttpsCertificate": {
"Enabled": true,
"CertificatePath": "Data/certificates",
"CertificateFileName": "port-watcher-cert",
"EnableAutoRenewal": true
}
}
3. New Service: IHttpsCertificateService
Interface for certificate management:
InitializeCertificateAsync()- Initialize on startupValidateAndRenewCertificateAsync()- Check and renew if neededGetCertificateInfoAsync()- Get certificate detailsRenewCertificateAsync()- Manual renewal
4. New Service: HttpsCertificateService
Implementation with:
- Self-signed certificate generation using RSA-2048
- Certificate validation with threshold checking
- Automatic renewal with backup of old certs
- Background task for periodic validation (every 24 hours)
- Comprehensive logging
5. New Controller: CertificateDiagnosticsController
REST API endpoints:
GET /api/certificatediagnostics/status- Get current certificate infoPOST /api/certificatediagnostics/validate- Validate and auto-renewPOST /api/certificatediagnostics/renew- Manual renewal
6. Configuration Files
- appsettings.Development.json - Development settings (auto-renewal disabled)
- appsettings.Production.json - Production template (uses standard ports 80/443)
- Properties/launchSettings.json - Added Production profile
7. Management Tools
- ManageCertificates.bat - Interactive batch script for Windows
- ManageCertificates.ps1 - Advanced PowerShell management script
8. Documentation
- HTTPS_CERTIFICATE_GUIDE.md - Complete configuration and troubleshooting guide
How the HTTPS Port Detection Now Works
Startup Process:
- Application starts with explicit Kestrel configuration
- Reads port settings from
appsettings.json(HttpSettings section) - Binds to specified HTTP and HTTPS ports
- Certificate service initializes (creates cert if missing)
- Background auto-renewal task starts (if enabled)
Result:
- ✅ HTTPS port is always known and properly configured
- ✅ No "Failed to determine the https port" warning
- ✅ Clean HTTPS redirect from HTTP to HTTPS
Configuration Guide
Development Environment
# Certificates auto-created in: Data/certificates/
# Auto-renewal: Disabled
# Subject: CN=localhost
# Validity: 365 days
dotnet run
Production Environment
# Option 1: Self-signed with auto-renewal
{
"HttpsCertificate": {
"UseProductionCert": false,
"EnableAutoRenewal": true
}
}
# Option 2: Use production certificate (Let's Encrypt, etc.)
{
"HttpsCertificate": {
"UseProductionCert": true,
"ProductionCertPath": "C:\\path\\to\\cert.pfx"
}
}
Certificate Management
Check Certificate Status
# PowerShell
.\ManageCertificates.ps1 -Status
# Batch
ManageCertificates.bat # Then select option 1
# API
curl -k https://localhost:7145/api/certificatediagnostics/status
Validate & Auto-Renew
# PowerShell
.\ManageCertificates.ps1 -Validate
# API
curl -k -X POST https://localhost:7145/api/certificatediagnostics/validate
Manual Renewal
# PowerShell
.\ManageCertificates.ps1 -Renew -Force
# API
curl -k -X POST https://localhost:7145/api/certificatediagnostics/renew
Backup/Restore
# List backups
.\ManageCertificates.ps1 -ListBackups
# Backup
.\ManageCertificates.ps1 -Backup
# Restore
.\ManageCertificates.ps1 -Restore
File Structure
PortWatcher/
├── Program.cs (Updated)
├── appsettings.json (Updated)
├── appsettings.Development.json (Updated)
├── appsettings.Production.json (New)
├── Properties/launchSettings.json (Updated)
├── Services/
│ ├── IHttpsCertificateService.cs (New)
│ ├── HttpsCertificateService.cs (New)
│ ├── [Other existing services...]
├── Controllers/
│ ├── CertificateDiagnosticsController.cs (New)
│ ├── [Other existing controllers...]
├── Data/
│ ├── certificates/ (Auto-created on first run)
│ │ └── port-watcher-cert.pfx (Auto-generated)
│ └── [Other existing data files...]
├── HTTPS_CERTIFICATE_GUIDE.md (New)
├── ManageCertificates.bat (New)
└── ManageCertificates.ps1 (New)
Security Features
Certificate Security
- RSA-2048 encryption
- Self-signed with proper extensions
- Subject Alternative Names (SANs) included
- Private key stored in PFX format
Auto-Renewal
- Checks 24 hours before expiration (configurable)
- Creates backups before renewal
- Logs all operations
- Fails safely
HSTS Policy
- Development: 30 days
- Production: 365 days (1 year)
- Includes subdomains
- Preload enabled for production
Troubleshooting
Still Getting HTTPS Port Warning?
- Clear build artifacts:
dotnet clean - Rebuild:
dotnet build - Run:
dotnet run --project PortWatcher.csproj - Check logs for any certificate initialization errors
Certificate Not Found?
- Check
Data/certificates/directory - Verify file permissions
- Check application logs
- Run
.\ManageCertificates.ps1 -Status
Cannot Connect to HTTPS?
- Verify ports 7145 (dev) or 443 (prod) are available
- Check Windows Firewall rules
- Test locally first:
https://localhost:7145
Auto-Renewal Not Working?
- Verify
EnableAutoRenewal: truein config - Check application logs
- Verify certificate expiration date
- Manually trigger:
.\ManageCertificates.ps1 -Renew
Integration with eSecSys/nCONNECT OS
For enterprise certificate management systems:
- Set
UseProductionCert: true - Point
ProductionCertPathto provisioned cert - Set
EnableAutoRenewal: false(let system manage) - Use API endpoints for monitoring:
- Monitor renewal status
- Validate certificates
- Trigger renewal when needed
Next Steps (Optional)
- Let's Encrypt Integration: Replace self-signed certs with automatic Let's Encrypt renewal
- Certificate Pinning: Pin specific certificates for added security
- Certificate Monitoring: Send alerts when renewal is triggered
- Audit Logging: Forward certificate events to audit system
- Multi-Domain Support: Support certificates for multiple domains
Testing Checklist
- Application starts without HTTPS port warning
- Certificate is auto-created on first run
- Certificate status API returns valid data
- Manual renewal works via API or scripts
- Auto-renewal background task runs
- Certificate backups are created
- HTTPS redirects HTTP traffic
- HSTS headers are present
Support
For issues or questions about HTTPS certificate configuration, refer to:
HTTPS_CERTIFICATE_GUIDE.md- Detailed configuration guide- Application logs in
Data/logs/directory - API diagnostics:
/api/certificatediagnostics/status
Implementation Complete ✅