Generate Certificates
Create and configure SSL/TLS certificates for AuthScape production deployments.
SSL/TLS certificates are required for secure HTTPS connections in production. This guide covers generating and configuring certificates for AuthScape.
Certificate Types
| Type | Use Case | Cost |
|---|---|---|
| Let's Encrypt | Most deployments | Free |
| Azure Managed | Azure App Service | Free with App Service |
| Commercial CA | Enterprise requirements | Paid |
| Self-Signed | Development only | Free |
Let's Encrypt Certificates
Using Certbot
Install Certbot and generate a certificate:
bash
# Install Certbotsudo apt-get updatesudo apt-get install certbot# Generate certificatesudo certbot certonly --standalone -d api.yourapp.com# Certificates are saved to:# /etc/letsencrypt/live/api.yourapp.com/fullchain.pem# /etc/letsencrypt/live/api.yourapp.com/privkey.pem
Convert to PFX
ASP.NET Core uses PFX format:
bash
openssl pkcs12 -export \-out certificate.pfx \-inkey /etc/letsencrypt/live/api.yourapp.com/privkey.pem \-in /etc/letsencrypt/live/api.yourapp.com/fullchain.pem \-password pass:YourPassword
Auto-Renewal
Set up automatic renewal:
bash
# Test renewalsudo certbot renew --dry-run# Add to crontab for automatic renewal0 0 1 * * certbot renew --quiet
Azure App Service Certificates
Free Managed Certificate
Azure provides free SSL for custom domains:
- Go to App Service > TLS/SSL settings
- Click Private Key Certificates > Create App Service Managed Certificate
- Select your custom domain
- Azure automatically manages renewal
Binding Certificate
bash
# Using Azure CLIaz webapp config ssl bind \--name your-app-name \--resource-group your-resource-group \--certificate-thumbprint <THUMBPRINT> \--ssl-type SNI
Development Certificates
.NET Development Certificate
bash
# Trust the development certificatedotnet dev-certs https --trust# Export for use elsewheredotnet dev-certs https --export-path ./dev-cert.pfx --password YourPassword
Self-Signed Certificate
For development and testing only:
bash
# Generate self-signed certificateopenssl req -x509 -newkey rsa:4096 \-keyout key.pem \-out cert.pem \-days 365 \-nodes \-subj "/CN=localhost"# Convert to PFXopenssl pkcs12 -export \-out dev-certificate.pfx \-inkey key.pem \-in cert.pem \-password pass:development
Configuring ASP.NET Core
Kestrel Configuration
In appsettings.Production.json:
json
{"Kestrel": {"Endpoints": {"Https": {"Url": "https://*:443","Certificate": {"Path": "/app/certificates/certificate.pfx","Password": "YourCertificatePassword"}}}}}
Using Azure Key Vault
Store certificates securely in Key Vault:
csharp
builder.WebHost.ConfigureKestrel(serverOptions =>{serverOptions.ConfigureHttpsDefaults(httpsOptions =>{var certificate = LoadCertificateFromKeyVault();httpsOptions.ServerCertificate = certificate;});});
Certificate from Environment Variable
csharp
var certBytes = Convert.FromBase64String(Environment.GetEnvironmentVariable("SSL_CERT_BASE64"));var certificate = new X509Certificate2(certBytes,Environment.GetEnvironmentVariable("SSL_CERT_PASSWORD"));
Docker Configuration
Dockerfile
dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:8.0WORKDIR /appCOPY --from=build /app/publish .# Copy certificateCOPY certificates/certificate.pfx /app/certificates/# Set environment variablesENV ASPNETCORE_URLS="https://+:443;http://+:80"ENV ASPNETCORE_Kestrel__Certificates__Default__Path=/app/certificates/certificate.pfxENV ASPNETCORE_Kestrel__Certificates__Default__Password=YourPasswordENTRYPOINT ["dotnet", "YourApp.dll"]
Docker Compose
yaml
services:api:build: .ports:- "443:443"- "80:80"volumes:- ./certificates:/app/certificates:roenvironment:- ASPNETCORE_Kestrel__Certificates__Default__Path=/app/certificates/certificate.pfx- ASPNETCORE_Kestrel__Certificates__Default__Password=${CERT_PASSWORD}
Certificate Verification
Check Certificate Details
bash
# View certificate infoopenssl x509 -in cert.pem -text -noout# Check expirationopenssl x509 -in cert.pem -enddate -noout# Verify certificate chainopenssl verify -CAfile chain.pem cert.pem
Test HTTPS Connection
bash
# Test SSL handshakeopenssl s_client -connect api.yourapp.com:443 -servername api.yourapp.com# Check certificate with curlcurl -vI https://api.yourapp.com
Monitoring Certificate Expiry
Azure Alert
Set up alerts for certificate expiration in Azure Monitor.
Script Check
bash
#!/bin/bashDOMAIN="api.yourapp.com"EXPIRY=$(echo | openssl s_client -servername $DOMAIN -connect $DOMAIN:443 2>/dev/null | openssl x509 -noout -enddate | cut -d= -f2)EXPIRY_EPOCH=$(date -d "$EXPIRY" +%s)NOW_EPOCH=$(date +%s)DAYS_LEFT=$(( ($EXPIRY_EPOCH - $NOW_EPOCH) / 86400 ))if [ $DAYS_LEFT -lt 30 ]; thenecho "WARNING: Certificate expires in $DAYS_LEFT days"fi
Best Practices
- Never commit certificates to source control
- Use Key Vault for production certificate storage
- Automate renewal with Let's Encrypt or Azure managed certs
- Monitor expiry and set up alerts
- Test certificates before deploying to production