2015-04-06 86 views
0

您好我想通過使用foreach在方法readInput發送該賬戶方法DisableADUser將禁用帳戶,並刪除從名稱刪除帳戶名全局列表invalidAccounts(整個代碼行7)如果操作成功。不能刪除帶有remove從列表項()

我已經嘗試使用Remove方法並將它放在DisableADUser方法中的if和else條件中,但它不起作用。我應該如何着手解決這個問題?提前致謝。 :)

readInput方法(線1- 13)

//Read user input 
    private static string readInput(string Input) 
    { 

     string input = string.Empty; 

     switch (Input) 
     { 
       case "disable": 

       invalidAccount.ForEach(delegate(String samAccountName) 
        { 
         Console.WriteLine('\n' + samAccountName); 

         //disable inactive accounts 
         DisableADUser(samAccountName); 

        }); 


        //count number of invalid accounts 
        int invalidAccounts = invalidAccount.Count; 

        Console.WriteLine("\nExecution has completed. "); 

        invalidAccount.Clear(); 

        Console.WriteLine("Press [enter] to continue.\n"); 

        input = Console.ReadLine(); 

        break; 

       case "query": 

        Console.WriteLine("\nQuery for expiry has finished.\n"); 

        Console.WriteLine("Press [enter] to continue.\n"); 

        input = Console.ReadLine(); 

        break; 

       case "exit": 

        //leave console 
        Environment.Exit(2); 

        break; 

       default: 
        throw new Exception("Invalid command entered."); 
     } 

     return input; 
} 

disableADUser(線1- 15)

//disable invalid accounts 
    private static void DisableADUser(string samAccountName) 
    { 
     try 
     { 
      PrincipalContext principalContext = new PrincipalContext(ContextType.Domain); 

      UserPrincipal userPrincipal = UserPrincipal.FindByIdentity 
        (principalContext, samAccountName); 

      userPrincipal.Enabled = false; 

      userPrincipal.Save(); 

      if (userPrincipal.Enabled != true) 
      { 
       Console.WriteLine("Account has been disabled successfully"); 

       //remove from list invalidAccounts 
       invalidAccount.Remove(samAccountName); 
      } 
      else 
      { 
       Console.Write("Unable to disable account"); 

       //invalidAccount.Remove(samAccountName); 
      } 

     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.Message); 
     } 
    } 

如果需要,我已經包括我的整個代碼。

namespace ConsoleApplication2 
{ 
class Program 
{ 
    const int UF_LOCKOUT = 0x0010; 

    const int UF_PASSWORD_EXPIRED = 0x800000; 

    private static List<string> invalidAccount = new List<string>(); 

    static void Main(string[] args) 
    { 

     string line; 

     Console.WriteLine("Welcome to account validator V1.1.\n"); 

     do 
     { 

     Console.WriteLine("Please enter service account username, password \nand desired ldap address to proceed.\n\n"); 

     //pass username to GetInput method 
     String serviceAccountUserName = GetInput("Username"); 

     //pass password to GetInput method 
     String serviceAccountPassword = GetInput("Password"); 

     //pass ldap address to GetInput method 
     String ldapAddress = GetInput("Ldap address"); 

     try 
     { 

      using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, ldapAddress)) 
      { 
       bool isValid = false; 

       // validate the credentials 
       isValid = pc.ValidateCredentials(serviceAccountUserName, serviceAccountPassword); 

       if (isValid) 
       { 

        Console.WriteLine("\nQuerying for users from domain " + ldapAddress + " now.\n\n"); 

         //pass login details to GetSAM method 
         GetSAM(ldapAddress, serviceAccountUserName, serviceAccountPassword); 

         Console.WriteLine("\nEnter exit to leave.\n"); 

         Console.WriteLine("Enter disable to disable the invalid accounts.\n"); 

         Console.WriteLine("Enter query to find the expiry date of valid accounts.\n"); 

         string Input = Console.ReadLine(); 

         //pass input to readInput method 
         readInput(Input); 

         Console.WriteLine("\nEnter exit to leave."); 

         Console.WriteLine("Press [enter] to query database."); 

       }//end of if statement for validate credentials 


       else 
       { 
        Console.WriteLine("\nInvalid login credentials.\n"); 

        Console.WriteLine("Press [enter] and enter exit to leave."); 

        Console.WriteLine("\nPress [enter] [enter] to try again.\n"); 

        Console.ReadLine(); 

       }//end of else statement for validate credentials 

      }//end of using 

     }//end of try 

