2016-08-15 66 views
0

我正在寫一些微型AD工具(與VS-C#)到我們的組織,並陷入了一個問題。C#問題與操縱ActiveDirectory用戶

我有一個主要功能,搜索用戶(當我點擊它在一個列表視圖)和一些功能,操縱用戶的對象。

public DirectoryEntry GetUser(string username) 
    { 
     try 
     { 
      Forest currentForest = Forest.GetCurrentForest(); 
      GlobalCatalog gc = currentForest.FindGlobalCatalog(); 

      using (DirectorySearcher searcher = gc.GetDirectorySearcher()) 
      { 
       searcher.Filter = "(&((&(objectCategory=Person)(objectClass=User)))(samaccountname=" + username + "*))"; 
       SearchResult results = searcher.FindOne(); 
       if (!(results == null)) 
       { 

        DirectoryEntry de = new DirectoryEntry(results.Path, strAdminUser, strAdminPass, AuthenticationTypes.Secure); 
        de.RefreshCache(new string[] { "canonicalName" }); 
        de.Path = de.Properties["canonicalName"].Value.ToString(); 
        de.CommitChanges(); 
        return de; 
       } 
       else 
       { 
        return null; 
       } 
      } 
     } 
     catch (DirectoryServicesCOMException e) 
     { 
      System.Windows.Forms.MessageBox.Show(e.Message); 
      return null; 
     } 
    } 

,這裏是一個檢查,如果用戶被鎖定的功能的例子:

public bool IsUserLocked(string username) 
    { 
     try 
     { 
      DirectoryEntry de = GetUser(username); 
      string attName = "msDS-User-Account-Control-Computed"; 
      de.RefreshCache(new string[] { attName }); 
      const int UF_LOCKOUT = 0x0010; 
      int userFlags = /*(int)*/Convert.ToInt32(de.Properties[attName].Value); 
      if ((userFlags & UF_LOCKOUT) == UF_LOCKOUT) 
      { 
       return true; 
      } 
      de.Dispose(); 
      return false; 
     } 
     catch (DirectoryServicesCOMException e) 
     { 
      System.Windows.Forms.MessageBox.Show(e.Message); 
      return false; 
     } 
     } 

,檢查用戶的鎖定狀態總是失敗,出現錯誤的功能:「未指定的錯誤」,但如果我沒有更改第一個函數中的目錄項的路徑,我得到「服務器不願意處理請求」錯誤(我正在使用正確的服務用戶名和密碼以及所需的所有權限),但仍然它發生。

有人可以發現問題嗎?

+0

做一個谷歌搜索..有幾個例子在網上以及在Stackoverflow關於檢查鎖定狀態使用C#在這裏檢查初學者 - http://stackoverflow.com/questions/2005637/如何確定,如果用戶帳戶是啓用或禁用的 – MethodMan

回答

0

明白了...

這解決了我的問題: de.Path = results.Path.Replace( 「GC:// DCNAME」, 「LDAP://」) ; 由於我searcing全局編錄上,我曾在路徑替換的部分匹配到正確的路徑:

public DirectoryEntry GetUser(string username) 
    { 
     try 
     { 
      Forest currentForest = Forest.GetCurrentForest(); 
      GlobalCatalog gc = currentForest.FindGlobalCatalog(); 

      using (DirectorySearcher searcher = gc.GetDirectorySearcher()) 
      { 
       searcher.Filter = "(&((&(objectCategory=Person)(objectClass=User)))(samaccountname=" + username + "*))"; 
       SearchResult results = searcher.FindOne(); 
       if (!(results == null)) 
       { 

        DirectoryEntry de = new DirectoryEntry(results.Path, strAdminUser, strAdminPass, AuthenticationTypes.Secure); 
        de = new DirectoryEntry(results.Path); 
        de.Path = results.Path.Replace("GC://DCNAME.", "LDAP://"); 
        de.CommitChanges(); 
        //System.Windows.Forms.MessageBox.Show(de.Path); 
        return de; 
       } 
       else 
       { 
        return null; 
       } 
      } 
     } 
     catch (DirectoryServicesCOMException e) 
     { 
      System.Windows.Forms.MessageBox.Show(e.Message); 
      return null; 
     } 
    } 

現在的路徑返回稱爲的getUser在正確的功能格式:)

0

如何使用System.DirectoryServices.AccountManagement命名空間?如果您在使用新名稱空間時沒有問題,則可以使用更簡單的方法來檢查用戶帳戶是否爲lockedunlock(如果需要)。

public bool IsUserLocked (string username) 
{ 
    using(PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "yourdomain.com") 
    { 
     using (UserPrincipal user = UserPrincipal.FindByIdentity(ctx, username) 
     { 
      if (user != null) return user.IsAccountLockedOut(); 
     } 
    } 
    return null; 
} 

與此類似,如果需要,您可以使用unlock用戶帳戶。

... 

if (user != null) 
{ 
    user.UnlockAccount(); 
    user.Save(); 
} 
+0

我們在組織中有一個域和子域,所以我首先需要確定什麼是我們呃的域名,然後使用你的例子.... –

+0

你知道域之前有'用戶名'? – smr5

+0

是的,我得到一個列表視圖列表與用戶符合搜索條件。 我使用這個來獲取列表: –