devxlogo

GetRandomPassword – Generating a random password with the specified length

GetRandomPassword – Generating a random password with the specified length

' Generate a random password with the specified length. The password will only ' contain digits and letters (either lowercase or uppercase)'' Example: generating a password of 8 chars'    Dim password As String = GetRandomPassword(8)Function GetRandomPassword(ByVal length As Integer) As String    Static rand As New Random    Dim password As New System.Text.StringBuilder(length)    For i As Integer = 1 To length        Dim charIndex As Integer        ' allow only digits and letters        Do            charIndex = rand.Next(48, 123)        Loop Until (charIndex >= 48 AndAlso charIndex = 65 AndAlso charIndex = 97 AndAlso _            charIndex ' add the random char to the password being built        password.Append(Convert.ToChar(charIndex))    Next    Return password.ToString()End Function

devx-admin

Share the Post: