The Contoso Hotels application uses environment variables to store sensitive information such as database credentials. This approach ensures that:
- Sensitive data is not committed to source control
- Different environments can use different credentials
- Credentials can be managed centrally in deployment environments
For local development:
- Copy
.env.exampleto.envin the root directory - Set a secure password in the
.envfile - Use the
load-env.ps1script to load these variables in PowerShell - Never commit the
.envfile to Git
When using Docker:
- Copy
.devcontainer/.env.exampleto.devcontainer/.env - Set a secure password in the file
- The Docker Compose setup will automatically use these credentials
For production environments:
- Use a secure secret management service appropriate for your hosting platform
- Set environment variables through your deployment pipeline
- Rotate credentials regularly
- Consider using managed identity when deploying to cloud platforms
Database connection strings are managed as follows:
- Template connection strings with placeholders are stored in
appsettings.jsonandappsettings.Development.json.example - Actual connection strings with credentials are stored in
appsettings.Development.json(not committed to Git) - At runtime, placeholders like
${SQL_PASSWORD}are replaced with actual values from environment variables
- Use parameterized queries to prevent SQL injection
- Validate all user inputs
- Implement HTTPS for all communications
- Follow the principle of least privilege for database access
- Keep dependencies updated to patch security vulnerabilities