Random Password Generator in C#.NET

This blog is going to illustrate the Random Password Generator in the .NET Framework. The GeneratePassword method is used to generate a random password and is most commonly used for reset password and new or temporary password. It is a very easy to use method.

GeneratePassword() method will generate the password with specific length.

The following is a syntax for GeneratePassword method

public static string GeneratePassword (int lengthint numberOfNonAlphanumericCharacters);

Length (int): The password length

numberOfNonAlphanumericCharacters (int): Minimum no. of alphanumeric characters.

The following is a code snippet to generate a random password. Here length and nonAlphaNumeric values are passed to Membership.GeneratePassword() method. And the result has been assigned to the password textbox.

using System.Web.Security;
 
private void GeneratePasswordButton_Click(object senderEventArgs e)
{
    passwordTextBox.Text =  Membership.GeneratePassword(int.Parse(lengthTextBox.Text), int.Parse(nonAlphaNumTextBox.Text));
}

The following is an output of the above code. The password is generated based on the length and number of alphanumeric characters.

I hope this helps you. Keep coding.


Comments

  1. The System.Web namespace, which includes the System.Web.Security.Membership.GeneratePassword(int length, int numberOfNonAlphanumericCharacters) method referenced in this article, is unsupported in .NET Core & .NET 5+.

    ReplyDelete
  2. Thank you for your comment and bringing up an important point. You're absolutely right that the System.Web namespace, including the System.Web.Security.Membership.GeneratePassword method, is not supported in .NET Core and .NET 5+.

    You can use the RNGCryptoServiceProvider class to generate a random sequence of characters that can be combined to form a secure password

    ReplyDelete

Post a Comment

Popular posts from this blog

Entity Framework Core (EF) with SQL Server LocalDB

Exploring EventCallback in Blazor: Building Interactive Components

A Step-by-Step Guide to Implementing Identity in ASP.NET Core MVC