Bonjour, voici mon code actuel.
J'ai deux listbox, la première affiche des groupes AD triés.
Sélectionner l'un des groupes doit afficher les utilisateurs qui en sont membres dans la seconde listbox.
Ma question principale porte sur la fin:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Security.Principal; using System.DirectoryServices; namespace WpfApplication1 { /// <summary> /// Logique d'interaction pour MainWindow.xaml /// </summary> public partial class MainWindow : Window { String uid = WindowsIdentity.GetCurrent().Name; String server = "LDAP://192.168.1.20"; String login = "login"; String pass = "pass"; public MainWindow() { InitializeComponent(); label1.Content = "Bonjour " + uid + ", voici les groupes dont vous êtes responsable"; try { DirectoryEntry Ldap = new DirectoryEntry(server, login, pass); DirectorySearcher searcher = new DirectorySearcher(Ldap); searcher.Filter = "(&(objectClass=group)(SAMAccountName=A_*))"; foreach (SearchResult result in searcher.FindAll()) { // On récupère l'entrée trouvée lors de la recherche DirectoryEntry DirEntry = result.GetDirectoryEntry(); //On peut maintenant afficher les informations désirées listBox1.Items.Add(DirEntry.Properties["SAMAccountName"].Value); //Console.WriteLine("Login : " + DirEntry.Properties["SAMAccountName"].Value); //Console.WriteLine("Nom : " + DirEntry.Properties["sn"].Value); //Console.WriteLine("Prénom : " + DirEntry.Properties["givenName"].Value); //Console.WriteLine("Email : " + DirEntry.Properties["mail"].Value); //Console.WriteLine("Tél : " + DirEntry.Properties["TelephoneNumber"].Value); //Console.WriteLine("Description : " + DirEntry.Properties["description"].Value); } } catch (Exception Ex) { MessageBox.Show(Ex.Message); } } private void button1_Click(object sender, RoutedEventArgs e) { button1.Content = "Encore"; } private void button2_Click(object sender, RoutedEventArgs e) { //ActiveDirectoryRights MessageBox.Show(listBox1.SelectedItem.ToString()); } private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e) { listBox2.Items.Clear(); DirectoryEntry Ldap = new DirectoryEntry(server, login, pass); DirectorySearcher searcher = new DirectorySearcher(Ldap); searcher.Filter = "(&(objectClass=group)(SAMAccountName=" + listBox1.SelectedItem.ToString() + "))"; DirectoryEntry group = searcher.FindOne().GetDirectoryEntry(); foreach (String dn in group.Properties["member"]) { //MessageBox.Show(ldp); DirectoryEntry Ldap2 = new DirectoryEntry(server + "/" + dn.ToString(), login, pass); DirectorySearcher searcher2 = new DirectorySearcher(Ldap2); searcher2.Filter = "(objectClass=user)"; DirectoryEntry user = searcher2.FindOne().GetDirectoryEntry(); //La référence d'objet n'est pas définie à une instance d'un objet. String test = (string) Ldap2.InvokeGet("sAMAccountName"); listBox2.Items.Add(test); } } } }
les 3 lignes qui concernent le "searcher2" ne fonctionnent pas, si il y a plus de 2 "dn", le programme pédale dans la semoule.
l'erreur estSi je les enlève ça fonctionne, mais j'obtiens aussi les groupes ce que je ne souhaite pas.La référence d'objet n'est pas définie à une instance d'un objet.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 foreach (String dn in group.Properties["member"]) { //MessageBox.Show(ldp); DirectoryEntry Ldap2 = new DirectoryEntry(server + "/" + dn.ToString(), login, pass); DirectorySearcher searcher2 = new DirectorySearcher(Ldap2); searcher2.Filter = "(objectClass=user)"; DirectoryEntry user = searcher2.FindOne().GetDirectoryEntry(); //La référence d'objet n'est pas définie à une instance d'un objet. String test = (string) Ldap2.InvokeGet("sAMAccountName"); listBox2.Items.Add(test); } }
Partager