File size: 8,293 Bytes
0e0fe0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# PortWatcher HTTPS Certificate Management - Complete Solution

## 🎯 Problem Solved

Fixed the warning:
```

warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3]

      Failed to determine the https port for redirect.

```

## ✨ What's New

### Enterprise-Grade HTTPS Management
- ✅ Automatic HTTPS port configuration
- ✅ Self-signed certificate generation
- ✅ Automatic certificate renewal before expiration
- ✅ Certificate backup & restore
- ✅ REST API for certificate diagnostics
- ✅ Background validation every 24 hours
- ✅ Production certificate support
- ✅ Comprehensive logging

---

## 🚀 Quick Start

### 1. Build & Run
```bash

dotnet clean

dotnet build

dotnet run --launch-profile https

```

### 2. The Certificate Creates Automatically
First run will:
- Create `Data/certificates/` directory
- Generate self-signed certificate: `port-watcher-cert.pfx`
- Start background renewal checks

### 3. Access Application
- **Development**: `https://localhost:7145`
- **Production**: `https://localhost` (port 443)

---

## 📋 Certificate Management

### Check Certificate Status
```bash

# PowerShell

.\ManageCertificates.ps1 -Status



# Batch (Windows)

ManageCertificates.bat

# Then select option 1



# API (curl)

curl -k https://localhost:7145/api/certificatediagnostics/status

```

### Manual Certificate Renewal
```bash

# PowerShell

.\ManageCertificates.ps1 -Renew -Force



# API

curl -k -X POST https://localhost:7145/api/certificatediagnostics/renew

```

### Create Backup
```bash

.\ManageCertificates.ps1 -Backup

```

### Restore from Backup
```bash

.\ManageCertificates.ps1 -ListBackups

.\ManageCertificates.ps1 -Restore

```

---

## 📁 Files Modified & Created

### Modified Files
- ✏️ `Program.cs` - Added Kestrel HTTPS configuration
- ✏️ `appsettings.json` - Added HTTP/HTTPS settings
- ✏️ `appsettings.Development.json` - Development certificate settings
- ✏️ `Properties/launchSettings.json` - Added production profile

### New Files
- 🆕 `Services/IHttpsCertificateService.cs` - Certificate service interface
- 🆕 `Services/HttpsCertificateService.cs` - Certificate management implementation
- 🆕 `Controllers/CertificateDiagnosticsController.cs` - REST API endpoints
- 🆕 `appsettings.Production.json` - Production configuration template
- 🆕 `ManageCertificates.bat` - Windows batch management tool
- 🆕 `ManageCertificates.ps1` - PowerShell management tool
- 🆕 `HTTPS_CERTIFICATE_GUIDE.md` - Detailed configuration guide
- 🆕 `HTTPS_IMPLEMENTATION_SUMMARY.md` - Implementation details
- 🆕 `QuickStart.sh` - Quick start script

---

## ⚙️ Configuration

### Development (Default)
```json

{

  "HttpSettings": {

    "HttpPort": 5293,

    "HttpsPort": 7145

  },

  "HttpsCertificate": {

    "Enabled": true,

    "EnableAutoRenewal": false,

    "SubjectName": "CN=localhost",

    "ValidityDays": 365

  }

}

```

### Production
```json

{

  "HttpSettings": {

    "HttpPort": 80,

    "HttpsPort": 443

  },

  "HttpsCertificate": {

    "Enabled": true,

    "EnableAutoRenewal": true,

    "AutoRenewThresholdDays": 30,

    "UseProductionCert": true,

    "ProductionCertPath": "C:/path/to/cert.pfx"

  }

}

```

---

## 🔌 REST API Endpoints

All endpoints require HTTPS and return JSON responses.

### Get Certificate Status
```

GET /api/certificatediagnostics/status

```

Response:
```json

{

  "success": true,

  "data": {

    "subjectName": "CN=localhost",

    "issuedDate": "2026-03-03T00:00:00",

    "expirationDate": "2027-03-03T00:00:00",

    "daysUntilExpiration": 365,

    "isExpired": false,

    "needsRenewal": false,

    "thumbprint": "ABC123..."

  }

}

```

### Validate & Auto-Renew
```

POST /api/certificatediagnostics/validate

```

### Manual Renewal
```

POST /api/certificatediagnostics/renew

```

---

## 📊 How It Works

