When a user on our site looses his password and heads off to the Lost Password page we need to give him a new temporary password. I don't really mind how random this is, or if it matches all the "needed" strong password rules, all I want to do is give them a password that they can change later.
The application is a Web application written in C#. so I was thinking of being mean and going for the easy route of using part of a Guid. i.e.
Guid.NewGuid().ToString("d").Substring(1,8)
Suggesstions? thoughts?
I know that this is an old thread, but I have what might be a fairly simple solution for someone to use. Easy to implement, easy to understand, and easy to validate.
Consider the following requirement:
The following regular expression can validate this case:
It's outside the scope of this question - but the regex is based on lookahead/lookbehind and lookaround.
The following code will create a random set of characters which match this requirement:
To meet the above requirement, simply call the following:
The code starts with an invalid character (
"!"
) - so that the string has a length into which new characters can be injected.It then loops from 1 to the # of lowercase characters required, and on each iteration, grabs a random item from the lowercase list, and injects it at a random location in the string.
It then repeats the loop for uppercase letters and for numerics.
This gives you back strings of length =
lowercase + uppercase + numerics
into which lowercase, uppercase and numeric characters of the count you want have been placed in a random order.I created this method similar to the available in the membership provider. This is usefull if you don't want to add the web reference in some applications.
It works great.
Here's how I generate random tokens:
It's been noted that as this returns a base-64 string, the output length is always a multiple of 4, with the extra space using
=
as a padding character. Thelength
parameter specifies the length of the byte buffer, not the output string (and is therefore perhaps not the best name for that parameter, now I think about it). This controls how many bytes of entropy the password will have. However, because base-64 uses a 4-character block to encode each 3 bytes of input, if you ask for a length that's not a multiple of 3, there will be some extra "space", and it'll use=
to fill the extra.If you don't like using base-64 strings for any reason, you can replace the
Convert.ToBase64String()
call with either a conversion to regular string, or with any of theEncoding
methods; eg.Encoding.UTF8.GetString(tokenBuffer)
- just make sure you pick a character set that can represent the full range of values coming out of the RNG, and that produces characters that are compatible with wherever you're sending or storing this. Using Unicode, for example, tends to give a lot of Chinese characters. Using base-64 guarantees a widely-compatible set of characters, and the characteristics of such a string shouldn't make it any less secure as long as you use a decent hashing algorithm.I've always been very happy with the password generator built-in to KeePass. Since KeePass is a .Net program, and open source, I decided to dig around the code a bit. I ended up just referncing KeePass.exe, the copy provided in the standard application install, as a reference in my project and writing the code below. You can see how flexible it is thanks to KeePass. You can specify length, which characters to include/not include, etc...
On my website I use this method:
Edit string
_SymbolsAll
for your array list.Here Is what i put together quickly.