Click here to Skip to main content
15,892,005 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.6K   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.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using EnterpriseConnString;

namespace UseECS
{
	// Administration console GUI for creating connection strings.
	// Generally run from the server
	public class ConnStrGUI : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox KeyWordTxt;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.TextBox serverTxt;
		private System.Windows.Forms.ListView connStrList;
		private System.Windows.Forms.ColumnHeader EnvCol;
		private System.Windows.Forms.ColumnHeader ConnStrCol;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.TextBox EnvTxt;
		private System.Windows.Forms.TextBox ConnStrTxt;
		private System.Windows.Forms.Button CloseBtn;
		private System.Windows.Forms.Button connectBtn;
		private System.Windows.Forms.Button refreshBtn;
		private System.Windows.Forms.Button AddReplaceBtn;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.GroupBox groupBox3;

		private EntConnString m_connStr;
		private bool m_bConnected;

		public ConnStrGUI()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			m_bConnected = false;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(ConnStrGUI));
			this.label1 = new System.Windows.Forms.Label();
			this.KeyWordTxt = new System.Windows.Forms.TextBox();
			this.connStrList = new System.Windows.Forms.ListView();
			this.EnvCol = new System.Windows.Forms.ColumnHeader();
			this.ConnStrCol = new System.Windows.Forms.ColumnHeader();
			this.label2 = new System.Windows.Forms.Label();
			this.serverTxt = new System.Windows.Forms.TextBox();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.connectBtn = new System.Windows.Forms.Button();
			this.label5 = new System.Windows.Forms.Label();
			this.refreshBtn = new System.Windows.Forms.Button();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.AddReplaceBtn = new System.Windows.Forms.Button();
			this.ConnStrTxt = new System.Windows.Forms.TextBox();
			this.EnvTxt = new System.Windows.Forms.TextBox();
			this.label8 = new System.Windows.Forms.Label();
			this.label7 = new System.Windows.Forms.Label();
			this.CloseBtn = new System.Windows.Forms.Button();
			this.button1 = new System.Windows.Forms.Button();
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.groupBox3.SuspendLayout();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(22, 90);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(54, 14);
			this.label1.TabIndex = 0;
			this.label1.Text = "Keyword :";
			// 
			// KeyWordTxt
			// 
			this.KeyWordTxt.Enabled = false;
			this.KeyWordTxt.Location = new System.Drawing.Point(84, 88);
			this.KeyWordTxt.Name = "KeyWordTxt";
			this.KeyWordTxt.Size = new System.Drawing.Size(144, 20);
			this.KeyWordTxt.TabIndex = 4;
			this.KeyWordTxt.Text = "";
			// 
			// connStrList
			// 
			this.connStrList.BackColor = System.Drawing.Color.LightGoldenrodYellow;
			this.connStrList.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																						  this.EnvCol,
																						  this.ConnStrCol});
			this.connStrList.FullRowSelect = true;
			this.connStrList.GridLines = true;
			this.connStrList.Location = new System.Drawing.Point(6, 18);
			this.connStrList.Name = "connStrList";
			this.connStrList.Size = new System.Drawing.Size(372, 94);
			this.connStrList.TabIndex = 7;
			this.connStrList.View = System.Windows.Forms.View.Details;
			this.connStrList.DoubleClick += new System.EventHandler(this.OnListDblClick);
			// 
			// EnvCol
			// 
			this.EnvCol.Text = "Environment";
			this.EnvCol.Width = 119;
			// 
			// ConnStrCol
			// 
			this.ConnStrCol.Text = "Connection String";
			this.ConnStrCol.Width = 226;
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(36, 27);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(44, 14);
			this.label2.TabIndex = 3;
			this.label2.Text = "Server :";
			// 
			// serverTxt
			// 
			this.serverTxt.Location = new System.Drawing.Point(84, 24);
			this.serverTxt.Name = "serverTxt";
			this.serverTxt.Size = new System.Drawing.Size(234, 20);
			this.serverTxt.TabIndex = 1;
			this.serverTxt.Text = "";
			// 
			// groupBox1
			// 
			this.groupBox1.BackColor = System.Drawing.SystemColors.Control;
			this.groupBox1.Controls.Add(this.connectBtn);
			this.groupBox1.Controls.Add(this.label5);
			this.groupBox1.Controls.Add(this.label2);
			this.groupBox1.Controls.Add(this.serverTxt);
			this.groupBox1.Controls.Add(this.KeyWordTxt);
			this.groupBox1.Controls.Add(this.label1);
			this.groupBox1.Location = new System.Drawing.Point(6, 4);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(386, 122);
			this.groupBox1.TabIndex = 9;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Authentication to server";
			// 
			// connectBtn
			// 
			this.connectBtn.Location = new System.Drawing.Point(244, 86);
			this.connectBtn.Name = "connectBtn";
			this.connectBtn.Size = new System.Drawing.Size(110, 23);
			this.connectBtn.TabIndex = 5;
			this.connectBtn.Text = "Connect";
			this.connectBtn.Click += new System.EventHandler(this.OnConnect);
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(86, 50);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(274, 30);
			this.label5.TabIndex = 9;
			this.label5.Text = "User should have write permissions on [server]\\HKLM Registry key. Leave blank for" +
				" local machine.";
			// 
			// refreshBtn
			// 
			this.refreshBtn.BackColor = System.Drawing.SystemColors.Control;
			this.refreshBtn.Location = new System.Drawing.Point(64, 118);
			this.refreshBtn.Name = "refreshBtn";
			this.refreshBtn.Size = new System.Drawing.Size(110, 23);
			this.refreshBtn.TabIndex = 6;
			this.refreshBtn.Text = "Refresh";
			this.refreshBtn.Click += new System.EventHandler(this.OnRefresh);
			// 
			// groupBox2
			// 
			this.groupBox2.BackColor = System.Drawing.SystemColors.Control;
			this.groupBox2.Controls.Add(this.AddReplaceBtn);
			this.groupBox2.Controls.Add(this.ConnStrTxt);
			this.groupBox2.Controls.Add(this.EnvTxt);
			this.groupBox2.Controls.Add(this.label8);
			this.groupBox2.Controls.Add(this.label7);
			this.groupBox2.Location = new System.Drawing.Point(8, 284);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(378, 106);
			this.groupBox2.TabIndex = 12;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Add/Replace connection string";
			// 
			// AddReplaceBtn
			// 
			this.AddReplaceBtn.Location = new System.Drawing.Point(138, 78);
			this.AddReplaceBtn.Name = "AddReplaceBtn";
			this.AddReplaceBtn.Size = new System.Drawing.Size(102, 23);
			this.AddReplaceBtn.TabIndex = 10;
			this.AddReplaceBtn.Text = "Add / Replace";
			this.AddReplaceBtn.Click += new System.EventHandler(this.OnAddReplace);
			// 
			// ConnStrTxt
			// 
			this.ConnStrTxt.Location = new System.Drawing.Point(110, 46);
			this.ConnStrTxt.Name = "ConnStrTxt";
			this.ConnStrTxt.Size = new System.Drawing.Size(258, 20);
			this.ConnStrTxt.TabIndex = 9;
			this.ConnStrTxt.Text = "";
			// 
			// EnvTxt
			// 
			this.EnvTxt.Location = new System.Drawing.Point(110, 21);
			this.EnvTxt.Name = "EnvTxt";
			this.EnvTxt.Size = new System.Drawing.Size(164, 20);
			this.EnvTxt.TabIndex = 8;
			this.EnvTxt.Text = "";
			// 
			// label8
			// 
			this.label8.Location = new System.Drawing.Point(12, 48);
			this.label8.Name = "label8";
			this.label8.Size = new System.Drawing.Size(98, 16);
			this.label8.TabIndex = 1;
			this.label8.Text = "Connection string :";
			this.label8.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// label7
			// 
			this.label7.Location = new System.Drawing.Point(36, 24);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(74, 14);
			this.label7.TabIndex = 0;
			this.label7.Text = "Environment :";
			this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// CloseBtn
			// 
			this.CloseBtn.BackColor = System.Drawing.SystemColors.Control;
			this.CloseBtn.Location = new System.Drawing.Point(136, 400);
			this.CloseBtn.Name = "CloseBtn";
			this.CloseBtn.Size = new System.Drawing.Size(128, 23);
			this.CloseBtn.TabIndex = 11;
			this.CloseBtn.Text = "Close";
			this.CloseBtn.Click += new System.EventHandler(this.CloseBtn_Click);
			// 
			// button1
			// 
			this.button1.BackColor = System.Drawing.SystemColors.Control;
			this.button1.Location = new System.Drawing.Point(206, 118);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(110, 23);
			this.button1.TabIndex = 13;
			this.button1.Text = "Delete";
			this.button1.Click += new System.EventHandler(this.OnDelete);
			// 
			// groupBox3
			// 
			this.groupBox3.BackColor = System.Drawing.SystemColors.Control;
			this.groupBox3.Controls.Add(this.connStrList);
			this.groupBox3.Controls.Add(this.refreshBtn);
			this.groupBox3.Controls.Add(this.button1);
			this.groupBox3.Location = new System.Drawing.Point(6, 132);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(384, 146);
			this.groupBox3.TabIndex = 14;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "Existing connection strings on server";
			// 
			// ConnStrGUI
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.BackColor = System.Drawing.Color.Lavender;
			this.ClientSize = new System.Drawing.Size(400, 427);
			this.Controls.Add(this.groupBox3);
			this.Controls.Add(this.CloseBtn);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.groupBox1);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "ConnStrGUI";
			this.Text = "        Enterprise Connection Strings Admin Console";
			this.Load += new System.EventHandler(this.OnLoad);
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.groupBox3.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void CloseBtn_Click(object sender, System.EventArgs e)
		{
			Close();
		}

		// Function takes care of changing the button text to connect
		// and disconnect (toggle)
		private void OnConnect(object sender, System.EventArgs e)
		{
			if (m_bConnected)
			{
				m_connStr = null;
				connStrList.Items.Clear();
			}
			else
			{
				m_connStr = new EntConnString(serverTxt.Text, true);
				try
				{
					m_connStr.Connect();
					OnRefresh(sender, e);
				} 
				catch (Exception ex)
				{
					m_connStr = null;
					MessageBox.Show(ex.Message, "Error");
					return;
				}
			}
			connectBtn.Text = (m_bConnected) ? "Connect" : "Disconnect";
			m_bConnected = ! m_bConnected;
		}

		private void OnRefresh(object sender, System.EventArgs e)
		{
			if (m_connStr == null)
			{
				MessageBox.Show("Should Connect before refreshing", "Error");
				return;
			}
			connStrList.Items.Clear();
			try
			{
				Hashtable values = m_connStr.GetAllConnectionStrings();
				IDictionaryEnumerator enumerator = values.GetEnumerator();
				while (enumerator.MoveNext())
				{
					ListViewItem lvi = connStrList.Items.Add((string)enumerator.Key);
					lvi.SubItems.Add((string)enumerator.Value);
				}
				values.Clear();
			} 
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "Error");
			}
		}

		private void OnAddReplace(object sender, System.EventArgs e)
		{
			if (m_connStr == null)
			{
				MessageBox.Show("Should Connect before Adding or Replacing strings", "Error");
				return;
			}
			try
			{
				m_connStr.SetConnectionString(EnvTxt.Text, ConnStrTxt.Text);
				EnvTxt.Text = ConnStrTxt.Text = null;
				OnRefresh(sender, e);
			} 
			catch (Exception ex) 
			{
				MessageBox.Show(ex.Message, "Error");
			}
		}

		private void OnListDblClick(object sender, System.EventArgs e)
		{
			if (connStrList.SelectedItems.Count > 0)
			{
				EnvTxt.Text = connStrList.SelectedItems[0].SubItems[0].Text;
				ConnStrTxt.Text = connStrList.SelectedItems[0].SubItems[1].Text;
			}
		}

		private void OnDelete(object sender, System.EventArgs e)
		{
			try
			{
				if (connStrList.SelectedItems.Count > 0)
					m_connStr.DeleteConnectionString(
						connStrList.SelectedItems[0].SubItems[0].Text);
				OnRefresh(sender, e);
			} 
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "Error");
			}
		
		}

		// check for the keyword and if it is not found or not
		// 8 characters in length exit the application
		private void OnLoad(object sender, System.EventArgs e)
		{
			try 
			{
				this.KeyWordTxt.Text = Keyword.GetKeyWord();
			}
			catch(Exception ex) // error - the keyword is not set on this machine
			{
				MessageBox.Show(ex.Message, "Error");
				Close();
			}
		}

	}
}

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