Amazon Ad

Wednesday 26 November 2014

Generate Password As Per Password Policy in C#

Hi Guys,

I was given a task to generate a password having following conditions

1. Min 10 Chracters
2. Must have one upper case letters
3. Must have one or more lower case letters
4. Must have one or more special characters
5. Must have one or more numbers.

Following is how i was able to do the same in C#

private string ActionNewPassword()
    {
        String Num = "0123456789";
        String Sp = "!@#$%&*()+{}[]^-";
        String SL = "abcdefghijklmnopqrstuvwxyz";
        String UCL = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        Random rnd = new Random();
       
        //One or more upper case letters
        string part1 = UCL.Substring(rnd.Next(UCL.Length),1);

        //One or more numbers
        string part2 = Num.Substring(rnd.Next(Num.Length), 1);

        //Minimum 4 small case letters
        string part3 = "";
        for(int i=0;i<4;i++)
            part3 += SL.Substring(rnd.Next(SL.Length), 1);

        //Minimum 1 special character
        string part4 = Sp.Substring(rnd.Next(Sp.Length), 1);

        //Minimum 3 small numbers
        string part5 = "";
        for (int i = 0; i < 3; i++)
            part5 += SL.Substring(rnd.Next(SL.Length), 1);

        string pwd = part1 + part2+part3+part4+part5;

        //Message.InnerHtml = pwd;
       
        return pwd;
    }

Thanks
Ritesh Tandon

No comments:

Post a Comment

Comments are welcome, Please join me on my Linked In account

http://in.linkedin.com/pub/ritesh-tandon/21/644/33b

How to implement Captcha v3 in ASP.NET

 I was facing an issue of dom parsing in my website. I finally resolved it by using Google Captcha V3. Step 1: Get your keys from https:...