Solved

random password

Posted on 2008-10-17
14
716 Views
Last Modified: 2012-05-05
hi,
i am using this code to generate a random number:
Session["Id"] = new Random().Next(1, 1000);
how can i also include uppercase and or lowercase characters to make it like a random pasword. but it must be unique.

or is there another way to generate a random but unique password and indicate how long it can be in asp.net and classic asp?

0
Comment
Question by:eaweb
  • 3
  • 2
  • 2
  • +5
14 Comments
 
LVL 5

Expert Comment

by:Goofytouy
ID: 22743864

Hi eaweb

Can't post the code here (i should think it), but you can use:

chr(Random().Next(65,90)) to obtain uppercase random letters
chr(Random().Next(97,122)) to obtain lowercase random letters.
Also, if you use range 65 to 122 you can include some symbols ([\]^_`)
By lowering 65 to 58, you may obtain too: :;<=>?@



0
 
LVL 5

Expert Comment

by:Goofytouy
ID: 22743886

Oopss.. it leave me before I've finished.

You should stablish which it's going to be the length of the random password, and iterate through this functions.
You also may do a Select Case from a random 1 to 3, and according to that random, call one of the three options (numbers, lower or uppercase)


Sorry for only giving you the ideas, but I do not have reachable my working framework.
Regards
Goofy
0
 
LVL 23

Expert Comment

by:Kamaraj Subramanian
ID: 22743897
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 9

Expert Comment

by:deathtospam
ID: 22744130
What itkamaraj said -- generating a GUID (globally unique ID) is a pretty good way to go.

http://articles.techrepublic.com.com/5100-10878_11-5708732.html
http://aspnet.4guysfromrolla.com/articles/101205-1.aspx
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 22744158
you can build your own function:

public string CreateRandomPassword(int size)
{
      Random rand = new Random();

      string pass = "";
      for (int i=0; i<size;)
      {
             char val = (char)rand.Next(48,127);
             if (char.IsLetterOrDigit(val))
             {
                  pass += val; i++;
             }
      }
      return pass;
}

So you can use as:

Session["Id"] = CreateRandomPassword(10);
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22744423
The author stated:

    "...a random but unique password..."

As the posts have shown thus far, there are MANY ways to generate a random string of characters that can be used as a password.

What has not been addressed is the "UNIQUE" aspect in the requirements.

Unique with respect to what?  Do you have a set of all previously generated passwords that you need to check against to prevent duplicates?

Give us more details...
0
 

Author Comment

by:eaweb
ID: 22744806
oh sorry,
i need the generation to be based on globally unique ID.

i need a way to do it in asp.net vb and classic asp vb
0
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22745006
use Guid.NewGuid()
this will give a globally unique id every time
this class is a part of dotnet framework
0
 

Author Comment

by:eaweb
ID: 22745052
how can i convert the following .net code into classic asp code?

Public Function GetRandomPasswordUsingGUID(ByVal length as Integer) as String  
  'Get the GUID
  Dim guidResult as String = System.Guid.NewGuid().ToString()
 
  'Remove the hyphens
  guidResult = guidResult.Replace("-", String.Empty)
 
  'Make sure length is valid
  If length <= 0 OrElse length > guidResult.Length Then
    Throw New ArgumentException("Length must be between 1 and " & guidResult.Length)
  End If
 
  'Return the first length bytes
  Return guidResult.Substring(0, length)
End Function
0
 
LVL 45

Expert Comment

by:aikimark
ID: 22746249
In order to ensure uniqueness, you will have to use all 32 characters of the GUID.  If your password is less than the GUID length, you will need to combine the GUID bytes in a way that ensures uniqueness.

Otherwise, you will need to retain/persist the passwords you've generated and do a look-up to ensure uniqueness before issuing a new password.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22747075
Right...there isn't any guarantee at all that GUIDs are actually unique.  They don't have any notion of what has been generated before...

The "uniqueness" is simply based on the fact that they are so long that there are literally millions of combinations that can be produced and it is unlikely to generate the same sequence in any short period.

Directly from the documentation:
http://msdn.microsoft.com/en-us/library/system.guid.newguid.aspx

    "There is a very low probability that the value of the new Guid is all zeroes or equal to any other Guid."

By shortening the length of the value you are decreasing the number of combinations to pick from and increasing the likelihood of a repeat.  The actual probability depends on how often you generate values, how long you need a generated value to remain "unique", and whether you are disposing of the values afterwards or need to persist them so you know they have been used before.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 22747681
@eaweb

1. What is the big picture for this question?  Why would passwords need to be guaranteed/absolutely unique versus probably unique?

2. How long are these passwords?

3. What kind of character set will comprise these passwords?

4. Are these passwords for use by humans or machines?  (memorability might be important)
0
 
LVL 45

Expert Comment

by:aikimark
ID: 23000873
PAQ with no refund
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question