
Editorial illustration.
Originally published 23 June 2023; rewritten and reviewed on 15 October 2026.
If you inherit a legacy .NET Framework ASP.NET WebForms or MVC application running on IIS, one of the first things that makes security auditors uncomfortable is a plaintext SQL Server password sitting inside <connectionStrings> in web.config. .NET Framework has a built-in feature called Protected Configuration that encrypts sections like connectionStrings or appSettings right inside the XML, while allowing ConfigurationManager to decrypt them transparently at runtime.
Before encrypting anything on a production server, though, you need to understand which provider you are using, who holds the decryption key, and how you will recover if that server dies.
Prefer aspnet_regiis.exe over running encryption from C# code
Many older tutorials show a snippet that encrypts web.config from inside a C# page or controller using WebConfigurationManager:
using System.Web.Configuration;
var configuration = WebConfigurationManager.OpenWebConfiguration("~");
var section = configuration.GetSection("connectionStrings");
if (section != null && !section.SectionInformation.IsProtected)
{
section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
section.SectionInformation.ForceSave = true;
configuration.Save();
}
While that API works when executed from a local administrative utility, you should never ship a live /encrypt.aspx or /admin/protect-config endpoint on a public website. Allowing the IIS worker process (w3wp.exe) to modify its own web.config requires write permissions on the application root — something you want locked down in production — and triggers an immediate AppDomain recycle.
Instead, run aspnet_regiis.exe from an elevated Command Prompt on the server during deployment:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis.exe `
-pef "connectionStrings" "C:\inetpub\wwwroot\MyLegacyApp" `
-prov "RsaProtectedConfigurationProvider"
To reverse it during troubleshooting, swap -pef for -pdf:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis.exe `
-pdf "connectionStrings" "C:\inetpub\wwwroot\MyLegacyApp"
Choose between RSA and DPAPI carefully
.NET Framework ships two default providers: DataProtectionConfigurationProvider (DPAPI) and RsaProtectedConfigurationProvider (RSA). DPAPI uses machine-specific keys tied to Windows, which is fine for a single standalone server, but breaks the moment you copy web.config to a second node behind a load balancer.
For a web farm — or even a single server where you want clean disaster recovery — use RsaProtectedConfigurationProvider with an exportable custom key container. You can create an exportable key container, export it to an encrypted XML backup stored in your password vault, and grant read access only to the specific IIS Application Pool identity:
# Create an exportable RSA key container
aspnet_regiis.exe -pc "MyAppConfigKeys" -exp
# Grant the IIS AppPool identity permission to read the private key
aspnet_regiis.exe -pa "MyAppConfigKeys" "IIS APPPOOL\MyLegacyAppPool"
# Export the key pair (including private key) for backup or other web nodes
aspnet_regiis.exe -px "MyAppConfigKeys" "C:\secure-backup\MyAppConfigKeys.xml" -pri
Without that -pa permission grant, your application will crash on startup with Failed to decrypt using provider 'RsaProtectedConfigurationProvider' ... The RSA key container could not be opened.
Know what encryption at rest does and does not protect
Encrypting web.config stops someone who casually opens the file on disk or grabs a misconfigured backup from reading your database password. It does not protect the secret from code running inside the same IIS process (such as a vulnerable third-party DLL or an RCE flaw), because IIS itself holds permission to decrypt the section.
It also does not excuse committing plaintext secrets into Git. Keep a sanitized web.config.example in source control, inject real values during deployment, and encrypt them on the target machine. If you are migrating the application toward .NET 8 or ASP.NET Core later, move away from web.config entirely in favour of environment variables, Azure Key Vault, or a dedicated secrets manager.
Test a cold start and a restore before calling it done
After encrypting web.config, recycle the Application Pool in IIS Manager, hit a page that queries the database, and check Windows Event Viewer for any configuration errors. Then verify that your exported key XML can actually be imported (aspnet_regiis.exe -pi) onto a staging box to decrypt a copy of the config. Encrypting a file without testing key recovery is one of the fastest ways to turn a routine server migration into an unexpected outage.