6
votes

I have created a web application. I am trying to connect to MS Sql database through entity framework.

I have an issue in the below connection string in appsettings.json file.

In the connection string password contains semicolon as below.

DefaultConnection": "Server=server name; Database=my db; User Id=myuser; Password=HjkT;tk-k@=?55\; MultipleActiveResultSets=true;

while i am trying to access the entity framework getting this error

An unhandled exception occurred while processing the request.

ArgumentException: Keyword not supported: 'tk-k@'

I tried putting this password in double quotes/single quote/" . But still facing this error.

1
Can you change your password to not have a semi-colon in it? - tadman
@tadman Thats not in my hand, I asked to my client to change it. but here should be some solution.. right? - jkyadav
What about putting a backslash "\" before the semicolon? - Md Hasan Ibrahim
@HasanIbrahim I tried that still same issue.. - jkyadav
You need to set it as literal somehow. Hasan has an idea that could work. If you can find a reference for the format of this DSN maybe that's the key. I go out of my way to set passwords that work with these sorts of descriptors. - tadman

1 Answers

9
votes

That's not appsettings.json issue, it's connection string issue.

The basic format of a connection string includes a series of keyword/value pairs separated by semicolons. The equal sign (=) connects each keyword and its value. To include values that contain a semicolon, single-quote character, or double-quote character, the value must be enclosed in double quotation marks
from here

So your connection string shoud look like:

Server=server name; Database=my db; User Id=myuser; Password="HjkT;tk-k@=?55\"; MultipleActiveResultSets=true;

And as soon as quote and backslash are special symbols for json - you should escape them with backslash. Here is your file:

{
  "DefaultConnection": "Server=server name; Database=my db; User Id=myuser; Password=\"HjkT;tk-k@=?55\\\"; MultipleActiveResultSets=true;"
}