簡體   English   中英

有沒有辦法判斷一個帳戶是否屬於某個OU?

[英]Is there a way to tell if an account belongs in a certain OU?

我可以檢查某個帳戶是否是AD組的成員,但是是否可以確定該帳戶是否屬於OU? 我想按OU而不是按AD組搜索,因此我不確定是否可以。 以下是我搜索廣告組的方法。

string myADSPath="LDAP://onecity/CN=Users,DC=onecity,DC=corp,DC=fabrikam,DC=com";  


if (DirectoryEntry.Exists(myADSPath))  
{  
    Console.WriteLine("In the group");  
}  
    else  
{  
    Console.WriteLine("Couldn't get in the group");  
}  

使用此方法,您可以檢查指定的用戶是否在foo/bar OU中:

public bool CheckUserInOU(string userName)
{
    using (var entryPoint = new DirectoryEntry($@"LDAP://onecity/OU=bar,OU=foo,DC=onecity,DC=corp,DC=fabrikam,DC=com"))
    {
        // User and pass for the LDAP query user if needed.
        entryPoint.Username = "YourUsernameHere";
        entryPoint.Password = "YourPasswordHere";

        using (var searcher = new DirectorySearcher(entryPoint))
        {
            searcher.SearchScope = SearchScope.OneLevel;
            searcher.Filter = $"(&(samAccountName={userName})(objectCategory=user))";

            return searcher.FindOne() != null;
        }
    }
}

正如@Damien_The_Unbeliever所說,如果您有用戶,則distinguishedName屬性將包含OU。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM