Click here to Skip to main content
15,891,828 members
Articles / Web Development / ASP.NET

How to implement Two basics uses for the Asp.net Login control 2.0 (login and RememberMe)

Rate me:
Please Sign up or sign in to vote.
3.27/5 (32 votes)
23 Nov 20066 min read 406.2K   72  
This article will show you how to implement two basics uses of the ASP.NET Login control (2.0), The login task itself to validate the users who’s trying to access your web site. In addition i will show how to implement the REMEMBERME process using cookies to save the user name inside the user PC
using System;
using System.ComponentModel;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;

namespace ErikSchmidt
{
	/// <summary> 
	/// LoginControl is a free .NET login control with cryptography support.
	/// </summary>
	public class LoginControl : System.Windows.Forms.UserControl
	{
		///<event cref="Successful">
		/// Raised after a successful Login.
		/// </event>
		public event System.EventHandler Successful;
		///<event cref="Failed">
		/// Raised after a failed Login.
		/// </event>
		public event System.EventHandler Failed;
		///<event cref="Ready">
		///    Raised after the User clicked on Login. Use this event to monitor LoginControl.
		///</event>
		/// <example> This sample shows how to use Ready.
		/// <code>
		/// private void OnEventReady(object sender, System.EventArgs e)
		/// {
		///		MessageBox.Show("Username: " + loginControl1.Username + "\nPassword: " + loginControl1.Password,"LoginControl");
		/// }
		/// </code>
		/// </example>
		public event System.EventHandler Ready;
		private bool bCheck = true;
		private bool bHash= true;
		private string theUsername;
		private string thePassword;
		private string tmpU;
		private string tmpP;
		private string ErrMsg = "Please enter the correct Username / Password combination!";
		private System.Windows.Forms.Button bLogin;
		private System.Windows.Forms.Label lPassword;
		private System.Windows.Forms.Label lUsername;
		private System.Windows.Forms.TextBox tbPassword;
		private System.Windows.Forms.Label lHeader;
		private System.Windows.Forms.Label lText;
		private System.Windows.Forms.PictureBox pbImage;
		private System.Windows.Forms.TextBox tbUsername;
		private System.Windows.Forms.Panel pHeader;
		private System.Windows.Forms.ErrorProvider errorProvider;
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		/// <summary> 
		/// The public Initializer. Nothing happening here.
		/// </summary>
		public LoginControl()
		{
			InitializeComponent();
		}

