ScrewTurn.Wiki.Tools.GenerateRandomPassword C# (CSharp) Method

GenerateRandomPassword() public static method

Generates a random 10-char Password.
public static GenerateRandomPassword ( ) : string
return string
        public static string GenerateRandomPassword()
        {
            Random r = new Random();
            string password = "";
            for(int i = 0; i < 10; i++) {
                if(i % 2 == 0)
                    password += ((char)r.Next(65, 91)).ToString(); // Uppercase letter
                else password += ((char)r.Next(97, 123)).ToString(); // Lowercase letter
            }
            return password;
        }

Usage Example

示例#1
0
        /// <summary>
        /// Migrates all the User Accounts from a Provider to another.
        /// </summary>
        /// <param name="source">The source Provider.</param>
        /// <param name="destination">The destination Provider.</param>
        /// <param name="sendEmailNotification">A value indicating whether or not to send a notification email message to the moved users.</param>
        public static void MigrateUsersStorageProviderData(IUsersStorageProviderV30 source, IUsersStorageProviderV30 destination, bool sendEmailNotification)
        {
            // User groups
            var groups = source.GetUserGroups().ToArray();

            foreach (UserGroup group in groups)
            {
                destination.AddUserGroup(group.Name, group.Description);
            }

            // Users
            var users      = source.GetUsers().ToArray();
            var movedUsers = new MovedUser[users.Length];

            for (var i = 0; i < users.Length; i++)
            {
                // Generate new Password, create MovedUser object, add new User, delete old User
                var password = Tools.GenerateRandomPassword();
                movedUsers[i] = new MovedUser(users[i].Username, users[i].Email, users[i].DateTime);
                var newUser = destination.AddUser(users[i].Username, users[i].DisplayName, password, users[i].Email, users[i].Active, users[i].DateTime);

                // Membership
                destination.SetUserMembership(newUser, users[i].Groups);

                // User data
                IDictionary <string, string> uData = source.RetrieveAllUserData(users[i]);
                foreach (KeyValuePair <string, string> pair in uData)
                {
                    destination.StoreUserData(newUser, pair.Key, pair.Value);
                }

                source.RemoveUser(users[i]);
            }

            // Remove old groups
            foreach (UserGroup group in groups)
            {
                source.RemoveUserGroup(group);
            }

            if (sendEmailNotification)
            {
                // Send Emails
                for (var i = 0; i < movedUsers.Length; i++)
                {
                    Users.SendPasswordResetMessage(movedUsers[i].Username, movedUsers[i].Email, movedUsers[i].DateTime);
                }
            }
        }