Click here to Skip to main content
15,896,154 members
Articles / Programming Languages / C#

Password Strength Control

Rate me:
Please Sign up or sign in to vote.
4.57/5 (31 votes)
21 Jul 2008CPOL5 min read 70.4K   3.5K   91  
This article is about a Password Strength control, which can be used in password dialogs and login windows in Windows based .NET applications. If you want, you can use it as a progress bar control as well. This control can be shaped to different styles and can be extended, if needed.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PasswordStrengthControlLib;

namespace PasswordStrengthDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            textBox2_TextChanged(null, null);
            propertyGrid1.SelectedObject = passwordStrengthControl12;
            propertyGrid1.PropertyValueChanged += new PropertyValueChangedEventHandler(propertyGrid1_PropertyValueChanged);
        }

        void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            passwordStrengthControl12 = (PasswordStrengthControl)propertyGrid1.SelectedObject;
            passwordStrengthControl12.Invalidate();
        }


        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            int x = PasswordStregthManager.Instance.GetPasswordScore(textBox1.Text, textBox2.Text);
            string str = PasswordStregthManager.Instance.GetPasswordStrength(textBox1.Text, textBox2.Text);
            Color col = PasswordStregthManager.Instance.GetPasswordColor(textBox1.Text, textBox2.Text);

            this.SetPasswordControls(this.passwordStrengthControl1 , x,str, Color.Black ,col);
            this.SetPasswordControls(this.passwordStrengthControl2, x, str, Color.Black, col);
            this.SetPasswordControls(this.passwordStrengthControl3, x, str, Color.Black, col);
            this.SetPasswordControls(this.passwordStrengthControl4, x, str, Color.Black, col);
            this.SetPasswordControls(this.passwordStrengthControl5, x, str, Color.Black, col);
            this.SetPasswordControls(this.passwordStrengthControl6, x, str, Color.Black, col);
            this.SetPasswordControls(this.passwordStrengthControl7, x, str, Color.Black, col);
            this.SetPasswordControls(this.passwordStrengthControl8, x, str, Color.Black, col);
            this.SetPasswordControls(this.passwordStrengthControl9, x, str, Color.Black, col);
            this.SetPasswordControls(this.passwordStrengthControl10, x, str, Color.Black, col);
            this.SetPasswordControls(this.passwordStrengthControl11, x, str, Color.Black, col);
            this.SetPasswordControls(this.passwordStrengthControl2, x, str, this.passwordStrengthControl2.ForeColor, this.passwordStrengthControl2.SolidColor);
    

        }

        private void SetPasswordControls(PasswordStrengthControl cont , int s , string text , Color f , Color b)
        {
            cont.Strength = s;
            cont.SolidColor = b;
            cont.ForeColor = f;
            cont.StrengthText = text;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox2.Focus();

        }

        private void passwordStrengthControl1_StrengtChangedEventHandler(object sender, StrengthChangedEventArgs e)
        {
            label1.Text = e.Text + " ( " + e.Level + " / {" + passwordStrengthControl1.MaxStrength + "})";
        }
    }
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Sri Lanka Sri Lanka
PhD, BSc(Eng), MCP (Web Development)

Comments and Discussions