It is very simple to develop your own password generator module. At first,
you need to create your own class, and inherit it from the base class
PasswordGenerator. Than you need to create a constructor, that
expects a parameter of type PasswordOptions. This constructor
needs to call the constructor with the same signature of the base class.
Finally you need to implement the method Generate. This method
is responsible for the generation of the password. How this method generates
the password is up to you.
In the following example an ArrayList is created in the constructor of the
CustomPasswordGenerator class and is filled with characters
dependent on the password options and is assigned to the private field
passwordChars. Finally a RandomNumberGenerator is created in the
constructor with the RandomNumberGeneratorFactory and is assigned to the
private field random.
The method Generate reads a character with the
RandomNumberGenerator from the array passwordChars as often as
specified by the parameter length and puts the characters
together. After this, the password is returned as a string.
using System;
using System.Collections;
using CAM.PasswordGeneratorLibrary;
namespace CustomPasswordGeneratorSample
{
public class CustomPasswordGenerator : PasswordGenerator
{
public CustomPasswordGenerator(PasswordOptions passwordOptions) :
base(passwordOptions)
{
if(PasswordOptions == PasswordOptions.NoCharacters)
throw new ArgumentException("No password option set","passwordOptions");
ArrayList passwordCharsTemp = new ArrayList();
if((PasswordOptions & PasswordOptions.UppercaseCharacters) != 0x00)
passwordCharsTemp.AddRange(new char[] {'A', 'B', 'C', 'D'});
if((PasswordOptions & PasswordOptions.LowercaseCharacters) != 0x00)
passwordCharsTemp.AddRange(new char[] {'a', 'b', 'c', 'd'});
if((PasswordOptions & PasswordOptions.Numbers) != 0x00)
passwordCharsTemp.AddRange(new char[] {'1', '2', '3', '4'});
passwordChars = (char[])passwordCharsTemp.ToArray(typeof(char));
random = RandomNumberGeneratorFactory.Instance.Create();
}
private RandomNumberGenerator random;
char[] passwordChars;
public override string Generate(int length)
{
char[] password = new char[length];
for(int i = 0; i < length; i++)
{
password[i] = passwordChars[random.GetRandomNumber(passwordChars.Length)];
}
return new string(password);
}
}
}To use this password generator with the Password Generator Library, you need to register it in the *.config file. This is done in the "PasswordGenerators" element. Here you need to add an element with the name "add". With the attribute "key" you need to specify the name of the password generator and with the attribute "value" you need to specify the name of the class and the assembly in which the class is contained.
<CAM.PasswordGeneratorLibrary>
<PasswordGenerators>
...
<add key="CustomPasswordGenerator"
value="CustomPasswordGeneratorSample.CustomPasswordGenerator,
CustomPasswordGeneratorSample" />
</PasswordGenerators>
</CAM.PasswordGeneratorLibrary>Now you can use the new password generator with the name
"CustomPasswordGenerator". You can either activate it with the
"PasswordGeneratorDefaults" element in the *.config file, or by changing the
property PasswordGeneratorFactory.Instance.PasswordGenerator or
you create an instance of the CustomPasswordGenerator class
directly.