connection string issue in web.config of ASP.Net - c#

I am using VSTS 2010 + C# + .Net 4.0 to develop an ASP.Net application using SQL Server 2008 Enterprise as database. I am learning someone else's code. I notice code like this, I am not sure whether the usage is correct and so I come here to ask for advice.
In the code, I see some code like this, I want to know whether using such method to read connection string from web.config is correct?
ConfigurationManager.ConnectionStrings["DBConnectinString"].ConnectionString
and such code is used to read connection string from web.config like below, please notice that connection string is defined outside of system.web section.
<?xml version="1.0"?>
<configuration>
<configSections>
<!--this section is empty-->
</configSections>
<appSettings>
...... content of appSettings
</appSettings>
<connectionStrings>
<add name="DBConnectinString" connectionString="data Source=.;uid=foo;pwd=foo;database=FOODB" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
... ... content of system.web
</system.web>
</configuration>

That is the standard place to define connection strings in web.config, yes.
See this for more info: http://msdn.microsoft.com/en-us/library/ms178411.aspx

Related

Configuration file - encrypting connection string

I'm trying to develop a C# Winform application, which connects to SQL database.
So far I was able to move the most sensitive data from my XML configuration file to an external XML configuration file, but that's it.
The last thing I have to do is to encrypt that file, as many people will have access to a directory in which application is located.
My main [APP] configuration file looks as follows:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings configSource="conn_string.config"/>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>
And there is my [conn_string] external configuration file in which I'm trying to hide a connection string:
<?xml version="1.0" encoding="utf-8" ?>
<connectionStrings>
<add name="myConnectionStringName"
providerName="System.Data.SqlClient"
connectionString="Data Source=ServerName;Initial
Catalog=InitialDatabaseName;User=UserName;Password=MyPassword;Application Name=MyAppName" />
</connectionStrings>
Now when it comes to encryption I have read that asp-netregiis.exe is looking only for file named "web"
so I temporarily renamed my "conn_string" file to "web"
And tried the encryption(via developer command line VS):
aspnet_regiis -pef "connectionStrings" "path_to_my_conn_string_file"
The result is: ~My translation
The web.config file doesn't contain a configuration tag
So I added one like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="myConnectionStringName"
providerName="System.Data.SqlClient"
connectionString="Data Source=ServerName;Initial
Catalog=InitialDatabaseName;User=UserName;Password=MyPassword;Application Name=MyAppName" />
</connectionStrings>
</configuration>
Now it complains about: ~Again my translation
File format configSource must be an element conatining section name
The steps you are taking that use aspnet_Regiis are really intended for web applications hosted in Internet Information Server (IIS). The file it is looking for is really "web.config." You mentioned that the app being constructed is a winforms application, which isn't a web application. Regular winforms applications are generally configured via a file called "app.config." Visual Studio may have created a base app.config for you depending on the version you're using.
You can "trick" aspnet_Regiis into encrypting your configuration file by temporarily renaming app.config to web.config, and then invoking aspnet_regiis with a flag that points to the exact path of our "phony" web.config:
For simplicity, let's say your initial app.config resides in c:\MyPrograms\MyApp.
Rename app.config to web.config.
From an administrative command prompt, set your current directory to c:\windows\micrsoft.net\framework\v4.0.30319
Invoke aspnet_regiis, using the "-pef" switch to instruct the tool to encrypt a particular section of your web.config:
aspnet_regiis -pef "connectionStrings" c:\MyPrograms\MyApp
If you see a "Succeeded" message, rename your web.config back to app.config, and run your application. .NET should decrypt your connection string automatically at runtime on that machine.
If you need to put this application on other machines, you may need to consider setting up a common encryption key that can be installed on other machines as well as define a provider in web.config that leverages that key. But for now, let's get the basic process working locally, and then worry about the other components once we know this part is working.
Hope this helps!

Connection string in EF

I’m a student. And this is my first production level project. I’m developing a WPF application using Entity Framework, which will be running only on a tab. I have problem in choosing a database. Since this application is going to run only on one device and cloud database cannot be used, what would be the best option?
If I use mssql the connection string in my development environment and production environment differs. Am I wrong? If I’m right what would be the solution for having a connection string that works identically in both environments.
Thanks in advance.
Usually you always end up with a different connection string for development (ex: without password) and for production (ex: long password).
C# handles this with App.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="yourname" connectionString="..." providerName="MySql.Data.MySqlClient" />
</connectionStrings>
</configuration>
And then you'll add a transformation file that changes certain values of your App.config depending on the environment where you'll deploy it.
Typically there is an App.Release.config file that updates the connectionString when built in Release mode (versus Debug mode)
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="yourname"
connectionString="productionConnectionStringHere"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
</configuration>
As to which database you're going to use: It doesn't really matter. Have a look at Sqlite. Or Mongo if you don't have tabular data.

How to specify connection string for Entity Framework in WPF project?

I normally work with ASP.NET MVC web projects so the connection string always goes into the web.config file.
Where do I put it in my WPF project?
Put it in your app.config under the configSections tag
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
...
</configSections>
<connectionStrings>
<add name="MyModel" connectionString="MyConnectionString" />
</connectionStrings>
...
If you don't have an app.config file you can create one manually.
You'll probably have to create an App.config file in your project by hand. Then you can fill it up as usual.

Centralised Connection String for multiple application asp.net

I have a web server with multiple application running. All the application have their web.config file. If the database password changes due to Policy I have to manually change the password in each of web.config files in the app setting section.
I was reading about the connection string setting in machine.config file.
Now my question is if I put connection string in appsetting section of machine.config with name ConnectionString and same in my web.config file will it overwrite the machine.config file values.
In my machine.config following is the setting
<configuration>
....
<appSettings>
<add key="ConnectionString" value="value"/>
</appSettings>
</configuration>
similarly in my web.config file
<configuration>
....
<appSettings>
<add key="ConnectionString" value="value"/>
</appSettings>
</configuration>
And I get the value in my code as below
string conString=ConfigurationManager.AppSettings["ConnectionString"];
will I get the overloaded value?
What's going to help you out here is to store your connection string(s) in .config file and then reference them either using the file="" attribute or the configSource="" attribute.
Here's an excellent question and answer that talks about the differences between the two and shows you how to implement them:
ASP.NET web.config: configSource vs. file attributes

ConfigurationManager does not point to correct app.config

I am trying to use ConfigurationManager to get the connection string from a project call FileShareAccessLibrary.
This is the code I am writting in order to do this:
ConfigurationManager.ConnectionStrings["FileShareAccessLibrary"].ConnectionString
This is the content of app.Config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections></configSections>
<connectionStrings>
<add name="FileShareAccessLibrary" connectionString="......"
providerName="System.Data.SqlClient" />
<add name="FileShareAccessLibrary.Properties.Settings"
connectionString="..."
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
When I run my app I get a NullReferenceException because ConfigurationManager.ConnectionStrings["FileShareAccessLibrary"] returns null.
While debugging I noticed that none of the two connection strings are stored inside ConfigurationManager.ConnectionStrings so I figured that the ConfigurationManager is pointing to another file.
In my project I have not other app.config file.
Is there something I am doing wrong here?
Why is ConfigurationManager not getting my connection string?
If your FileShareAccessLibrary project is a class library rather than a windows forms application or console application then you will need to move the connection strings (and app settings if you have any) from the FileShareAccessLibrary config file to the config file of the application(s) that reference the FileShareAccessLibrary dll.

Categories