Click here to Skip to main content
Email Password   helpLost your password?

Introduction

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.

Background

Custom control design is one of the fascinating features given by the .NET framework to control authors. Using inheritance, we can extend the behavior or the look and feel of preexisting Windows Forms controls. In addition, because of the flexibility given to authors, they can give designers good design time experience using property grids and other means. There are lots of controls with similar features in the web using the power of JavaScript and AJAX, but I was not able to find such a control for Windows based applications. Because of this, I decided to create this control, which I believe has very good value.

MainImage.jpg

Password Strength Control Features

You can use this control mainly in Change Password windows and in Login dialog boxes to show the strength of the input password. Using the security policy of the company, given a password, you can decide a mark for the strength of that password. In my demo application, I have used a one password strength algorithm to show the power of this control. In addition, you can divide the entire strength range in to several segments and name those as you wish. In my demo application, I have used ranges like Short, Weak, Good, and Strong.

What are the styles available in this control? Using the ControlStyle property, you can change the styles.

Style

Description

Solid

Strength indicator bar is drawn with a solid (single) color.

Gradient

Strength indicator bar is drawn with two colors having a horizontal gradient. (GradientStartColor, GradientEndColor)

Wire

Strength indicator bar is drawn with two colors having a vertical gradient. (GradientStartColor, GradientEndColor)

Imaged

Strength indicator bar is drawn using an input image. (ForeGroundImage)

Using the same styles, you can change the control behavior and look and feel to a segmented mode using the EnableSegment property. Furthermore, you have to set the properties like SegmentGap and SegmentWidth to customize this mode.

To change the alignment of the text, you can use the TextAlignment property which has options like Left, Right, Center, and None. You can also change the border color and background images.

Control Implementation

The Password Strength control is an extension of the System.Windows.Forms.Control class. Even though this has a similar look and feel as the ProgressBar control, I avoided extending it because the behavior and some of the visual effects had to be developed from scratch. In fact, the usage of this control is completely different from that of the ProgressBar control.

The main implementation is in the Draw method. When the control is refreshed or repainted, this method will be called. There, I have divided the implementation into three methods. They are DrawBorder, DrawBackgorund, and DrawText. Out of these methods, I am going to discuss only the most important method.

In the DrawBackgorund method, when the control is in the segmented mode, I draw a region with segments, or in other words, like a scale.

Region region = new Region();
Rectangle segmentRect = new Rectangle(InnerRectangle.Left, InnerRectangle.Top, 
                                      swidth, InnerRectangle.Height); 
for (int i = 0; i < segmentNumber; i++) { 
  region.Union(segmentRect); 
  segmentRect.Offset(swidth + gap, 0); 
}

I used this code snippet to create a segmented region just like a scale. Then, using the FillRegion method in the Graphics class, I filled the segmented region using different brushes.

Other important implantations are the DrawWire and DrawGradient methods used in this control. For this, different brushes like TextureBrush, LinearGradientBrush, and SolidBrush provided by the System.Drawing namespace were really useful.

LinearGradientBrush gradientBrush = new LinearGradientBrush( 
 new Point(rc.X, rc.Y), 
 new Point(rc.X + rc.Width, rc.Y), 
 this.gradientStratColor, 
 this.gradientEndColor);

I used the LinearGradientBrush to create the gradient when the control is in the Gradient style. Here, when strength increases, the gradient increases as well. Use this brush for FillRectangle or FillRegion to paint the surface accordingly.

In the DrawWire method, I used another GradientBrush with the color blend option to enable multi-color gradient in the vertical direction.

LinearGradientBrush gradientBrush = new LinearGradientBrush( 
  new Point(rc.X, rc.Y), 
  new Point(rc.X, rc.Y + rc.Height), 
  this.gradientStratColor, 
  this.gradientEndColor); 
ColorBlend colorBlend = new ColorBlend(3); 
colorBlend.Colors = new Color[] { this.gradientEndColor, 
           this.gradientStratColor, this.gradientEndColor }; 
colorBlend.Positions = new float[] { 0, (float)0.4, 1 }; 
gradientBrush.InterpolationColors = colorBlend;

When designing a control, we have to consider the properties we are going to expose to the propertygrid and other external entities. Using the attributes given by the System.ComponentModel namespace, we have the flexibility to selectively expose the things we want. For instance, the Browsable (bool) attribute can be used to avoid a specific property being displayed in the property window. In the following table, you can find the meaning of the attributes I have used:

