Generating Random Password Code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Krimp
    New Member
    • Aug 2008
    • 1

    Generating Random Password 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?



    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="Button 1_Click" />
    </div>
    </form> The simple method is shown below. This is how the code-behind should look:
    Code:
    Imports Microsoft.Visua lBasic
    Imports System
    Imports System.Data
    Imports System.Configur ation
    Imports System.Web
    Imports System.Web.Secu rity
    Imports System.Web.UI
    Imports System.Web.UI.W ebControls
    Imports System.Web.UI.W ebControls.WebP arts
    Imports System.Web.UI.H tmlControls

    Partial Public Class _Default Inherits System.Web.UI.P age
    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 CreateRandomPas sword(ByVal PasswordLength As Integer) As String Dim _allowedChars As String = "abcdefghijkmno pqrstuvwxyzABCD EFGHJKLMNOPQRST UVWXYZ012345678 9"
    Dim randNum As New Random()
    Dim chars(PasswordL ength - 1) As Char
    Dim allowedCharCoun t As Integer = _allowedChars.L ength

    For i As Integer = 0 To PasswordLength - 1 chars(i) = _allowedChars.C hars(CInt(Fix(( _allowedChars.L ength) * randNum.NextDou ble())))
    Next i

    Return New String(chars)
    End Function

    Protected Sub Button1_Click(B yVal sender As Object, ByVal e As EventArgs) If TextBox1.Text <> "" Then Dim myInt As String = TextBox1.Text.T oString()
    Label1.Text = "Your generated password is: " & CreateRandomPas sword(Integer.P arse(myInt))
    End If
    End Sub
    End Class
  • YarrOfDoom
    Recognized Expert Top Contributor
    • Aug 2007
    • 1243

    #2
    Welcome to bytes.com Krimp.

    Wouldn't reclicking the button generate a new password, it seems more handy to do that, than to refresh the page.

    As for the rest, I don't think this is a VB question, and please use [CODE]-tags in your next questions. You might want to read the posting guidelines.

    Comment

    Working...