Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Generating Random Password in .NET

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 53
    Comment on it

    For doing password Reset and changing password we always have a thing whether to send the same old password or send a random password to the user.

    For generating random password to the user we  will create a code file that will generate random password every-time when the request has been made.

     

    For doing that i have created a class file in my project called as RandomPassword

     public class PasswordGenerator
        {
            public static string GenerateRandomString()
            {
                string ForgotPasswordLength = ConfigurationManager.AppSettings["ForgotPasswordLength"].ToString();
                //It will generate string with combination of small,capital letters and numbers
                char[] charArr = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".ToCharArray();
                string randomString = string.Empty;
                Random objRandom = new Random();
                for (int i = 0; i <=Convert.ToInt32(ForgotPasswordLength) - 1; i++)
                {
                    //Don't Allow Repetation of Characters
                    int x = objRandom.Next(1, charArr.Length);
                    if (!randomString.Contains(charArr.GetValue(x).ToString()))
                    {
                        randomString += charArr.GetValue(x);
                    }
                    else {
                        i -= 1;
                    }
                }
                return randomString;
            }
    
        }
    

     

    This file will create random password each time we want

    To use this file password we will write and mail the random password to the user

       int rowsAffected = 0;
                SqlTransaction transaction = null;
                string randomPassword = PasswordGenerator.GenerateRandomString();
                empResponse = new EmployeeResponse();
                using (SqlConnection sqlConnection = new SqlConnection(ConnectionString))
                {
                    if (sqlConnection.State == ConnectionState.Closed)
                    {
                        sqlConnection.Open();
                    }
                   
                    transaction = sqlConnection.BeginTransaction("ForgotPasswordTransaction");
                    try
                    {
                        SqlCommand cmd = new SqlCommand("uspForgotPassword", sqlConnection,transaction);
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add("@Email", SqlDbType.VarChar, 100).Value = Email;
                        cmd.Parameters.Add("@Password", SqlDbType.VarChar, 100).Value = randomPassword;
                        cmd.Parameters.Add("@PasswordSalt", SqlDbType.VarChar, 200).Value = Guid.NewGuid().ToString();
                        rowsAffected = cmd.ExecuteNonQuery();
                        if (rowsAffected > 0)
                        {
                            bool isMailSent = Mail.SendMail(Email, randomPassword);
    
                            if (isMailSent)
                            {
                                transaction.Commit();
                                empResponse.Status = true;
                                empResponse.Message = "New password successfully sent in your mail.";
                            }
                            else {
                                transaction.Rollback();
                                empResponse.Status = false;
                                empResponse.Message = "Problem in sending password in your mail.";
                            }
                        }
                        else {
                            transaction.Rollback();
                            empResponse.Status = false;
                            empResponse.Message = "Please check your email address and then try again.";
                        }
    
                    }
                    catch (SqlException ex)
                    {
                        Logger.LogException(ex);
                        transaction.Rollback();
                        empResponse.Status = false;
                        empResponse.Message = ex.Message;
                    }
                }
                return empResponse;
            }
    
    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: