-
Password Generation Code
I pulled this code from Vbasic.net or 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?
vbasic.net/detail.aspx?tid=105
[VBA] <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:
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[/VBA]
-
Good Afternoon.
If you'll notice, there are several parts to this page, but the entire process is contained within this one script, which is quite nice.
The first part is the Protected Sub Page_Load. This procedure will run every time the page loads. Based on the code written, it will set the Label to give the order to enter the password.
The second part is the Public Shared Function CreateRandomPassword. This is the function which actually generates the password. Because this is a public function, it can be accessed from anywhere in your code.
The third part is Protected Sub Button1_Click. This is the specific part of the page that begins the process of generating the password. This is what makes the page require you to manually enter the length of the password.
The solution to your answer is to determine a standard length for your random password and move the command to generate it and change Label1 to the new Password. You will want to move it into your Page_Load procedure.
[vba]
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server"></asp:Label><br />
</form>
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)
Label1.Text = "Your generated password is: " & CreateRandomPassword(Integer.Parse(myInt))
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
End Class[/vba]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules