Need help with dapper to prevent sql injection on database parameter!
Hello, this is my current endpoint:
Now reading the documentation of dapper they implement a endpoint like this:
How can I implement this on the database parameter from my code?
Thanks in advance!
16 Replies
by database variable do you mean
databaseName
?Yes
Parameter*
for what i know you select the database from the connection string
Yea i know what you mean but in my case the api supports multiple databases. So in the actual front end a user can choose what database he wants to do certain CRUD operations in
then build up a connection string and set the database's variable in the connection string based on the user's input
So you are saying instead of doing
Something like
the
{variable}
wont get converted, you need to put the dollar sign before the first quote of the stringyea i know, it was just to get an indication of if this is what u meant
and.. yes
is this safer? does this prevent sql injection ? because we removed the problem while trying to use dapper to prevent sql injection but now it feels like we just moved the parameter to somewhere else without fixing the sql injection ?
well you cant put sql in the sql connection
true
Okay that worked but I feel like declaring the connection string for every end point like this bad practice?
Is there a way I can maybe put a parameter inside the appsettings.json somehow so i can do something like
you could make multiple connection string configs and set the key as the db name
Right, is that something that is a good solution for a problem like this ?
never really had to do multiple databases from a single endpoint where the user specifies the db but..
at the very least it's not a bad solution... you are retrieving the connection string the same way you did before, but instead of hard coding default, the user passes the value
they could of course pass a value that does not exist as a db and you should account for that
true ill keep that in mind, thanks! also thank you @ℝ ill close the post