使用C#在Active Directory中获取用户的父OU OU#

问题描述:

我想检查用户是否在特定的父OU中。使用C#在Active Directory中获取用户的父OU OU#

我该怎么做?

请查看以下代码,了解我所寻找的内容。

using System.DirectoryServices.AccountManagement; 

public bool IsUserInOU(string samAccountName, string OUName){ 

    using (var context = new PrincipalContext(ContextType.Domain)) 
     { 
      using (var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, samAccountName)) 
      {      
       //Check if the user is in the OU specified in OUName 
       //Something like: 
       //return user.IsInOU(OUName); 
      } 
     } 
} 

public void TestIt_1(){ 
    //The parent OU of this user is "AwesomeOU" 
    string samAccountName = "Joe"; 
    string OUName = "AwesomeOU"; 
    bool expected = true; 
    bool actual = IsUserInOU(samAccountName, OUName); 
    Assert.AreEqual(expected, actual); 
} 

public void TestIt_2(){ 
    //The parent OU of this user is "WhateverOU" 
    string samAccountName = "Mike"; 
    string OUName = "AwesomeOU"; 
    bool expected = false; 
    bool actual = IsUserInOU(samAccountName, OUName); 
    Assert.AreEqual(expected, actual); 
} 

域:

  • 国家OU
    • 真棒OU
    • 无论OU
      • 迈克·

EMPI的回答

与EMPI给出的信息后,解决方案1,我写了下面的方法提取的distinguishedName来第一个OU。完成之后,剩下的就变得轻而易举了。

public static string GetOUForUser(string samAccountName) 
    { 
     using (var context = new PrincipalContext(ContextType.Domain)) 
     { 
      using (var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, samAccountName)) 
      { 
       //System.Console.WriteLine(user.DistinguishedName); 
       int startIndex = user.DistinguishedName.IndexOf("OU=", 1) + 3; //+3 for length of "OU=" 
       int endIndex = user.DistinguishedName.IndexOf(",", startIndex); 
       var group = user.DistinguishedName.Substring((startIndex), (endIndex - startIndex)); 
       return group; 
      } 
     } 
    } 

解决方案2后JPBlanc的回答

public static string GetOUForUser(string samAccountName) 
    { 
     using (var context = new PrincipalContext(ContextType.Domain)) 
     { 
      using (var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, samAccountName)) 
      { 
       using (DirectoryEntry deUser = user.GetUnderlyingObject() as DirectoryEntry) 
       { 
        using (DirectoryEntry deUserContainer = deUser.Parent) 
        { 
         return deUserContainer.Properties["Name"].Value.ToString(); 
        } 
       } 
      } 
     } 
    } 
+1

如果对象在其可分辨名称中包含逗号,则不起作用。您需要处理逃脱方式,或使用JPBlanc的解决方案2. – Chalky 2015-09-29 20:17:42

好@Empi解决方案的工作,但UserPrincipal是建立在DirectoryEntry对象提供parentcontainer性质只是给你你正在寻找的对象,而无需使用字符串方式。

/* Retreiving a principal context 
*/ 
PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, "WM2008R2ENT:389", "dc=dom,dc=fr", "dom\\jpb", "MyPwd"); 

/* Retreive a user 
*/ 
UserPrincipal user = UserPrincipal.FindByIdentity(domainContext, "user1"); 

/* Retreive the container 
*/ 
DirectoryEntry deUser = user.GetUnderlyingObject() as DirectoryEntry; 
DirectoryEntry deUserContainer = deUser.Parent; 
Console.WriteLine (deUserContainer.Properties["distinguishedName"].Value); 
+1

太棒了!似乎不太可能没有更优雅的方式。 :) – Kjensen 2012-04-13 07:39:49

+1

不知道:) – empi 2012-04-13 09:20:44

这些信息在UserPrincipal.DistinguishedName。您应该检查DistinguishedName是否以“,”+ ou专名(不区分大小写)结尾。但是,您必须知道您要查看的姓名。

例如,如果DN是:CN=Jeff Smith,OU=Sales,DC=Fabrikam,DC=COM,然后它说,用户在OU=Sales,DC=Fabrikam,DC=COM OU。

+1

这是更进一步的,谢谢!我现在可以用我的方式解决问题,但似乎应该有更好的方法。 – Kjensen 2012-04-12 11:07:52

+3

据我所知这不是黑客。这只是目录服务的工作方式。如果你有一个文件路径,你应该检查文件是否在某个目录中,你会做同样的事情。 – empi 2012-04-12 11:10:04

+0

我想你是对的。谢谢。 :) – Kjensen 2012-04-12 11:18:38