Browsable(bool)

Indicates whether a property or an event should be displayed in the Properties window.

Category(name)

Provides the name of the category to which a property or an event belongs to. This allows for logical grouping of properties and events.

DefaultValue(value)

Used to provide a default value for a control property. If the property doesn't have a simple type, a type converter is needed.

Description(text)

Provides a description of a property or an event. The text is displayed at the bottom of the Properties window when the user selects a property or event.

Apart from these, I have used:

[ToolboxItem(true)]
[ToolboxBitmap(typeof(ProgressBar))]
[Description ("Password Strength Control with different styles.")]

Attributes which will be used by the Toolbox when you add this to the VS2005 Toolbox:

ToolboxItem(bool)

Provides a base implementation of a toolbox item.

ToolboxBitmap

Allows you to specify an icon to represent a control in a container, such as the Microsoft Visual Studio Form Designer.

I created a new event called StrengthChangedEventHandler to notify the users that the strength value or the text of the control has changed. This event provides a StrengthChangedEventArgs type of EventArgs with the respective strength value and text.

If you want to extend this control, you can do that by easily overriding the existing method implementations in any way suitable.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralTharindu
Paul Talbot
0:28 14 Sep '09  
Hello Tharindu, this could be improved by bringing the password check functionality into the component itself, rather than relying on the user of the component having to add the code from Form1.

Create another property called 'BindPasswordBox' set as a TextBox. You can then put the functionality into the component and you have a self contained piece of software that you just drop onto the form and fill in the properties.


TextBox bindPasswordTextBox = null;

[Description ("Bind password text box to control")]
public TextBox BindPasswordBox
{
get { return bindPasswordBox; }
set
{
bindPasswordBox = value;

if (bindPasswordBox != null)
bindPasswordBox.TextChanged += new EventHandler (txtPassword_TextChanged);
}
}

void txtPassword_TextChanged (object sender, EventArgs e)
{
//Add the password check functionality here from the form
}


You could also extend it by having a callback function in the form that would set the OK button active when the password crossed the threshold of a variable in the properties that the user could set.

Cheers

Paul
GeneralFeature suggestion
stensones
23:34 28 Jul '08  
How about extending the control to set many of the appearance values (colours, images etc) in a collection keyed by password strength? This would allow you to define the look (i.e. colour, text, image etc) in steps associated with the password strength.

Since most of us classify password strength in terms of "poor, medium and good" or similar and these (in a UK/US culture at least) would typically be graphically represented as red, yellow and green colours this would enable a the control to render that nicely.
GeneralRe: Feature suggestion
Tharindu Nishad Patikirikorala
23:51 28 Jul '08  
Thanks for the suggestion.

In my demo application I have used one of password strength algorithms. According to security policies, there can be number of different algorithms and ranges. So in my control I tried to avoid this complexity. Where user decides the ranges and colors for those. When that range reaches user can change color accordingly. That’s exactly what I have done in my demo application.
If you want to extend this control to a hardbound algorithm you can do it by easily extending or editing the code.
RantPlease fix typos.
psouza4micronet
10:26 22 Jul '08  
Here they are...

   Title of this article:
      Password Strengh Control

   Background (screen shot):
      Chnage Properties below
      Sloid Style

   Background:
      PasswordStrengthContrl Features
      from the starch
         (did you mean "from scratch"?)

(c'mon.. the title, really?)
GeneralRe: Please fix typos.
ChPo1234
12:31 22 Jul '08  
Given that the author is from Sri Lanka, I would like to complement him on the standard of his spelling/typing considering that English is presumably not his native language. In no way do the few errors detract from a useful article.

Your website on the other hand (http://www.petersouza.com/About), written in I assume your native language, has the following words with incorrect spelling:

untolerable
couldnt
habbit
Itallian

Please fix your spelling mistakes before you criticise and demand correction from others!

Smile
GeneralRe: Please fix typos.
RUrry
21:01 28 Jul '08  
LOL! and thats how you put out a flame Smile
GeneralRe: Please fix typos.
Jomar Pabuaya
18:34 30 Mar '09  
You are correct!

This guy always criticizes the work of other people. He always gives a bad comment rather than suggesting and motivating people. Look at the messages he posted.

He don’t even had one article posted. Visit his website again at http://www.petersouza.com. Look at the design. :(


Last Updated 22 Jul 2008 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010