How to securely store a connection string in a WinForms application?

Simply , the .net framework allows you to do that , see http://msdn.microsoft.com/en-us/library/89211k9b(v=vs.80).aspx Relevant information: This goes into the machine.config file: <configProtectedData defaultProvider=”RsaProtectedConfigurationProvider”> <providers> <add name=”RsaProtectedConfigurationProvider” type=”System.Configuration.RsaProtectedConfigurationProvider, … /> <add name=”DataProtectionConfigurationProvider” type=”System.Configuration.DpapiProtectedConfigurationProvider, … /> </providers> </configProtectedData> And this is the application code: Shared Sub ToggleConfigEncryption(ByVal exeConfigName As String) ‘ Takes the executable file name without … Read more

Persist Security Info Property=true and Persist Security Info Property=false

Even if you set Persist Security Info=true OR Persist Security Info=false it won’t show a difference up front. The difference is happening in the background. When Persist Security Info=False, security-sensitive information, such as the password, is not returned as part of the connection if the connection is open or has ever been in an open … Read more

App.config connection string relative path

You can specify a relative path as described in Lefty’s answer. However this will be relative to the current working directory, which will not necessarily be the directory containing your executable. One way round this is to modify the connection string before using it, e.g. In app.config: connectionString=”data source={AppDir}\data\EmailDatabase.sqlite In your code: ConnectionStringSettings c = … Read more

How do I use Web.Config transform on my connection strings?

This works for me but I too have found it to be a bit flakey at times. You will need to create another file called Web.Config.Release and fill it with the following: <configuration xmlns:xdt=”http://schemas.microsoft.com/XML-Document-Transform”> <connectionStrings> <add name=”local” connectionString=”Data Source=IPAddress,Port;Initial Catalog=SomeOtherDB;User ID=TopSecretUsername;Password=SecurePassword” xdt:Transform=”SetAttributes” xdt:Locator=”Match(name)”/> </connectionStrings> <system.web> <compilation xdt:Transform=”RemoveAttributes(debug)” /> </system.web> <appSettings> <add key=”default_db_connection” value=”local” xdt:Transform=”SetAttributes” xdt:Locator=”Match(key)” … Read more

Get ConnectionString from appsettings.json instead of being hardcoded in .NET Core 2.0 App

STEP 1: Include the following in OnConfiguring() protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { IConfigurationRoot configuration = new ConfigurationBuilder() .SetBasePath(AppDomain.CurrentDomain.BaseDirectory) .AddJsonFile(“appsettings.json”) .Build(); optionsBuilder.UseSqlServer(configuration.GetConnectionString(“DefaultConnection”)); } STEP 2: Create appsettings.json: { “ConnectionStrings”: { “DefaultConnection”: “Server=YOURSERVERNAME; Database=YOURDATABASENAME; Trusted_Connection=True; MultipleActiveResultSets=true” } } STEP 3: Hard copy appsettings.json to the correct directory Hard copy appsettings.json.config to the directory specified in the … Read more

keyword not supported data source

What you have is a valid ADO.NET connection string – but it’s NOT a valid Entity Framework connection string. The EF connection string would look something like this: <connectionStrings> <add name=”NorthwindEntities” connectionString= “metadata=.\Northwind.csdl|.\Northwind.ssdl|.\Northwind.msl; provider=System.Data.SqlClient; provider connection string=&quot;Data Source=SERVER\SQL2000;Initial Catalog=Northwind;Integrated Security=True;MultipleActiveResultSets=False&quot;” providerName=”System.Data.EntityClient” /> </connectionStrings> You’re missing all the metadata= and providerName= elements in your EF connection … Read more

SQL Server Express connection string for Entity Framework Code First

The problem with your connection string here is: <add name=”TrempimModel” connectionString=”data source=.\SQLEXPRESS;Integrated Security=SSPI; AttachDBFilename=|DataDirectory|aspnetdb.sdf; User Instance=true” providerName=”System.Data.SqlClient” /> You’re basically defining what “server” you’re connecting to – but you’re not saying what database inside the file to connect to. Also – the file extension for SQL Server Express database files is .mdf (not .sdf – … Read more