Click here to Skip to main content
15,884,298 members
Articles / Desktop Programming / MFC

Strategy to distribute secure database connection strings in an enterprise environment

Rate me:
Please Sign up or sign in to vote.
4.75/5 (11 votes)
29 Nov 20034 min read 78.4K   1.1K   30  
The article discusses a strategy to securely configure and administer a set of connection strings which can be maintained environment wise. It also talks about distributing this information securely in a huge environment to be used by authorized clients only.
// Copyright � 2003
// Author : Sriram Chitturi
// Date : November 29, 2003

using System;
using Microsoft.Win32;
using System.Configuration;

namespace EnterpriseConnString
{
	// Singleton class to get the keyword
	// from either Registry or config file

	// Customize this class to read KeyWord from any source
	public class Keyword
	{
		private static string m_keyword = null;

		private Keyword()
		{
		}
		public static string GetKeyWord()
		{
			// use any of the methods below to retrieve a keyword, depends on
			// enterprise level security

			if (m_keyword == null)
			{
				// m_keyword = ReadFromRegistry();
				m_keyword = ReadFromConfigFile();
			}
			if (m_keyword.Length != 8)
			{
				m_keyword = null;
				throw new Exception("Keyword should be 8 characters since DES encryption is being used");
			}

			return m_keyword;
		}

		private static string ReadFromRegistry()
		{
			// keyword should be already present in the registry
			// If you want to create keyword in registry
			// use a reg file with the following 3 lines

			// Windows Registry Editor Version 5.00
			// [HKEY_LOCAL_MACHINE\SOFTWARE\ECS]
			// "keyword"="MyKeyWord"

			RegistryKey HKLM = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\ECS");
			string regvalue = (string)HKLM.GetValue("KeyWord");
			if (regvalue == null)
				throw new Exception("KeyWord not found in Registry!");
			return regvalue;
		}

		private static string ReadFromConfigFile()
		{
			// use a config file to set the keyword
			// administrators can use machine.config files to set enterprise wide
			// keywords. This can enable administrators to change keywords often.
			// Note : Whenever the keyword is changed, run admin console to
			//			update the server Registry

			// I am using a local app.config file to set the keyword as an example
			string [] values = ConfigurationSettings.AppSettings.GetValues("keyword");
			if (values == null)
				throw new Exception("Keyword not found in config file!");
			return (string)(values[0]);
		}
	
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions