Generate Random Password in the VB.NET

In this article, We will see how to generate the random password in the VB.NET.
  • 2414

In this article, We will see how to generate the random password in the VB.NET. Random password show the run time.

This code is aspx:-

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
    <title>Generate Random Password</title>
    <form id="form1" runat="server">
<table width="400px">
            <tr align="center">
                        <legend style="border: none; font-weight: bold; color: Red; font-size: 15pt; font-family: Segoe UI">
                            Random Password&nbsp;</legend>
                        <asp:Label ID="Label1" runat="server" ForeColor="Blue" Font-Bold="true" Text="Enter Password Length:"></asp:Label>
                        <asp:TextBox ID="TextBoxPasswordLength" runat="server" Width="100" MaxLength="2"></asp:TextBox><br />
                        <asp:Label ID="LabelShowPwd" runat="server" ForeColor="Blue" Font-Bold="true"></asp:Label><br />
                       <asp:Button ID="ButtonOk" runat="server" Text=" Ok " OnClick="ButtonOk_Click" />

This code is .vb:-

Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        If IsPostBack Then
            LabelShowPwd.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 randomNumber 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) * randomNumber.NextDouble())))
        Next i
        Return New String(chars)
    End Function
    Protected Sub ButtonOk_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonOk.Click
        If TextBoxPasswordLength.Text <> "" Then
            Dim password As String = TextBoxPasswordLength.Text.ToString()
            LabelShowPwd.Text = "Your generated password is: " & CreateRandomPassword(Integer.Parse(password))        
        End If
    End Sub
End Class





More Articles

© 2020 DotNetHeaven. All rights reserved.