		/// <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 Component 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(LoginControl));
			this.bLogin = new System.Windows.Forms.Button();
			this.lPassword = new System.Windows.Forms.Label();
			this.lUsername = new System.Windows.Forms.Label();
			this.tbPassword = new System.Windows.Forms.TextBox();
			this.pHeader = new System.Windows.Forms.Panel();
			this.lHeader = new System.Windows.Forms.Label();
			this.lText = new System.Windows.Forms.Label();
			this.pbImage = new System.Windows.Forms.PictureBox();
			this.tbUsername = new System.Windows.Forms.TextBox();
			this.errorProvider = new System.Windows.Forms.ErrorProvider();
			this.pHeader.SuspendLayout();
			this.SuspendLayout();
			// 
			// bLogin
			// 
			this.bLogin.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.bLogin.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.bLogin.Location = new System.Drawing.Point(330, 245);
			this.bLogin.Name = "bLogin";
			this.bLogin.Size = new System.Drawing.Size(70, 20);
			this.bLogin.TabIndex = 9;
			this.bLogin.Text = "Login";
			this.bLogin.Click += new System.EventHandler(this.OnClickLogin);
			// 
			// lPassword
			// 
			this.lPassword.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.lPassword.Location = new System.Drawing.Point(30, 205);
			this.lPassword.Name = "lPassword";
			this.lPassword.Size = new System.Drawing.Size(80, 20);
			this.lPassword.TabIndex = 11;
			this.lPassword.Text = "Password";
			this.lPassword.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// lUsername
			// 
			this.lUsername.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.lUsername.Location = new System.Drawing.Point(30, 165);
			this.lUsername.Name = "lUsername";
			this.lUsername.Size = new System.Drawing.Size(80, 20);
			this.lUsername.TabIndex = 10;
			this.lUsername.Text = "Username";
			this.lUsername.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// tbPassword
			// 
			this.tbPassword.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.tbPassword.Location = new System.Drawing.Point(130, 205);
			this.tbPassword.Name = "tbPassword";
			this.tbPassword.Size = new System.Drawing.Size(270, 20);
			this.tbPassword.TabIndex = 7;
			this.tbPassword.Text = "";
			// 
			// pHeader
			// 
			this.pHeader.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
			this.pHeader.Controls.AddRange(new System.Windows.Forms.Control[] {
																				  this.lHeader,
																				  this.lText,
																				  this.pbImage});
			this.pHeader.Dock = System.Windows.Forms.DockStyle.Top;
			this.pHeader.Name = "pHeader";
			this.pHeader.Size = new System.Drawing.Size(460, 60);
			this.pHeader.TabIndex = 8;
			// 
			// lHeader
			// 
			this.lHeader.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.lHeader.Location = new System.Drawing.Point(10, 10);
			this.lHeader.Name = "lHeader";
			this.lHeader.Size = new System.Drawing.Size(360, 20);
			this.lHeader.TabIndex = 3;
			this.lHeader.Text = "Login";
			this.lHeader.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// lText
			// 
			this.lText.Location = new System.Drawing.Point(20, 30);
			this.lText.Name = "lText";
			this.lText.Size = new System.Drawing.Size(360, 20);
			this.lText.TabIndex = 4;
			this.lText.Text = "Please enter your Username and Password.";
			this.lText.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// pbImage
			// 
			this.pbImage.Image = ((System.Drawing.Bitmap)(resources.GetObject("pbImage.Image")));
			this.pbImage.Location = new System.Drawing.Point(388, 5);
			this.pbImage.Name = "pbImage";
			this.pbImage.Size = new System.Drawing.Size(48, 48);
			this.pbImage.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
			this.pbImage.TabIndex = 5;
			this.pbImage.TabStop = false;
			// 
			// tbUsername
			// 
			this.tbUsername.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.tbUsername.Location = new System.Drawing.Point(130, 165);
			this.tbUsername.Name = "tbUsername";
			this.tbUsername.Size = new System.Drawing.Size(270, 20);
			this.tbUsername.TabIndex = 6;
			this.tbUsername.Text = "";
			// 
			// LoginControl
			// 
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.bLogin,
																		  this.lPassword,
																		  this.lUsername,
																		  this.tbPassword,
																		  this.pHeader,
																		  this.tbUsername});
			this.Name = "LoginControl";
			this.Size = new System.Drawing.Size(460, 300);
			this.Load += new System.EventHandler(this.OnLoad);
			this.pHeader.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		static void Main()
		{
		}

		/// <summary> 
		/// OnLoad currently only defines the value tbPassword.PasswordChar.
		/// </summary>
		private void OnLoad(object sender, System.EventArgs e)
		{
			tbPassword.PasswordChar = '\u25CF';
			if (bCheck)
			{
				tmpU = theUsername;
				tmpP = thePassword;
			}
		}

		private void OnClickLogin(object sender, System.EventArgs e)
		{
			if (!bCheck)
			{
				if (!bHash)
				{
					theUsername = tbUsername.Text;
					thePassword = tbPassword.Text;
					if (Ready != null)
					{
						Ready(this, new System.EventArgs());
					}
				}
				else
				{
					theUsername = HashString(tbUsername.Text);
					thePassword = HashString(tbPassword.Text);
					if (Ready != null)
					{
						Ready(this, new System.EventArgs());
					}
				}
			}
			else
			{
				bool b = CheckCredentials(tmpU, tmpP);
				if (b)
				{
						if (Ready != null)
				 {
					 Successful(this, new System.EventArgs());
				 }
				}
				else
				{
					if (Ready != null)
					{
						Failed(this, new System.EventArgs());
					}
				}
			}
		}
	
		/// <summary> 
		/// Public property defining the headline text.
		/// </summary>	
		public string labelHeadline
		{
			get
			{
				return lHeader.Text;
			}
			set
			{
				lHeader.Text = value;
			}
		}

		/// <summary> 
		/// Public property defining the header text.
		/// </summary>	
		public string labelHeader
		{
			get
			{
				return lText.Text;
			}
			set
			{
				lText.Text = value;
			}
		}

		/// <summary> 
		/// Public property defining the label text before the Username textbox.
		/// </summary>	
		public string labelUsername
		{
			get
			{
				return lUsername.Text;
			}
			set
			{
				lUsername.Text = value;
			}
		}

		/// <summary> 
		/// Public property defining the label text before the Password textbox.
		/// </summary>	
		public string labelPassword
		{
			get
			{
				return lPassword.Text;
			}
			set
			{
				lPassword.Text = value;
			}
		}

		/// <summary> 
		/// Public property defining the text on the login button.
		/// </summary>	
		public string labelButton
		{
			get
			{
				return bLogin.Text;
			}
			set
			{
				bLogin.Text = value;
			}
		}

		/// <summary> 
		/// Public property to transfer the Username between the Host Application and LoginControl.
		/// </summary>	
		public string Username
		{
			get
			{
				return theUsername;
			}
			set
			{
				theUsername = value;
			}
	
		}

		/// <summary> 
		/// Public property to transfer the Password between the Host Application and LoginControl.
		/// </summary>	
		public string Password
		{
			get
			{
				return thePassword;
			}
			set
			{
				thePassword = value;
			}
		}

		/// <summary>
		/// Property NoCheck. Defines if the validation of Username and Password is handled by the Host Application (True) or LoginControl (False).
		/// </summary>
		public bool Check
		{
			get
			{
				return bCheck;
			}
			set
			{
				bCheck = value;
			}
		}

		/// <summary>
		/// Property NoHash. Defines if the user data is hashed (False).
		/// </summary>
		public bool Hash
		{
			get
			{
				return bHash;
			}
			set
			{
				bHash= value;
			}
		}

		/// <summary> 
		/// If NoCheck is set to false this method checks Username and Password.
		/// </summary>
		private bool CheckCredentials(string usr, string pwd)
		{
			if (!bHash)
			{
				if (tbUsername.Text == usr)
				{
					if (tbPassword.Text == pwd)
					{
						return true;
					}
					else
					{
						errorProvider.SetError(bLogin, ErrMsg);
						return false;
					}
				}
				else
				{
					errorProvider.SetError(bLogin, ErrMsg);
					return false;
				}
			}
			else
			{
				if (HashString(tbUsername.Text) == HashString(usr))
				{
					if (HashString(tbPassword.Text) == HashString(pwd))
					{
						return true;
					}
					else
					{
						errorProvider.SetError(bLogin, ErrMsg);
						return false;
					}
				}
				else
				{
					errorProvider.SetError(bLogin, ErrMsg);
					return false;
				}
			}
		}
		
		/// <summary>
		/// If NoHash is set to false HashPassword ... well ... hashes the password.
		/// </summary>
		private string HashString(string s)
		{
			UTF8Encoding UTF8 = new UTF8Encoding();
			byte[] data = UTF8.GetBytes(s);
			MD5 md5 = new MD5CryptoServiceProvider();
			byte[] result = md5.ComputeHash(data);
			return System.Text.Encoding.UTF8.GetString(result);
		}

		/// <summary>
		/// Error message for the ErrorProvider.
		/// </summary>
		public string ErrorMessage
		{
			get
			{
				return ErrMsg;
			}
			set
			{
				ErrMsg = value;
			}
		}

	
	

	}
}

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
Web Developer
Colombia Colombia
Web Developer during 3 years and counting, i worked over my school website by two years, right now im webmaster at World Wowan foundation banking in my home city.

Please forgive my pictures examples, they are at spanish language because its my mother language.


Comments and Discussions