Click here to Skip to main content
15,892,809 members
Articles / Web Development / HTML

Implementing WS-SecureConversation in Microsoft IssueVision

Rate me:
Please Sign up or sign in to vote.
4.61/5 (12 votes)
27 Sep 20056 min read 73.3K   776   38  
Adding secure communications to the Microsoft IssueVision sample application using WSE 2.0.
using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Configuration;
using System.Diagnostics;
using System.Drawing;
using System.Resources;
using System.Windows.Forms;

namespace IssueVision
{
	public class LoginForm : Form
	{
		private PictureBox PictureBox1;
		private Button btnCancel;
		private Button btnOk;
		private Timer fadeTimer;
		private TextBox txtUserName;
		private TextBox txtPassword;
		private Label Label3;
		private Label labelConnect;
		private Label Label1;
		private Label Label2;

		private bool m_showing = true;

		private IContainer components = null;

		#region Windows Form Designer generated code
		public LoginForm()
		{
			InitializeComponent();
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing && components != null)
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		[DebuggerStepThroughAttribute()]
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(LoginForm));
			this.btnCancel = new System.Windows.Forms.Button();
			this.btnOk = new System.Windows.Forms.Button();
			this.txtPassword = new System.Windows.Forms.TextBox();
			this.txtUserName = new System.Windows.Forms.TextBox();
			this.Label2 = new System.Windows.Forms.Label();
			this.Label1 = new System.Windows.Forms.Label();
			this.PictureBox1 = new System.Windows.Forms.PictureBox();
			this.labelConnect = new System.Windows.Forms.Label();
			this.Label3 = new System.Windows.Forms.Label();
			this.fadeTimer = new System.Windows.Forms.Timer(this.components);
			this.SuspendLayout();
			// 
			// btnCancel
			// 
			this.btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
			this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnCancel.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnCancel.Location = new System.Drawing.Point(232, 200);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(72, 23);
			this.btnCancel.TabIndex = 5;
			this.btnCancel.Text = "Cancel";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// btnOk
			// 
			this.btnOk.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
			this.btnOk.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnOk.Location = new System.Drawing.Point(136, 200);
			this.btnOk.Name = "btnOk";
			this.btnOk.Size = new System.Drawing.Size(80, 23);
			this.btnOk.TabIndex = 4;
			this.btnOk.Text = "OK";
			this.btnOk.Click += new System.EventHandler(this.btnOk_Click);
			// 
			// txtPassword
			// 
			this.txtPassword.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.txtPassword.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.txtPassword.Location = new System.Drawing.Point(88, 160);
			this.txtPassword.Name = "txtPassword";
			this.txtPassword.PasswordChar = '●';
			this.txtPassword.Size = new System.Drawing.Size(216, 21);
			this.txtPassword.TabIndex = 2;
			this.txtPassword.Text = "demo";
			this.txtPassword.Enter += new System.EventHandler(this.txtPassword_TextChanged);
			// 
			// txtUserName
			// 
			this.txtUserName.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.txtUserName.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.txtUserName.Location = new System.Drawing.Point(88, 128);
			this.txtUserName.Name = "txtUserName";
			this.txtUserName.Size = new System.Drawing.Size(216, 21);
			this.txtUserName.TabIndex = 1;
			this.txtUserName.Text = "demo";
			this.txtUserName.Enter += new System.EventHandler(this.txtUserName_TextChanged);
			// 
			// Label2
			// 
			this.Label2.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Label2.Location = new System.Drawing.Point(8, 160);
			this.Label2.Name = "Label2";
			this.Label2.Size = new System.Drawing.Size(72, 16);
			this.Label2.TabIndex = 8;
			this.Label2.Text = "&Password:";
			// 
			// Label1
			// 
			this.Label1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Label1.Location = new System.Drawing.Point(8, 128);
			this.Label1.Name = "Label1";
			this.Label1.Size = new System.Drawing.Size(72, 16);
			this.Label1.TabIndex = 6;
			this.Label1.Text = "&Username:";
			// 
			// PictureBox1
			// 
			this.PictureBox1.Dock = System.Windows.Forms.DockStyle.Top;
			this.PictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("PictureBox1.Image")));
			this.PictureBox1.Location = new System.Drawing.Point(0, 0);
			this.PictureBox1.Name = "PictureBox1";
			this.PictureBox1.Size = new System.Drawing.Size(316, 64);
			this.PictureBox1.TabIndex = 9;
			this.PictureBox1.TabStop = false;
			// 
			// labelConnect
			// 
			this.labelConnect.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.labelConnect.Location = new System.Drawing.Point(8, 88);
			this.labelConnect.Name = "labelConnect";
			this.labelConnect.Size = new System.Drawing.Size(310, 23);
			this.labelConnect.TabIndex = 10;
			this.labelConnect.Text = "http://localhost/replaced";
			// 
			// Label3
			// 
			this.Label3.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.Label3.Location = new System.Drawing.Point(8, 72);
			this.Label3.Name = "Label3";
			this.Label3.Size = new System.Drawing.Size(296, 23);
			this.Label3.TabIndex = 10;
			this.Label3.Text = "Connect to";
			// 
			// fadeTimer
			// 
			this.fadeTimer.Interval = 50;
			this.fadeTimer.Tick += new System.EventHandler(this.fadeTimer_Tick);
			// 
			// LoginForm
			// 
			this.AcceptButton = this.btnOk;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
			this.CancelButton = this.btnCancel;
			this.ClientSize = new System.Drawing.Size(316, 240);
			this.Controls.Add(this.labelConnect);
			this.Controls.Add(this.PictureBox1);
			this.Controls.Add(this.btnCancel);
			this.Controls.Add(this.btnOk);
			this.Controls.Add(this.txtPassword);
			this.Controls.Add(this.txtUserName);
			this.Controls.Add(this.Label2);
			this.Controls.Add(this.Label1);
			this.Controls.Add(this.Label3);
			this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "LoginForm";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "IssueVision Login";
			this.Closing += new System.ComponentModel.CancelEventHandler(this.LoginForm_Closing);
			this.Load += new System.EventHandler(this.LoginForm_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void LoginForm_Load(object sender, EventArgs e)
		{
			labelConnect.Text = ConfigurationSettings.AppSettings["DataServiceUrl"].ToString();
			
			Opacity = 0.0;
			Activate();
			Refresh();
			fadeTimer.Start();
			Refresh();
			
			if (UserSettings.Instance != null)
			{
				txtUserName.Text = UserSettings.Instance.Username;
				txtPassword.Text = UserSettings.Instance.Password;
				
				Authenticate();
			}
			else
			{
				txtUserName.Text = "demo";
				txtPassword.Text = "demo";
			}
		}
		
		private void Authenticate()
		{
			Cursor = Cursors.WaitCursor;
			Refresh();
			WebServicesExceptionType exceptionType = WebServicesLayer.Authenticate(txtUserName.Text, txtPassword.Text);
			
			switch (exceptionType)
			{
				case WebServicesExceptionType.None:
					UserSettings.Initialize();
					UserSettings.Instance.Username = txtUserName.Text;
					UserSettings.Instance.Password = txtPassword.Text;
					UserSettings.Instance.Serialize();
					DialogResult = DialogResult.OK;
					return;
					
				case WebServicesExceptionType.WebException:
					if (UserSettings.CanWorkOffline())
					{
						UserSettings.Instance.WorkOffline = true;
						UserSettings.Instance.Serialize();
						MessageBox.Show(WebServicesLayer.IV_WEBEXCEPTIONOFFLINEOK_TEXT, 
										WebServicesLayer.IV_WEBEXCEPTIONOFFLINEOK_TITLE, 
										MessageBoxButtons.OK, 
										MessageBoxIcon.Error);
						DialogResult = DialogResult.OK;
						return;
					}
					else
					{
						Visible = true;
						WebServicesLayer.HandleWebServicesException(WebServicesExceptionType.WebException);
						break;
					}
					
				case WebServicesExceptionType.SoapException:
					Visible = true;
					WebServicesLayer.HandleWebServicesException(WebServicesExceptionType.SoapException);
					txtPassword.Text = String.Empty;
					break;

				case WebServicesExceptionType.Exception:
					Visible = true;
					WebServicesLayer.HandleWebServicesException(WebServicesExceptionType.Exception);
					break;
			}
			
			DialogResult = DialogResult.None;
			Cursor = Cursors.Default;
			Refresh();
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (txtUserName.Text.Trim().Length > 0 && txtPassword.Text.Trim().Length > 0)
			{
				Authenticate();
			}
			else
			{
				MessageBox.Show("Enter a valid username and password.", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
				DialogResult = DialogResult.None;
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			DialogResult = DialogResult.Cancel;
		}

		private void txtUserName_TextChanged(object sender, EventArgs e)
		{
			txtUserName.SelectAll();
		}

		private void txtPassword_TextChanged(object sender, EventArgs e)
		{
			txtPassword.SelectAll();
		}

		private void LoginForm_Closing(object sender, CancelEventArgs e)
		{
			m_showing = false;
			fadeTimer.Start();
		}

		private void fadeTimer_Tick(object sender, EventArgs e)
		{
			if (m_showing)
			{
				double d = 1000.0 / fadeTimer.Interval / 100.0;
				if (Opacity + d >= 1.0)
				{
					Opacity = 1.0;
					fadeTimer.Stop();
				}
				else
				{
					Opacity += d;
				}
			}
			else
			{
				double d = 1000.0 / fadeTimer.Interval / 100.0;
				if (Opacity - d <= 0.0)
				{
					Opacity = 0.0;
					fadeTimer.Stop();
				}
				else
				{
					Opacity -= d;
				}
			}
		}
	}
}

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
Software Developer (Senior)
United States United States
Weidong has been an information system professional since 1990. He has a Master's degree in Computer Science, and is currently a MCSD .NET

Comments and Discussions