Click here to Skip to main content
15,886,046 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to create a software for a library. for connection to database i used Codefirst overfolw.
I have a form in my project that user enter server name and user id and password in it.
Now I don't Know that where my connection string to save.
if I save connectionString to web.config or app.config , how edit it when user change your server name(or username or password)?
and essentially is correct that we store connection string in web.config ?(because any one can open it with notepad and get username and password)
Posted
Updated 2-Mar-15 19:33pm
v3
Comments
[no name] 3-Mar-15 2:39am    
http://www.aspsnippets.com/Articles/Change-Modify-connection-string-in-WebConfig-at-Runtime-in-ASPNet-using-C-and-VBNet.aspx

In your scenario, you want to create the connection string at run time, based on the user input. So storing the connection string in the web.config file is not a good solution, espectially for a web application where you are going to have multiple users accessing your application at the same time (they will overwrite each other!).

What you need to do is to create a new connection string object based on the user input. You can use the SqlConnectionStringBuilder class for that purpose.
See https://msdn.microsoft.com/en-us/library/ms254947(v=vs.110).aspx[^]

If you are using Entity Framework, you can pass a connection string to the DbContext constructor:

public DbContext(string nameOrConnectionString)
    Member of System.Data.Entity.DbContext


Summary:
Constructs a new context instance using the given string as the name or connection string for the database to which a connection will be made. See the class remarks for how this is used to create a connection.

Parameters:
nameOrConnectionString: Either the database name or a connection string.


IMPORTANT: risk of connection string injection attack!

A connection string injection attack can occur when dynamic string concatenation is used to build connection strings that are based on user input. If the string is not validated and malicious text or characters not escaped, an attacker can potentially access sensitive data or other resources on the server.

You've been warned!
 
Share this answer
 
Store your connection string like
<add name="MyConnection" connectionString="Data Source=(local);Initial Catalog=mydb;Integrated Security=False;user id={0};password={1};" providerName="System.Data.SqlClient" />


Access it like

string con = string.Format(System.Configuration.ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString, "usernameHere", "passwordHere");
 
Share this answer
 
You should store the ConnectionString in web.config. No one can access that from browser.

Refer - Programmatically Add or Update Connection String in ASP.Net Web.Config File[^]
 
Share this answer
 
Comments
F-ES Sitecore 3-Mar-15 5:00am    
Updating the web.config will recycle the app and destroy all sessions.
Yeah you are correct. He needs to implement this differently. I did not carefully read his problem statement.

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