It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming Visual Basic Forum

Generating Random Passwords

Moderator: dafunkymunky

Generating Random Passwords

Postby Adamu on Thu Aug 14, 2008 7:01 pm

I pulled this code from http://www.vbasic.net for generating random passwords. I want to know how I can set up so that each time the page is refreshed a new password is generated without having to fill in the form boxes. Anyone lend a hand?

http://vbasic.net/detail.aspx?tid=105

Code:
<form id="form1" runat="server">
<div> 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" />
</div>
</form> The simple method is shown below. This is how the code-behind should look:
Code:
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 = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ 0123456789"
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
Adamu
 
Posts: 0
Joined: Thu Aug 14, 2008 6:57 pm

Who is online

Users browsing this forum: No registered users and 4 guests