Hi,
I want generate random password. it should contain at least 1 upper case letter, 1 digit, 6 character length.
I can use Membership.GeneratePassword(-, -), but its not having numbers some times.
Any help please?
Thank you.
1 5970
Hi Ramanak,
Here's a quick algorithm to do want you want to do. Sorry it's not the cleanest but it works. Didn't have plenty of time to test it though.
You can change the minimum and maximum number of chars. You can also add other chars (symbols, etc) if you want. You can also make it cleaner, but don't create the Random object in the function or it will always generate the same password.
Again, sorry it's not the cleanest but I think you get the idea.
Have a good day.
Tomy -
protected String RandomPassword()
-
{
-
String pwd = "";
-
int length = 0;
-
int index = 0;
-
int numericIndex = -1;
-
int upperCaseIndex = -1;
-
-
//Length of your password
-
length = rnd.Next(MINLENGTH, MAXLENGTH);
-
-
// You generate a password of the desired length
-
for (int i = 0; i < length; i++)
-
{
-
// Generate an index that smaller than the size of your allowed chars
-
index = rnd.Next(0, allowedChars.Length);
-
-
pwd += allowedChars[index];
-
}
-
-
////*********************************************************
-
// We make sure that there is at least one numeric
-
// Replace one random char by a numeric
-
numericIndex = rnd.Next(0, pwd.Length);
-
-
// Generate a numeric, delete one char and replace it with a numeric
-
index = rnd.Next(0, numericChars.Length);
-
pwd = pwd.Remove(numericIndex, 1);
-
pwd = pwd.Insert(numericIndex, numericChars[index].ToString());
-
////*********************************************************
-
-
-
////*********************************************************
-
// We make sure that there is at least one uppercase
-
// Replace one random char by a numeric
-
upperCaseIndex = rnd.Next(0, pwd.Length);
-
-
// We make sure our uppercase index is different
-
// from our numeric index or we will overwrite our
-
// only numeric value with our uppercase value
-
while (upperCaseIndex == numericIndex)
-
{
-
upperCaseIndex = rnd.Next(0, pwd.Length);
-
}
-
-
// Generate a numeric, delete one char and replace it with a numeric
-
index = rnd.Next(0, upperCaseChars.Length);
-
pwd = pwd.Remove(upperCaseIndex, 1);
-
pwd = pwd.Insert(upperCaseIndex, upperCaseChars[index].ToString());
-
////*********************************************************
-
-
return pwd;
-
}
-
-
Random rnd = new Random();
-
const int MINLENGTH = 6;
-
const int MAXLENGTH = 20;
-
const String allowedChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
-
const String upperCaseChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
-
const String numericChars = "0123456789";
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Adams-Blake Co. |
last post by:
I want to allow the user to enter her own username and password.
I want to validate the password the way lots of programs do.... that it has
to be:
- 6 or more characters.
- must be at...
|
by: avillela |
last post by:
I'm looking for a password generator.
example: I pass as parameter a string and the function return a
password.(string)
can anyone provide me any password generator ????
thanks
André -...
|
by: learn.2005 |
last post by:
Hello Friends!
I am Manu Gupta, an IT Engineering student, learning PHP presently. I
want to match user name and pwd from MySQL, it shows case
insensitiveness. Can anybody help me in implementing...
|
by: bigd7788 |
last post by:
Ladies and gents,
Need a little help with the following:
Write a program that will accept a string from the user and evaluate it as a password, giving it a passing or a failing grade. A good...
|
by: Chris |
last post by:
I want to ensure a password has a least one lower case letter, one upper
letter and a number. I'm a bit of a newbie but I understand you can use the
pipe system for OR what about AND.I would like...
|
by: Army1987 |
last post by:
Is there anything wrong with this program? It seems to behave
strangely if I give stdin EOF when asked for the character set...
/* BEGIN pwdgen.c */
#include <stdio.h>
#include "random.h"...
|
by: DarthPeePee |
last post by:
Hello everyone.
I am working on a Password Strength Meter and I am running into 1 problem that I would like to fix.
When pressing the "Clear Password & Try Again" button, the password clears...
|
by: ashurack |
last post by:
I found a stored procedure online a while back and want to inplement it. The only problem is that it doesn't check to see if the number generated is currently in use in the DB.
I know it's really...
|
by: ccarter45 |
last post by:
I'm new to java and writing a program that accepts user input for a password and it has to meet the following requirements:
1. At least 6 characters long.
2. Leading character can't be a digit....
|
by: Jonathan Boivin |
last post by:
If I could give an advice on your method, in fact, what I would do.. is :
Generate a password with your lower security set of characters. Then upper
case randomizally some letters and finally...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| | |