<p>The application my team is currently developing has a DLL that is used to perform all database access.  The application can not use a trusted connection because the database is behind a firewall and the domain server is not.  So it appears that the connection string needs to have a DB username and password.  The DLL currently has the database connection string hard coded, but I don't want to do this when we launch as the assembly can be disassembled and the username and password would be right there in the open.</p>

<p>One of the requirements is that the password needs to be changed once every few months, so we would need to roll that out to our internal user base.</p>

<p>Is there a way to store the password encrypted in such a way we can easily distribute to the entire user base without storing it in the assembly?</p>

<p>UPDATE: Thanks to everyone who's answered.  I'll try to answer some of the questions back to me...  The data DLL is used by both ASP.NET WebForms and VB.NET WinForms.  I understand that Applications can have their own config files, but I haven't seen anything on config files for DLLs.  Unfortunately, I can't get to the Jon Galloway post at work so I can't judge if that will work.  From a development standpoint, we don't want to use web services inhouse, but may be providing them to third parties sometime next year.  I don't think impersonation will work because we can't authenticate the user through the firewall.  As a user (or former user) can be an attacker, we're keeping it from everyone!</p>

[{'answer_id': 6117, 'author': 'Adam V', 'author_id': 517, 'author_profile': 'https://Stackoverflow.com/users/517', 'pm_score': 4, 'selected': True, 'text': '<p>I\'m not certain, but I believe you can put it in a config file and encrypt the config file.</p>\n\n<p>Update: See Jon Galloway\'s post <a href="http://weblogs.asp.net/jongalloway/archive/2008/04/13/encrypting-passwords-in-a-net-app-config-file.aspx" rel="nofollow noreferrer">here.</a></p>\n'}, {'answer_id': 6123, 'author': 'Kev', 'author_id': 419, 'author_profile': 'https://Stackoverflow.com/users/419', 'pm_score': 0, 'selected': False, 'text': '<p>If the app is an ASP.NET app then just encrypt the connection strings section of your <code>web.config</code>.</p>\n<p>If the app is a client application running on multiple machines, instead of storing the connection string locally, consider using a web service or some other kind of secure mechanism to store it centrally. This would facilitate easier updates in the future and you\'re not storing the connection string locally.</p>\n<p>Just some thoughts.</p>\n<p><strong>Updated:</strong> @<a href="https://stackoverflow.com/questions/6113/what-is-the-best-way-to-store-connection-string-in-net-dlls#6162">lassevk</a></p>\n<p><em>&quot;Storing the connection string on a server, and obtaining it through a web connection sounds good, until you realize that you need security on that web connection as well, otherwise an attacker could just as well impersonate your program and talk to the web connection.&quot;</em></p>\n<p>Security on the web service was implicit. Depending on the type of deployment there are a numerous options...for example client side certificates.</p>\n'}, {'answer_id': 6162, 'author': 'Lasse V. Karlsen', 'author_id': 267, 'author_profile': 'https://Stackoverflow.com/users/267', 'pm_score': 1, 'selected': False, 'text': '<p>I hate to say this but as soon as you put something on a client machine, security for that data goes out the window.</p>\n\n<p>If your program is going to decrypt that string, you need to assume that an attacker can do the same. Attaching a debugger to your program would be one way.</p>\n\n<p>Storing the connection string on a server, and obtaining it through a web connection sounds good, until you realize that you need security on that web connection as well, otherwise an attacker could just as well impersonate your program and talk to the web connection.</p>\n\n<p>Let me ask a question. Who are you hiding the connection string from? The user or an attacker? And if the user, why?</p>\n'}, {'answer_id': 6176, 'author': 'Brian Childress', 'author_id': 721, 'author_profile': 'https://Stackoverflow.com/users/721', 'pm_score': 0, 'selected': False, 'text': '<p>There are some other idea\'s also. You can always use impersonation. Also, you can use the Enterprise Library\'s (Common Library).</p>\n\n<pre><code>&lt;section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /&gt;\n&lt;enterpriseLibrary.ConfigurationSource selectedSource="Common"&gt;\n&lt;sources&gt;\n  &lt;add name="Common" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"\n    filePath="Config\\Exception.config" /&gt;\n&lt;/sources&gt;\n</code></pre>\n\n<p></p>\n'}, {'answer_id': 6180, 'author': 'EndangeredMassa', 'author_id': 106, 'author_profile': 'https://Stackoverflow.com/users/106', 'pm_score': 0, 'selected': False, 'text': '<p>.NET supports encryption on config values like this. You could leave it in a config file, but encrypted.</p>\n'}, {'answer_id': 6196, 'author': 'Brad Barker', 'author_id': 12081, 'author_profile': 'https://Stackoverflow.com/users/12081', 'pm_score': 0, 'selected': False, 'text': "<p>You want to be able to distribute the DLL with all of the setup information being in a configurable place, but the fact is you can't have one of the handy-dandy .NET config files for a DLL unless you do something custom.</p>\n\n<p>Maybe you need to rethink what responsibility your DLL should have. Would it be possible, or make sense to require that the connection string be passed in by the user of your library? Does it really make sense that your DLL reads a config file?</p>\n"}, {'answer_id': 25679, 'author': 'Webjedi', 'author_id': 1975, 'author_profile': 'https://Stackoverflow.com/users/1975', 'pm_score': 2, 'selected': False, 'text': "<p>Just assume that the bad guys WILL get the credentials out of your config file.  This means that they'd be able to login to your database and do whatever that user is capable of.  So just make sure that user can't do anything bad like access the tables directly.  Make that user only capable of executing certain stored procedures and you'll be in better shape.  This is one place that sprocs shine.</p>\n"}, {'answer_id': 747810, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>Several options:</p>\n\n<ol>\n<li>Store in web.config and encrypt</li>\n<li>Store in dll and obfuscate (dotfuscator)</li>\n<li>Store one in web.config (encrypted of course) and rest in the database (if you have to use multiple and encryption/decryption becomes a pain)</li>\n</ol>\n'}]
['https://Stackoverflow.com/questions/6113', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/702/']