2012-04-12 157 views
11

我想檢查用戶是否在特定的父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

回答

12

好@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

2

這些信息在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

相關問題