2015-03-25 68 views
2

嗨我想阻止用戶在輸入字段中不輸入任何內容。防止用戶在c中的空控制檯輸入#

我試過使用一個如果別的,但控制檯不斷崩潰時,沒有輸入。 (對於用戶輸入和LDAP地址輸入==>我想讓它顯示,並允許用戶重新輸入用戶名「無輸入檢測。」)

如果我用(results == " "),我會得到一個錯誤:

"Operator '==' cannot be applied to operands of type 'System.DirectoryServices.SearchResult' and 'string'"

有沒有什麼辦法可以解決這個問題?代碼如下所示。

影響的代碼從管線16日起(對於碼的頂塊)

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

    if (isAccountActived) 
     Console.WriteLine(targetUserName + "'s account is active."); 
    else 
     Console.WriteLine(targetUserName + "'s account is inactive."); 

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

    if (isAccountLocked) 
     Console.WriteLine(targetUserName + "'s account is locked or has expired."); 
    else 
     Console.WriteLine(targetUserName + "'s account is not locked or expired."); 

    Console.WriteLine("\nEnter bye to exit."); 
    Console.WriteLine("Press any key to continue.\n\n"); 
}        
else if (results == " ") 
{ 
    //no user entered 
    Console.WriteLine("No input detected!"); 
    Console.WriteLine("\nEnter bye to exit."); 
    Console.WriteLine("Press any key to continue.\n"); 
} 
else 
{ 
    //user does not exist 
    Console.WriteLine("User not found!"); 
    Console.WriteLine("\nEnter bye to exit."); 
    Console.WriteLine("Press any key to continue.\n"); 
} 

如果有幫助,我已附加下面的整個代碼。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.DirectoryServices; 
using System.DirectoryServices.AccountManagement; 
using System.Net; 
using System.Net.Sockets; 
using System.Net.NetworkInformation; 
using System.Data.SqlClient; 

namespace ConsoleApplication2 
{ 
    class Program 
    { 
     const String serviceAccountUserName = "mobileuser1"; 
     const String serviceAccountPassword = "password123$"; 
     const int UF_LOCKOUT = 0x0010; 
     const int UF_PASSWORD_EXPIRED = 0x800000; 

     static void Main(string[] args) 
     { 
      string line; 
      Console.WriteLine("Welcome to account validator V1.0.\n"+"Please enter the ldap address to proceed."); 
      Console.Write("\nEnter address: "); 
      String ldapAddress = Console.ReadLine(); 
      try 
      { 
       if (ldapAddress != null) 
       { 
        Console.WriteLine("\nQuerying for users in " + ldapAddress); 
        //start of do-while 
        do 
        { 
         Console.WriteLine("\nPlease enter the user's account name to proceed."); 
         Console.Write("\nUsername: "); 
         String targetUserName = Console.ReadLine(); 

         bool isValid = false; 

         using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, ldapAddress)) 
         { 
          // validate the credentials 
          isValid = pc.ValidateCredentials(serviceAccountUserName, serviceAccountPassword); 

          // search AD data 
          DirectoryEntry entry = new DirectoryEntry("LDAP://" + ldapAddress, serviceAccountUserName, serviceAccountPassword); 

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

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

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

          if (results != null) 
          { 

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

           if (isAccountActived) Console.WriteLine(targetUserName + "'s account is active."); 

           else Console.WriteLine(targetUserName + "'s account is inactive."); 


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

           if (isAccountLocked) Console.WriteLine(targetUserName + "'s account is locked or has expired."); 

           else Console.WriteLine(targetUserName + "'s account is not locked or expired."); 


           Console.WriteLine("\nEnter bye to exit."); 
           Console.WriteLine("Press any key to continue.\n\n"); 
          } 
          else if (results == " ") 
          { 
           //no user entered 
           Console.WriteLine("No input detected!"); 
           Console.WriteLine("\nEnter bye to exit."); 
           Console.WriteLine("Press any key to continue.\n"); 
          } 
          else 
          { 
           //user does not exist 
           Console.WriteLine("User not found!"); 
           Console.WriteLine("\nEnter bye to exit."); 
           Console.WriteLine("Press any key to continue.\n"); 
          } 

         }//end of using       
        }//end of do 

        //leave console when 'bye' is entered 
        while ((line = Console.ReadLine()) != "bye"); 

       }//end of if for ldap statement 
       else if (ldapAddress == " ") 
       { 
        Console.WriteLine("No input detected."); 
        Console.ReadLine(); 
        Console.WriteLine("\nEnter bye to exit."); 
        Console.ReadLine(); 
        Console.WriteLine("Press any key to continue.\n"); 
        Console.ReadLine(); 
       } 
       else 
       { 
        Console.WriteLine("Address not found!"); 
        Console.ReadLine(); 
        Console.WriteLine("\nEnter bye to exit."); 
        Console.ReadLine(); 
        Console.WriteLine("Press any key to continue.\n"); 
        Console.ReadLine(); 
       } 

      }//end of try 
      catch (Exception e) 
      { 
       Console.WriteLine("Exception caught:\n\n" + e.ToString()); 
      } 
     } //end of main void 

     static private bool IsActive(DirectoryEntry de) 
     { 
      if (de.NativeGuid == null) return false; 

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

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

     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; 
     } 
    } 
} 
+1

(!string.IsNullOrEmpty(輸入)); – tharif 2015-03-25 06:51:57

+0

嗨實用程序,謝謝你回答我的問題。 – coder123 2015-03-25 08:08:36

+0

是否解決了問題?如果有幫助標記爲答案 – tharif 2015-03-25 08:16:30

回答

2

您應該將ReadLine置於循環中。

string UserName = ""; 
do { 
    Console.Write("Username: "); 
    UserName = Console.ReadLine(); 
    if (!string.IsNullOrEmpty(UserName)) { 
     Console.WriteLine("OK"); 
    } else { 
     Console.WriteLine("Empty input, please try again"); 
    } 
} while (string.IsNullOrEmpty(UserName)); 

您基本上一直重複提示,直到用戶輸入的字符串不再爲空或空。 最佳方法很可能會創建一個新的函數來獲取非空輸入:

private static string GetInput(string Prompt) 
{ 
    string Result = ""; 
    do { 
     Console.Write(Prompt + ": "); 
     Result = Console.ReadLine(); 
     if (string.IsNullOrEmpty(Result)) { 
      Console.WriteLine("Empty input, please try again"); 
     } 
    } while (string.IsNullOrEmpty(Result)); 
    return Result; 
} 

然後,您可以只使用函數來得到這樣的輸入:

static void Main(string[] args) 
{ 
    GetInput("Username"); 
    GetInput("Password"); 
} 

結果: enter image description here

+0

您好Jens,我如何在我的「靜態無效Main(string [] args)」方法中使用GetInput?當我嘗試調用它時,我得到「非靜態字段,方法或屬性需要對象引用」錯誤。提前致謝! :) – coder123 2015-03-25 08:07:32

+0

如果您阻止用戶只輸入空格會更好。否則,這是最好的方法! – Younes 2015-03-25 08:07:42

+0

@ coder123只是使用「私人靜態字符串GetInput(字符串提示)」... – Younes 2015-03-25 08:08:28

0

嘗試使用代碼:

(!string.IsNullOrEmpty(input));