Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,926 questions

51,859 answers

573 users

How to generate random password in C#

3 Answers

0 votes
using System;
using System.Text;
 
public class Program
{
    public static string GenerateRandomPassword(int length) {
        string chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%&";
 
        StringBuilder sb = new StringBuilder();
        Random rnd = new Random();
 
        for (int i = 0; i < length; i++) {
            int index = rnd.Next(chars.Length);
            sb.Append(chars[index]);
        }
 
        return sb.ToString();
    }
 
    public static void Main()
    {
        int length = 7;
 
        string password = GenerateRandomPassword(length);
        
        Console.WriteLine(password);
    }
}




 
/*
run:

cZ!v6tw

*/

 



answered Mar 28, 2023 by avibootz
0 votes
using System;
using System.Linq;
 
public class Program
{
    public static string GenerateRandomPassword(int length) {
        string chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%&";
 
        Random rnd = new Random();
        
        return new string(Enumerable.Repeat(chars, length)
                .Select(s => s[rnd.Next(s.Length)]).ToArray());
    }
 
    public static void Main()
    {
        int length = 7;
 
        string password = GenerateRandomPassword(length);
        
        Console.WriteLine(password);
    }
}




 
/*
run:

@%un917

*/

 



answered Mar 28, 2023 by avibootz
0 votes
using System;
using System.Security.Cryptography;
 
public class Program
{
    public static string GenerateRandomPassword(int length) {
        byte[] password = new byte[length];
        
        RNGCryptoServiceProvider rngCrypt = new RNGCryptoServiceProvider();
        rngCrypt.GetBytes(password);
        
        return Convert.ToBase64String(password);
    }
 
    public static void Main()
    {
        int length = 7;
 
        string password = GenerateRandomPassword(length);
        
        Console.WriteLine(password);
    }
}




 
/*
run:

ohB8E73Vog==

*/

 



answered Mar 28, 2023 by avibootz

Related questions

1 answer 106 views
1 answer 83 views
1 answer 85 views
1 answer 69 views
1 answer 71 views
3 answers 123 views
123 views asked Dec 22, 2024 by avibootz
2 answers 93 views
93 views asked Dec 21, 2024 by avibootz
...