【问题标题】:Get parent OU of user in Active Directory using C#使用 C# 在 Active Directory 中获取用户的父 OU
【发布时间】:2012-04-12 10:53:55
【问题描述】:

我想检查用户是否在特定的父 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
      • 迈克

empi回答后的解决方案1

根据 empi 给出的信息,我编写了下面的方法来提取 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;
            }
        }
    }

JPBlanc 回答后的解决方案 2

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();
                    }
                }
            }
        }
    }

【问题讨论】:

  • 如果对象的专有名称中有逗号,则不起作用。您需要处理那些被转义的方式,或者使用 JPBlanc 的解决方案 2。

标签: c# active-directory


【解决方案1】:

好的@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);

【讨论】:

    【解决方案2】:

    此信息位于UserPrincipal.DistinguishedName。您应该检查 DistinguishedName 是否以 "," + ou 专有名称结尾(不区分大小写)。但是,您必须知道您正在检查的 ou 的专有名称。

    例如,如果 dn 是:CN=Jeff Smith,OU=Sales,DC=Fabrikam,DC=COM,则表示用户在OU=Sales,DC=Fabrikam,DC=COM ou。

    【讨论】:

    • 这更进一步,谢谢!我现在可以通过字符串破解我的解决方案,但似乎应该有更好的方法。
    • 据我所知,这不是黑客攻击。这就是目录服务的工作方式。如果给你一个文件路径,你应该检查文件是否在某个目录中,你会做同样的事情。
    • 此测试对于嵌套 OU 不正确。例如如果用户是 CN=Jeff,OU=Sales,OU=North...那么您对 ​​OU=North 进行“测试”,您会得到肯定的结果,对吧?
    【解决方案3】:

    这就是我获取特定 AD 用户的专有名称的方式,希望对您有所帮助 :-)

    private static string GetDNOfUser(string user)
    {
        var ctx = new PrincipalContext(ContextType.Domain, Environmentals.Domain, Environmentals.OUPath);
    
        //Creating object for search filter
        UserPrincipal userPrin = new UserPrincipal(ctx)
        {
            //Only getting users with the same name as the input
            Name = user
        };
    
        var searcher = new PrincipalSearcher
        {
            //Applying filter to query
            QueryFilter = userPrin
        };
    
        //Finding the user
        var results = searcher.FindOne();
        searcher.Dispose();
    
        //Return the distinguishedname
        return results.DistinguishedName;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多