2012-03-07 13 views
7

मुझे एक समाधान समाधान मिला है, हालांकि मुझे पूरा यकीन है कि कम संसाधन-गहन विधि है क्योंकि वर्तमान समाधान में समूह के सदस्य को प्राप्त करने के लिए एक प्रश्न करना और फिर एक प्रश्न प्रत्येक उपयोगकर्ता की जानकारी प्राप्त करें।उन उपयोगकर्ताओं को प्राप्त करें जो 'सदस्य' समूह हैं

यहाँ कोड मैं है:

DirectoryEntry root = new DirectoryEntry("LDAP://server:port"); 
DirectorySearcher searcher = new DirectorySearcher(root); 
searcher.Filter = "(&(ObjectClass=Group)(CN=foo-group))"; 

var members = (IEnumerable)searcher.FindOne() 
       .GetDirectoryEntry() 
       .Invoke("members"); 

Dictionary<string , string> results = new Dictionary<string , string>(); 

foreach(object member in members) { 
    DirectoryEntry de = new DirectoryEntry(member); 
    results.Add(de.Properties[ "SAMAccountname" ][ 0 ].ToString(), de.Properties[ "cn" ][ 0 ].ToString()); 
} 

आदर्श रूप में मैं हर उपयोगकर्ता है कि एक समूह के सदस्य हैं पाने के लिए किसी एक क्वेरी करने में सक्षम होना चाहते हैं, लोड करने के लिए और उसके बाद प्रदर्शन गुण फिल्टर उन्हें। तो

DirectoryEntry root = new DirectoryEntry("LDAP://server:port"); 
DirectorySearcher searcher = new DirectorySearcher(root); 
searcher.PropertiesToLoad.Add("cn"); 
searcher.PropertiesToLoad.Add("SAMAccountname"); 
searcher.Filter = "(&(ObjectClass=user)(memberof=foo-group))"; 

foreach(var user in searcher.FindAll()) { 
    //do whatever... 
} 

दुर्भाग्य से, यह किसी कारण से काम नहीं करता है।

आपकी मदद के लिए धन्यवाद,

उत्तर

9

:

var context = new PrincipalContext(ContextType.Domain, "YOUR_DOMAIN_NAME"); 
using (var searcher = new PrincipalSearcher()) 
{ 
    var groupName = "YourGroup"; 
    var sp = new GroupPrincipal(context, groupName); 
    searcher.QueryFilter = sp; 
    var group = searcher.FindOne() as GroupPrincipal; 

    if (group == null) 
     Console.WriteLine("Invalid Group Name: {0}", groupName); 

    foreach (var f in group.GetMembers()) 
    { 
     var principal = f as UserPrincipal; 

     if (principal == null || string.IsNullOrEmpty(principal.Name)) 
      continue; 

     Console.WriteLine("{0}", principal.Name); 
    } 
} 

मैं कुछ VB कोड है कि 'है यह पुराना तरीका भी करेगा, लेकिन यह खाता प्रबंधन के साथ निश्चित रूप से सरल है।


यहाँ VB कोड मैं की बात कर रहा था (फिर से यह बहुत नहीं है लेकिन यह कार्यात्मक है):

Public Function GetUsersByGroup(de As DirectoryEntry, groupName As String) As IEnumerable(Of DirectoryEntry) 
    Dim userList As New List(Of DirectoryEntry) 
    Dim group As DirectoryEntry = GetGroup(de, groupName) 

    If group Is Nothing Then Return Nothing 

    For Each user In GetUsers(de) 
     If IsUserInGroup(user, group) Then 
      userList.Add(user) 
     End If 
    Next 

    Return userList 
End Function 

Public Function GetGroup(de As DirectoryEntry, groupName As String) As DirectoryEntry 
    Dim deSearch As New DirectorySearcher(de) 

    deSearch.Filter = "(&(objectClass=group)(SAMAccountName=" & groupName & "))" 

    Dim result As SearchResult = deSearch.FindOne() 

    If result Is Nothing Then 
     Return Nothing 
    End If 

    Return result.GetDirectoryEntry() 
