微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用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,actual);
}

域名:

>国家OU

>真棒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,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,samAccountName))
            {
                using (DirectoryEntry deUser = user.GetUnderlyingObject() as DirectoryEntry)
                {
                    using (DirectoryEntry deUserContainer = deUser.Parent)
                    {
                        return deUserContainer.Properties["Name"].Value.ToString();
                    }
                }
            }
        }
    }

解决方法

Ok @Empi解决方案正在运行,但是UserPrincipal构建在DirectoryEntry对象上,该对象提供了一个父或容器属性,只需要给出您要查找的对象,而不使用字符串方式.
/* 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);

原文地址:https://www.jb51.cc/csharp/91368.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