Generate Random Passwords in C#
Complete code tutorial with examples and best practices
[ Code Example - Quick Summary ]
Language: C#
What: Generate secure random passwords in C# using <code>RandomNumberGenerator</code> for cryptographically secure random data. Perfect for ASP.NET and .NET applications.
Try it: Use our interactive Passwords generator or integrate this code into your C# application.
Generate secure random passwords in C# using RandomNumberGenerator for cryptographically secure random data. Perfect for ASP.NET and .NET applications.
Looking for other languages? Check our code examples in
PHP
,
JavaScript
,
Python
,
Java
,
C++
,
Ruby
and
Go
or use our interactive web generator.
C# Code Example
using System;
using System.Security.Cryptography;
using System.Text;
class PasswordGenerator
{
private const string Lowercase = "abcdefghijklmnopqrstuvwxyz";
private const string Uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private const string Numbers = "0123456789";
private const string Special = "!@#$%^&*()-_=+[]{}|;:,.<>?";
public static string GeneratePassword(int length = 16, bool includeSpecial = true)
{
string chars = Lowercase + Uppercase + Numbers;
if (includeSpecial)
{
chars += Special;
}
using (var rng = RandomNumberGenerator.Create())
{
var password = new StringBuilder(length);
byte[] randomBytes = new byte[length * 4];
rng.GetBytes(randomBytes);
for (int i = 0; i < length; i++)
{
uint randomValue = BitConverter.ToUInt32(randomBytes, i * 4);
password.Append(chars[(int)(randomValue % chars.Length)]);
}
return password.ToString();
}
}
static void Main()
{
// Generate a 16-character password with special characters
string password = GeneratePassword(16, true);
Console.WriteLine(password); // Example: aB3$xY7!mN2@pQ9&
// Generate a 12-character password without special characters
string simplePassword = GeneratePassword(12, false);
Console.WriteLine(simplePassword); // Example: aB3xY7mN2pQ9
}
}
[EXPLANATION]
RandomNumberGenerator provides cryptographically secure random bytes. The method converts random bytes to indices for character selection, ensuring uniform distribution across the character set.
Expected Output
aB3$xY7!mN2@pQ9& aB3xY7mN2pQ9
Common Use Cases
- ASP.NET Core web application authentication
- WPF/WinForms desktop application security
- Unity game development (account systems)
- Azure Functions user provisioning
- Xamarin mobile app security features
- .NET MAUI cross-platform apps
Important Notes
-
Use
RandomNumberGeneratorfor cryptographic security -
StringBuilderimproves performance for string building - Available in .NET Framework, .NET Core, and .NET 5+
-
Consider using
PasswordValidatorto enforce rules
Try Our Interactive Generator
Don't want to write code? Use our free web-based Passwords generator with instant results.
TRY PASSWORDS GENERATOR →Other Programming Languages
View Passwords generation code examples in PHP
View Passwords generation code examples in JavaScript
View Passwords generation code examples in Python
View Passwords generation code examples in Java
View Passwords generation code examples in C++
View Passwords generation code examples in Ruby
View Passwords generation code examples in Go