SoFunction
Updated on 2025-03-06

Example of a simple method for generating random passwords in C#

This article describes the method of simply generating random passwords in C#. Share it for your reference, as follows:

using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
public partial class _Default : 
{
  public static int counter = 0;
  protected void Page_Load(object sender, EventArgs e)
  {
  }
  public static string MakePassword(string pwdchars, int pwdlen)
  {
    //Judge the length of the random password    string tmpstr = "";
    int iRandNum;
    if (pwdlen > )
    {
      return "The length is too long!" + ;
    }
    Random rnd = new Random();
    for (int i = 0; i < pwdlen; i++)
    {
      iRandNum = ();
      tmpstr += pwdchars[iRandNum];
    }
    return tmpstr;
  }
  protected void Button1_Click(object sender, EventArgs e)
  {
    int t1 = Convert.ToInt32();
    if (t1 < 10 || t1 > 20 || t1 < 0)
    {
      ("The specified password failed!");
    }//Discreate whether the specified password length meets the    else
    {
      string randomchars = "abcdefghijklmnopqrstuvwxyz~!@#$%^&*()_+|0123456789~!@#$%^&*()_+|ABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_+|";
      string password = MakePassword(randomchars, Convert.ToInt32());
       = password;
    }// Generate random password value  }//Determine whether the entered password meets the randomly generated password  protected void Button2_Click(object sender, EventArgs e)
  {
    if (()==())
    {
      ("success!");
      ("Your password is:"+"<font color=red>"++"</font>");
    }
    else
    {
      ("fail!");
    }
  }
  protected void TextBox1_TextChanged(object sender, EventArgs e)
  {
  }
}

HTML code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:///TR/xhtml1/DTD/">
<html xmlns="http:///1999/xhtml" >
<head  runat="server">
  <title>Untitled page</title>
</head>
<body bgcolor="#66ffcc">
  <form  runat="server" >
  <div>
         <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <table bgcolor=SpringGreen>
      <tr>
        <td width="100px">
    <asp:Label ID="Label1" runat="server" Text="Specify password length(10~20Between)" Width="129px"></asp:Label></td>
        <td width="159px">
    <asp:TextBox ID="TextBox1" runat="server" Width="39px" OnTextChanged="TextBox1_TextChanged">10</asp:TextBox>
    <asp:TextBox ID="TextBox2" runat="server" Width="195px" ReadOnly="true"></asp:TextBox></td>
        <td width="103px">
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Create password" /></td>
        <td width="100px">
        </td>
      </tr>
      <tr>
        <td width="100px">
    <asp:Label ID="Label2" runat="server" Text="Enter the generated password"></asp:Label></td>
        <td width="159px">
          <asp:TextBox ID="TextBox3" runat="server" Width="261px" TextMode="Password"></asp:TextBox></td>
        <td width="103px">
    <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="verify" /></td>
        <td width="100px">
        </td>
      </tr>
    </table>
    <br />
    <br />
    <br />
    </div>
  </form>
</body>
</html>

PS: Here are two online tools with similar functions for your reference:

Online random number/string generation tool:
http://tools./aideddesign/suijishu

High-strength password generator:
http://tools./password/CreateStrongPassword

For more information about C#, you can also view the special topic of this site: "Summary of C# string operation skills》、《C# data structure and algorithm tutorial》、《Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《Summary of thread usage techniques for C# programming》、《Summary of C# array operation skills"and"Introduction to C# object-oriented programming tutorial

I hope this article will be helpful to everyone's C# programming.