1
Answer

code for generating username and password dynamically

ANUSHA VULLI

ANUSHA VULLI

11y
1.1k
1
In registration form user will give his/her first name and last name.By using these names we have to generate unique Username and password for that user and send these to his/her mail Id,to mobile which  are given by user registration form in asp.net (n-tier architecture)
Answers (1)
1
Javeed M Shaikh

Javeed M Shaikh

NA 7.8k 69.7k 11y
Ideally you should create GUID if you want unique strings, but GUID is very big for a username or password. You can also try to generate random alphanumeric text as username/password but you have to make sure that you check that this does not exists for any existing user before you send the email. Here is an example:

you can use the useLetter parameter to generate username in alphabets and password in numbers or change the code to use both.

public static string GenerateString(int length, bool useLetter)
        {           
            string charsandspecialonly = "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
            string numberonly = "1234567890";
            char[] chars = new char[length];
            Random rd = new Random();

            for (int i = 0; i < length; i++)
            {
                if (useLetter)
                {
                    chars[i] = charsandspecialonly[rd.Next(0, charsandspecialonly.Length)];
                    
                }
                else
                {
                    chars[i] = numberonly[rd.Next(0, numberonly.Length)];
                 
                }

            }

            return new string(chars);
        }