2016-08-15 63 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

是的,我得到一个列表视图列表与用户符合搜索条件。 我使用这个来获取列表: –