     catch (Exception e) 
     { 
      Console.WriteLine("\nlogin attempt has failed. See exception for more information. "); 

      throw new Exception("Log in attempt has failed." + " Exception caught:\n\n" + e.ToString()); 

     }//end of catch 

     }//end of do 

     while ((line = Console.ReadLine()) != "exit"); 

     //Thread.Sleep(60000); 
    } //end of main 


    //Read user input 
    private static string readInput(string Input) 
    { 

     string input = string.Empty; 

     switch (Input) 
     { 
       case "disable": 

       invalidAccount.ForEach(delegate(String samAccountName) 
        { 
         Console.WriteLine('\n' + samAccountName); 

         //disable inactive accounts 
         DisableADUser(samAccountName); 

        }); 


        //count number of invalid accounts 
        int invalidAccounts = invalidAccount.Count; 

        Console.WriteLine("\nExecution has completed. " + invalidAccounts + " invalid accounts have been disabled."); 

        invalidAccount.Clear(); 

        Console.WriteLine("Press [enter] to continue.\n"); 

        input = Console.ReadLine(); 

        break; 

       case "query": 

        Console.WriteLine("\nQuery for expiry has finished.\n"); 

        Console.WriteLine("Press [enter] to continue.\n"); 

        input = Console.ReadLine(); 

        break; 

       case "exit": 

        //leave console 
        Environment.Exit(2); 

        break; 

       default: 
        throw new Exception("Invalid command entered. Please enter command again."); 
     } 

     return input; 
} 

    // find password expiry date 


    //Get SAMAccount 
    private static string GetSAM(string ldapAddress, string serviceAccountUserName, string serviceAccountPassword) 
    { 

     string readOutput; 

     int countAll = 0; 

     string ldapPath = "LDAP://" + ldapAddress; 

     string ldapFilter = "(&(objectclass=user)(objectcategory=person))"; 

     DirectoryEntry directoryEntry = new DirectoryEntry(ldapPath, serviceAccountUserName, serviceAccountPassword); 

     using (DirectorySearcher directorySearcher = new DirectorySearcher(directoryEntry)) 
     { 
      string samAccountName; 

      directorySearcher.Filter = ldapFilter; 

      directorySearcher.SearchScope = SearchScope.Subtree; 

      directorySearcher.PageSize = 1000; 

      using (SearchResultCollection searchResultCollection = directorySearcher.FindAll()) 
      { 

       foreach (SearchResult result in searchResultCollection) 
       { 
        samAccountName = result.Properties["sAMAccountName"][0].ToString(); 

        //validate accounts by passing details into valSAM method 
        if (valSAM(samAccountName, ldapAddress, serviceAccountUserName, serviceAccountPassword) != true) 
        { 
         //add invalid account to list invalidAccount 
         invalidAccount.Add(samAccountName); 
        } 

        //count all accounts 
        countAll++; 

       } //end of foreach 

       // Count all invalid accounts 
       int invalidAccounts = invalidAccount.Count; 

       Console.WriteLine("\nFound " + invalidAccounts + " invalid accounts out of " + countAll + " user accounts.\n"); 

       Console.WriteLine("Query in " + ldapAddress + " has finished."); 

       Console.WriteLine("Press [enter] to continue.\n"); 

       readOutput = Console.ReadLine(); 

      }//SearchResultCollection will be disposed here 
     } 

     return readOutput; 

    } 


    //Validate SAMAccount 
    private static bool valSAM(string samAccountName, string ldapAddress, string serviceAccountUserName, string serviceAccountPassword) 
    { 
     string ldapPath = "LDAP://" + ldapAddress; 

     DirectoryEntry directoryEntry = new DirectoryEntry(ldapPath, serviceAccountUserName, serviceAccountPassword); 

     StringBuilder builder = new StringBuilder(); 

     bool accountValidation = false; 

     //create instance fo the directory searcher 
     DirectorySearcher desearch = new DirectorySearcher(directoryEntry); 

     //set the search filter 
     desearch.Filter = "(&(sAMAccountName=" + samAccountName + ")(objectcategory=user))"; 

     //find the first instance 
     SearchResult results = desearch.FindOne(); 

     using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, ldapAddress)) 
     { 

      //if users are present in database 
      if (results != null) 
      { 

       //Check if account is activated 
       bool isAccountActived = IsActive(results.GetDirectoryEntry()); 

       //Check if account is expired or locked 
       bool isAccountLocked = IsAccountLockOrExpired(results.GetDirectoryEntry()); 

       accountValidation = ((isAccountActived != true) || (isAccountLocked)); 

       //account is invalid 
       if (accountValidation) 
       { 
        builder.Append("User account " + samAccountName + " is invalid. "); 

        if ((isAccountActived != true) && (isAccountLocked)) 
        { 
         builder.AppendLine("Account is inactive and locked or expired."); 
        } else if (isAccountActived != true) 
        { 
         builder.AppendLine("Account is inactive."); 
        } 
        else if (isAccountLocked) 
        { 
         builder.AppendLine("Account is locked or has expired.") ; 
        } 
        else 
        { 
         builder.AppendLine("Unknown reason. Contact admin for help."); 
        } 

        accountValidation = false; 

       } 

       //account is valid 
       if ((isAccountActived) && (isAccountLocked != true)) 
       { 
        builder.AppendLine("User account " + samAccountName + " is valid."); 

        accountValidation = true; 
       } 

      } 
      else Console.WriteLine("No users found."); 

      //print only invalid accounts 
      if (!accountValidation) 
      { 
       //prevent printing of empty lines 
       if (builder.Length > 0) 
       { 
        Console.WriteLine(builder); 
       } 
      } 

     }//end of using 

     return accountValidation; 

    } 


    //Prevent empty user input 
    private static string GetInput(string Prompt) 
    { 

     string Result = string.Empty; 

     do 
     { 
      Console.Write(Prompt + ": "); 

      Result = Console.ReadLine(); 

      if (string.IsNullOrEmpty(Result)) Console.WriteLine("Empty input, please try again.\n"); 

     } 

     while (!(!string.IsNullOrEmpty(Result))); 

     return Result; 

    } 


    //check if account is active 
    static private bool IsActive(DirectoryEntry de) 
    { 
     if (de.NativeGuid == null) return false; 

     int flags = (int)de.Properties["userAccountControl"].Value; 

     return !Convert.ToBoolean(flags & 0x0002); 
    } 

    //check if account is locked or expired 
    static private bool IsAccountLockOrExpired(DirectoryEntry de) 
    { 
     string attribName = "msDS-User-Account-Control-Computed"; 

     de.RefreshCache(new string[] { attribName }); 

     int userFlags = (int)de.Properties[attribName].Value; 

     return userFlags == UF_LOCKOUT || userFlags == UF_PASSWORD_EXPIRED; 
    } 


    //disable invalid accounts 
    private static void DisableADUser(string samAccountName) 
    { 
     try 
     { 
      PrincipalContext principalContext = new PrincipalContext(ContextType.Domain); 

      UserPrincipal userPrincipal = UserPrincipal.FindByIdentity 
        (principalContext, samAccountName); 

      userPrincipal.Enabled = false; 

      userPrincipal.Save(); 

      if (userPrincipal.Enabled != true) 
      { 
       Console.WriteLine("User " + samAccountName + "'s account has been disabled successfully"); 

       //remove from list invalidAccounts 
       invalidAccount.Remove(samAccountName); 
      } 
      else 
      { 
       Console.Write("Unable to disable account"); 

       //invalidAccount.Remove(samAccountName); 
      } 

     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.Message); 
     } 
    } 


} 
} 
+1

你需要學習如何創建一個[如何創建一個最小的,完整的,並且可驗證的示例](http://stackoverflow.com/help/mcve) 。這裏有太多的代碼來演示這個問題。當問一個問題時,儘量在儘可能少的行內製作一個能夠展示問題的程序。 – 2015-04-06 04:59:03

回答

1

您不能從正在迭代的列表中刪除項目。它與枚舉器混淆,從它下面刪除了一些東西。您需要將想要保留的項目複製到另一個列表中,並在必要時將其複製回來;或者創建一個你想刪除的項目列表,並在最後一次全部刪除它們。

這有各種方法的討論:Intelligent way of removing items from a List<T> while enumerating in C#

+0

好的答案,coder123你需要保留一個備份和刪除項目的備份根據索引 – Shubhojit 2015-04-06 06:58:49