示例#1
0
        private LocalPrincipalData InnerCreateUser(string userName)
        {
            string             rvUserName = null;
            string             rvPassword = null;
            LocalPrincipalData rv         = null;

            using (var context = new PrincipalContext(ContextType.Machine))
            {
                bool          userSaved = false;
                ushort        tries     = 0;
                UserPrincipal user      = null;

                try
                {
                    do
                    {
                        try
                        {
                            if (user != null)
                            {
                                user.Dispose();
                            }

                            rvPassword       = Membership.GeneratePassword(8, 2).ToLowerInvariant() + Membership.GeneratePassword(8, 2).ToUpperInvariant();
                            user             = new UserPrincipal(context, userName, rvPassword, true);
                            user.DisplayName = "Warden User " + userName;
                            user.Save();
                            userSaved = true;
                        }
                        catch (PasswordException ex)
                        {
                            log.DebugException(ex);
                        }

                        ++tries;
                    } while (userSaved == false && tries < 5);

                    if (userSaved)
                    {
                        rvUserName = user.SamAccountName;

                        foreach (string userGroupName in this.wardenUserGroups)
                        {
                            AddUserToGroup(rvUserName, userGroupName);
                        }

                        rv = new LocalPrincipalData(rvUserName, rvPassword);
                    }
                }
                finally
                {
                    if (user != null)
                    {
                        user.Dispose();
                    }
                }
            }

            return(rv);
        }
        private LocalPrincipalData InnerCreateUser(string userName)
        {
            string rvUserName = null;
            string rvPassword = null;
            LocalPrincipalData rv = null;

            using (var context = new PrincipalContext(ContextType.Machine))
            {
                bool userSaved = false;
                ushort tries = 0;
                UserPrincipal user = null;

                try
                {
                    do
                    {
                        try
                        {
                            if (user != null)
                            {
                                user.Dispose();
                            }

                            rvPassword = Membership.GeneratePassword(8, 2).ToLowerInvariant() + Membership.GeneratePassword(8, 2).ToUpperInvariant();
                            user = new UserPrincipal(context, userName, rvPassword, true);
                            user.DisplayName = "Warden User " + userName;
                            user.Save();
                            userSaved = true;
                        }
                        catch (PasswordException ex)
                        {
                            log.DebugException(ex);
                        }

                        ++tries;
                    } while (userSaved == false && tries < 5);

                    if (userSaved)
                    {
                        rvUserName = user.SamAccountName;

                        foreach (string userGroupName in this.wardenUserGroups)
                        {
                            AddUserToGroup(rvUserName, userGroupName);
                        }

                        rv = new LocalPrincipalData(rvUserName, rvPassword);
                    }
                }
                finally
                {
                    if (user != null)
                        user.Dispose();
                }
            }

            return rv;
        }