The character breaks passwords that are stored in the web.config
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Special characters like &, <, >, ", and ' break passwords stored in web.config because the file is XML. These characters have special meaning in XML and must be escaped with XML entities (e.g., & for &). If you store a password like P@ss&word directly in web.config, the XML parser interprets & as the start of an entity reference and fails to parse the file, causing your application to crash on startup.
The Problem
The XML parser sees &word and tries to interpret it as an XML entity (like & or <). Since &word; is not a valid entity, parsing fails.
XML Special Characters and Their Escapes
| Character | XML Entity | Description |
& | & | Ampersand |
< | < | Less than |
> | > | Greater than |
" | " | Double quote |
' | ' | Single quote (apostrophe) |
Fix 1: Escape Special Characters
The XML parser converts entities back to their characters when reading the value, so the actual password used at runtime is P@ss&word.
Fix 2: Use CDATA Section
CDATA is useful for custom config sections where you control the XML structure, but it cannot be used in attribute values (which is how standard appSettings and connectionStrings store data).
Fix 3: Store Encrypted Passwords
ASP.NET automatically decrypts at runtime. This solves both the special character problem and the security concern of plaintext passwords.
Fix 4: Use Environment Variables or User Secrets
Fix 5: URL-Encode in Connection Strings
Some database drivers accept URL-encoded values:
This depends on the database driver supporting URL-encoded connection strings. SQL Server's SqlClient does not URL-decode — use XML escaping instead.
Common Special Character Passwords
Programmatic Config Access
Common Pitfalls
- Double-escaping: If you write
&amp;in web.config, the application reads&(not&) as the password. Only escape once — use&in XML to get&at runtime. - Forgetting
&in connection strings:Server=db;User=admin&Password=passis not a valid connection string anyway (semicolons separate parts, not ampersands), but if your password contains&, it must be escaped as&. - CDATA in attributes:
<add value="<![CDATA[text]]>" />does not work — CDATA is only valid in element content, not attributes. The literal string<![CDATA[text]]>becomes the value. - Editing with text editors that auto-escape: Some editors double-escape when you save. If you type
&and the editor saves&amp;, your password breaks. Use Visual Studio's config editor or verify the raw XML. - Encrypted sections with special chars: Encrypt the section before adding special characters to avoid escaping issues entirely.
aspnet_regiishandles escaping internally during encryption.
Summary
- XML special characters (
&,<,>,",') must be escaped in web.config values - Use
&for&,<for<,>for>,"for" - ASP.NET automatically unescapes XML entities when reading config values at runtime
- Encrypt connection strings with
aspnet_regiisto avoid escaping and improve security - In .NET Core/.NET 5+, use
appsettings.json(JSON) or environment variables to avoid XML escaping entirely - Only escape once — double-escaping causes the escaped characters to appear in the actual password
Related reading
- The ciphertext refers to a customer master key that does not exist,
- The difference between AWS Amplify and amazon-cognito-identity-js?
- The identity used to sign the executable is no longer valid
- The remote server returned an error 407 Proxy Authentication Required
- The current .NET SDK does not support targeting .NET 6.0. Either target .NET 5.0 or lower, or use a version of the .NET SDK that supports .NET 6.0
- The current .NET SDK does not support targeting .NET Core 2.1. Either target .NET Core 2.0 or lower, or use a .NET SDK that supports .NET Core 2.1
- The resource could not be loaded because the App Transport Security policy requires the use of a secure connection
- The security token included in the request is expired

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.