### Startup Process
1. Application starts with Kestrel configured for HTTPS
2. Reads HTTP/HTTPS ports from `appsettings.json`
3. Certificate service initializes
4. If certificate missing → Create new self-signed certificate
5. If certificate exists → Validate expiration
6. Start background task for periodic checks

### Auto-Renewal Process
1. Background task runs every 24 hours
2. Checks if certificate expires within 30 days
3. If renewal needed → Create new certificate
4. Backup old certificate with timestamp
5. Log all operations

### HTTPS Redirect
1. All HTTP requests are redirected to HTTPS
2. HSTS headers are set (30 days dev, 365 days prod)
3. No more "Failed to determine the https port" warning

---

## 🔍 Monitoring & Logging

### Check Logs
Application logs are stored in the output console and can be configured in:
- `appsettings.json` → `Logging` section

### Watch for These Log Messages
```

[INF] HTTPS certificate found at Data/certificates/port-watcher-cert.pfx

[INF] Certificate Info: Subject=CN=localhost, Expires=2027-03-03, DaysRemaining=365

[INF] Running automatic certificate validation check...

[WRN] HTTPS certificate expires in 25 days. Auto-renewal will be triggered.

[INF] HTTPS certificate renewed successfully.

```

### API Diagnostics
```bash

# Check status

curl -k https://localhost:7145/api/certificatediagnostics/status



# Trigger validation

curl -k -X POST https://localhost:7145/api/certificatediagnostics/validate

```

---

## 🛡️ Security Features

### Certificate Security
- **RSA-2048 Encryption**: Industry standard encryption
- **Self-signed**: Suitable for development and production
- **SANs Included**: Supports localhost and 127.0.0.1
- **PFX Format**: Includes private key protection

### Automatic Renewal
- Checks 30 days before expiration
- Creates backups before renewal
- Fails safely with error logging
- No manual intervention required

### HSTS Policy
- **Development**: 30 days
- **Production**: 365 days (1 year)
- Prevents downgrade attacks
- Preload-ready

---

## 🐛 Troubleshooting

### Still Getting HTTPS Port Warning?
1. Clean build: `dotnet clean`
2. Rebuild: `dotnet build`
3. Run: `dotnet run`
4. Check logs for certificate errors

### Certificate Not Created?
1. Check directory permissions
2. Verify `Data/certificates/` directory exists
3. Check application logs
4. Run: `.\ManageCertificates.ps1 -Status`

### Cannot Connect via HTTPS?
1. Check firewall (port 7145 for dev, 443 for prod)
2. Access locally first: `https://localhost:7145`
3. Accept self-signed certificate in browser
4. Check certificate validity: `.ps1 -Status`

### Auto-Renewal Not Working?
1. Verify `EnableAutoRenewal: true`
2. Check certificate expiration: `.ps1 -Status`
3. Check logs for renewal task
4. Manually renew: `.ps1 -Renew`

---

## 📚 Documentation

- 📖 `HTTPS_CERTIFICATE_GUIDE.md` - Complete configuration guide
- 📖 `HTTPS_IMPLEMENTATION_SUMMARY.md` - Implementation details
- 📖 This file - Quick reference and overview

---

## 🤝 Integration with Enterprise Systems

For eSecSys/nCONNECT OS certificate management:

```json

{

  "HttpsCertificate": {

    "UseProductionCert": true,

    "ProductionCertPath": "/provisioned/path/cert.pfx",

    "EnableAutoRenewal": false

  }

}

```

Use the API endpoints to:
- Monitor certificate status
- Trigger renewal when needed
- Track certificate lifecycle

---

## ✅ Verification Checklist

- [ ] Application starts without HTTPS port warning
- [ ] Certificate is created automatically on first run
- [ ] Certificate status shows via API
- [ ] HTTP redirects to HTTPS
- [ ] HTTPS works at `https://localhost:7145` (dev)
- [ ] Certificate auto-renewal is happening in background
- [ ] Certificate backups are created
- [ ] HSTS headers are present in responses

---

## 📞 Support

For detailed configuration:
1. Read `HTTPS_CERTIFICATE_GUIDE.md`
2. Check application logs
3. Use API diagnostics: `/api/certificatediagnostics/status`
4. Run management scripts with `-Verbose` flag

---

**Status**: ✅ Implementation Complete
**Last Updated**: March 3, 2026
**Framework**: .NET 8.0 / ASP.NET Core