End Function 

Public Function GetUsers(de As DirectoryEntry) As IEnumerable(Of DirectoryEntry) 
    Dim deSearch As New DirectorySearcher(de) 
    Dim userList As New List(Of DirectoryEntry) 

    deSearch.Filter = "(&(objectClass=person))" 

    For Each user In deSearch.FindAll() 
     userList.Add(user.GetDirectoryEntry()) 
    Next 

    Return userList 
End Function 

Public Function IsUserInGroup(user As DirectoryEntry, group As DirectoryEntry) As Boolean 
    Dim memberValues = user.Properties("memberOf") 

    If memberValues Is Nothing OrElse memberValues.Count = 0 Then Return False 

    For Each g In memberValues.Value 
     If g = group.Properties("distinguishedName").Value.ToString() Then 
      Return True 
     End If 
    Next 

    Return False 
End Function 

और उपयोग:

Dim entries = New DirectoryEntry("LDAP://...") 
Dim userList As IEnumerable(Of DirectoryEntry) = GetUsersByGroup(entries, "GroupName") 
+0

है, यह अच्छी तरह से काम कर रहा है, धन्यवाद। हालांकि इस और दूसरी विधि के बीच क्या अंतर है। क्या कोई तरीका है कि मैं सर्वर को इसकी खोज निर्दिष्ट कर सकता हूं। मैं यह सुनिश्चित करना चाहता हूं कि मैं प्रदर्शित होने वाले किसी अन्य डेटा के साथ कोई विसंगति नहीं है। –

+0

मुझे 100% यकीन नहीं है कि यह कवर के तहत क्या कर रहा है लेकिन मैंने बिना किसी समस्या या विसंगतियों के उन्हें मिश्रित किया है। –

+0

क्या आपको पता है कि यह डोमेन को उचित एलडीएपी सर्वर से कैसे जोड़ता है? –

2

यदि आप इसे HERE आप निम्न कर सकते हैं: आप System.DirectoryServices.AccountManagement उपयोग कर सकते हैं

DirectoryEntry group = new DirectoryEntry("LDAP://CN=foo-group,DC=Cmp,DC=COM"); 
foreach(object dn in group.Properties["member"]) 
    //do whatever 
+1

जो मेरे लिए काम नहीं करेगा क्योंकि मेरे पास पूरा पथ नहीं है, मेरे पास केवल सीएन –

2
using System.DirectoryServices; 

DirectoryEntry objEntry = DirectoryEntry(Ldapserver, userid, password); 
DirectorySearcher personSearcher = new DirectorySearcher(objEntry); 
personSearcher.Filter = string.Format("(SAMAccountName={0}", username); 
SearchResult result = personSearcher.FindOne(); 

if(result != null) 
{ 
    DirectoryEntry personEntry = result.GetDirectoryEntry(); 
    PropertyValueCollection groups = personEntry.Properties["memberOf"]; 
    foreach(string g in groups) 
    { 
     Console.WriteLine(g); // will write group name 
    } 
} 

मैं मूल रूप से इस्तेमाल किया आपके द्वारा पोस्ट की गई एक विधि के समान एक विधि और मेरी पूरी कंपनी के एडी के माध्यम से चलाने में लगभग 12 मिनट लग गए और परिणाम प्राप्त हुए। इस विधि में स्विच करने के बाद, इसमें लगभग 2 मिनट लगते हैं। आपको ldapserver पते का उपयोग करने की आवश्यकता होगी जहां मैंने ldapserver और उपयोगकर्ता आईडी और पासवर्ड भी लिखा था और उपयोगकर्ता जिस व्यक्ति को आप देख रहे हैं उसके लिए उपयोगकर्ता नाम SAMAccountName है।

संबंधित मुद्दे