how can the credentials be stored (securely) in a configuration file to avoid prompting upon each connection?
Below is a PowerShell example to save the password to a file:
# save encrypted password to file
Add-Type -AssemblyName System.Security
$filePath = "C:\secrets\password.txt"
$password = Read-Host "Enter password"
$passwordBytes = [System.Text.Encoding]::Unicode.GetBytes($password)
$protectedPasswordBytes = [System.Security.Cryptography.ProtectedData]::Protect( `
$passwordBytes, $null, [System.Security.Cryptography.DataProtectionScope]::CurrentUser)
$protectedPasswordBase64String = [System.Convert]::ToBase64String($protectedPasswordBytes)
[void](New-Item -Path $filePath -ItemType File -Force)
$protectedPasswordBase64String | Out-File $filePath
And an example to decrypt the password and build an ODBC connection string for a DSN:
# get encrypted password from file and build connection string
Add-Type -AssemblyName System.Security
$filePath = "C:\secrets\password.txt"
$protectedPasswordBase64String = Get-Content -Path "C:\secrets\password.txt"
$password = [System.Text.Encoding]::Unicode.GetString([System.Security.Cryptography.ProtectedData]::Unprotect( `
[System.Convert]::FromBase64String($protectedPasswordBase64String), $null, `
[System.Security.Cryptography.DataProtectionScope]::CurrentUser))
$connectionString = "DSN=YourDSN;UID=YourLogin;PWD=$password"
This can be translated into .NET code in the language of your choice.