We pulled this fromVbasic.net. A friend of mine has it running on his home server but I keep getting errors on our production mahcine please help!!!! Thanks!!!

Generating random passwords can increase the security of a website by taking the process out of the hands of the user, or simply providing an alternative, and thus reducing the chance of easily-guessable passwords being used. This tutorial shows a simple method of creating a random password.
First, we create the ASPX page:

<form id="form1" runat="server">
Enter Required Password Length:
<asp:TextBox ID="TextBox1" runat="server" Columns="2" MaxLength="2"></asp:TextBox><br />
<asp:Label ID="Label1" runat="server"></asp:Label><br />
<asp:Button ID="Button1" runat="server" Text="Generate Password" OnClick="Button1_Click" />

The simple method is shown below. This is how the code-behind should look:

Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls

Partial Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If IsPostBack Then
Label1.Text = "Please enter a password length (e.g. 8)"
End If
End Sub

Public Shared Function CreateRandomPassword(ByVal PasswordLength As Integer) As String
Dim _allowedChars As String = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789"
Dim randNum As New Random()
Dim chars(PasswordLength - 1) As Char
Dim allowedCharCount As Integer = _allowedChars.Length

For i As Integer = 0 To PasswordLength - 1
chars(i) = _allowedChars.Chars(CInt(Fix((_allowedChars.Length) * randNum.NextDouble())))
Next i

Return New String(chars)
End Function

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
If TextBox1.Text <> "" Then
Dim myInt As String = TextBox1.Text.ToString()
Label1.Text = "Your generated password is: " & CreateRandomPassword(Integer.Parse(myInt))
End If
End Sub
End Class

Edited by Dani: Formatting fixed

8 Years
Discussion Span
Last Post by tuse

Here is a much simpler code to generate random passwords. The code is in VB.NET

Public Class Form1

    Public Function GenerateRandomPassword(ByVal length As Integer) As String
        Dim strGuid As String = System.Guid.NewGuid().ToString()
        strGuid = strGuid.Replace("-", String.Empty)
        Return strGuid.Substring(0, length)
    End Function

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim s As String
        s = GenerateRandomPassword(8) 'Pass length as arguement
        Me.TextBox1.Text = s
    End Sub
End Class
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.