Azure Password Change Fix - Deployment Guide

Problem Identified

The password change functionality works locally but fails in Azure deployment due to database file path and connectivity issues.

Root Cause

  1. Database Path Issues: SQLite database file path resolution differs in Azure App Service environment
  2. File Permissions: Azure App Service has different file system permissions than local development
  3. Database Locking: Concurrent access patterns can cause database locking issues in Azure

Solution Implemented

1. Enhanced Database Path Resolution

  • For Production (Azure): Uses temporary directory (Path.GetTempPath()) for better reliability
  • For Development: Uses content root path as before
  • Added Logging: Database path is now logged for debugging

2. Improved Error Handling

  • Enhanced PIN Change Method: Added comprehensive error handling and logging
  • Database Transaction Verification: Verifies that PIN changes are actually saved
  • Exception Logging: Detailed error messages for debugging Azure-specific issues

3. Health Check Endpoint

  • New Endpoint: /health/db for monitoring database connectivity
  • Diagnostics: Provides database status, user count, admin status, and connection details
  • Environment Detection: Shows current environment and database path

Deployment Steps

Step 1: Deploy Updated Code

Deploy the updated application with the following changes:

  • Enhanced database path resolution in Program.cs
  • Improved error handling in AuthService.cs
  • New health check endpoint
  • Production configuration file

Step 2: Verify Database Connectivity

After deployment, test the database health check:

GET https://your-app.azurewebsites.net/health/db

Expected response:

{
  "databaseConnected": true,
  "userCount": 1,
  "hasAdmin": true,
  "adminName": "Admin",
  "hasSettings": true,
  "databasePath": "Data Source=/tmp/auth.db",
  "environment": "Production"
}

Step 3: Test Password Change

  1. Log in as Admin or any user
  2. Navigate to password change section
  3. Change PIN using the form
  4. Verify the change works and shows “PIN changed” message

Step 4: Monitor Logs

Check Azure App Service logs for:

  • Database path logging: “Database path: /tmp/auth.db (Environment: Production)”
  • PIN change debug messages
  • Any database connectivity errors

Configuration Changes

appsettings.Production.json

Created production-specific configuration with enhanced logging:

{
  "ConnectionStrings": {
    "AuthDb": "Data Source=auth.db"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning",
      "Microsoft.EntityFrameworkCore.Database.Command": "Information"
    }
  },
  "AllowedHosts": "*"
}

Troubleshooting Guide

If Password Change Still Fails

  1. Check Database Health
    • Visit /health/db endpoint
    • Verify databaseConnected: true
    • Check for error messages
  2. Check Application Logs
    • Look for “PIN change debug” messages
    • Check for database save errors
    • Verify database path is correct
  3. Verify Database File
    • Use Kudu console to check if auth.db exists in temp directory
    • Check file permissions on database file
  4. Restart App Service
    • Stop and restart the Azure App Service
    • This will recreate database if needed

Common Issues and Solutions

Issue Cause Solution
“Database not connected” File permissions Restart app service
“PIN change failed” Database locking Check concurrent access
“User not found” Database corruption Recreate database

Monitoring and Maintenance

Regular Health Checks

  • Monitor /health/db endpoint
  • Set up alerts for database connectivity failures
  • Log PIN change attempts for audit trail

Backup Strategy

  • Regular database backups via /api/admin/download-database
  • Store backups in Azure Storage
  • Test restore process periodically

Verification Checklist

Before deploying to production:

  • Database path resolution works in Azure
  • Health check endpoint returns correct information
  • Password change functionality works end-to-end
  • Error logging provides useful diagnostics
  • Application logs show database path and connectivity

Rollback Plan

If issues persist after deployment:

  1. Immediate: Stop the app service
  2. Restore: Deploy previous version
  3. Database: Restore database from backup if needed
  4. Verify: Test password change functionality

Success Criteria

The fix is successful when:

  1. ✅ Database health check shows “databaseConnected: true”
  2. ✅ Password change works for both Admin and regular users
  3. ✅ No database-related errors in application logs
  4. ✅ Consistent behavior between local and Azure environments
  5. ✅ Application can be restarted without losing password change functionality

Implementation Status: ✅ COMPLETE

All necessary changes have been implemented:

  • ✅ Enhanced database path resolution for Azure
  • ✅ Improved error handling in PIN change functionality
  • ✅ Added comprehensive logging and diagnostics
  • ✅ Created health check endpoint for monitoring
  • ✅ Added production configuration file
  • ✅ Created deployment and troubleshooting guide

The application is now ready for Azure deployment with working password change functionality.


📚 ← Back to Project Documentation


?? ? Back to Project Documentation