Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I want to put set of special characters as a key value under appsettings tag in web.config file.
Special characters to set as a key value - @"\|!#$%&()=?»«£§€{};'<>,+*^%"
i tried as below but it is not coming right

What I have tried:

Tried to set in web.config file under appsettings tag as a key value as below:
<add key="BlockedSpChacters" value=@"\|!#$%&()=?»«£§€{};'<>,+*^%"/>
Posted
Updated 11-Nov-21 2:58am

1 solution

The config file is an XML file. Attribute values need to be encoded properly for XML.

Specifically, & needs to be encoded as &amp;; < needs to be encoded as &lt;; and > needs to be encoded as &gt;.

You also can't use the C# "verbatim string" prefix (@) on the attribute value.
XML
<add key="BlockedSpChacters" value="\|!#$%&amp;()=?»«£§€{};'&lt;&gt;,+*^%"/>

NB: Your setting name makes me suspect that you're trying to filter out "bad" characters from values that you're injecting into a SQL query. If that's the case, stop immediately. Your code will be vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900