Create Random Password - CSharp System

CSharp examples for System:Random

Description

Create Random Password

Demo Code


using System.Threading.Tasks;
using System.Text;
using System.Security.Cryptography;
using System.Linq;
using System.Collections.Generic;
using System;//  ww w.  j av  a2s  .  co m

public class Main{
        public static string CreateRandomPassword(int passwordLength)
        {
            const string allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
            var randNum = new Random();
            var chars = new char[passwordLength];

            for (int i = 0; i < passwordLength; i++)
            {
                chars[i] = allowedChars[randNum.Next(0, allowedChars.Length)];
            }

            return new string(chars);
        }
}

Related Tutorials