DirectorySearcher.SearchScope : DirectorySearcher « System.DirectoryServices « C# / C Sharp by API

Home
C# / C Sharp by API
1.Microsoft.Win32
2.System
3.System.Collections
4.System.Collections.Generic
5.System.Collections.Specialized
6.System.ComponentModel
7.System.Configuration
8.System.Data
9.System.Data.Common
10.System.Data.Linq
11.System.Data.Odbc
12.System.Data.OleDb
13.System.Data.Sql
14.System.Data.SqlClient
15.System.Diagnostics
16.System.DirectoryServices
17.System.Drawing
18.System.Drawing.Drawing2D
19.System.Drawing.Imaging
20.System.Drawing.Printing
21.System.Drawing.Text
22.System.EnterpriseServices
23.System.Globalization
24.System.IO
25.System.IO.Compression
26.System.IO.IsolatedStorage
27.System.IO.Ports
28.System.Linq
29.System.Management
30.System.Media
31.System.Messaging
32.System.Net
33.System.Net.Mail
34.System.Net.NetworkInformation
35.System.Net.Sockets
36.System.Reflection
37.System.Resources
38.System.Runtime
39.System.Runtime.CompilerServices
40.System.Runtime.InteropServices
41.System.Runtime.Remoting
42.System.Runtime.Remoting.Channels
43.System.Runtime.Remoting.Channels.Http
44.System.Runtime.Remoting.Messaging
45.System.Runtime.Serialization
46.System.Runtime.Serialization.Formatters.Binary
47.System.Runtime.Serialization.Formatters.Soap
48.System.Security
49.System.Security.AccessControl
50.System.Security.Cryptography
51.System.Security.Cryptography.X509Certificates
52.System.Security.Permissions
53.System.Security.Policy
54.System.Security.Principal
55.System.ServiceProcess
56.System.Text
57.System.Text.RegularExpressions
58.System.Threading
59.System.Timers
60.System.Web.Security
61.System.Web.Services
62.System.Windows.Controls
63.System.Windows.Forms
64.System.Xml
65.System.Xml.Linq
66.System.Xml.Schema
67.System.Xml.Serialization
68.System.Xml.XPath
69.System.Xml.Xsl
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp by API » System.DirectoryServices » DirectorySearcher 
DirectorySearcher.SearchScope
 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Text;
using System.DirectoryServices;

public class UserSearchForm : Form {
    private string username;
    private string password;
    private string hostname;
    private string schemaNamingContext;
    private string defaultNamingContext;

    public UserSearchForm() {
        InitializeComponent();
    }
    protected void SetLogonInformation() {
        username = (textBoxUsername.Text == "" null : textBoxUsername.Text);
        password = (textBoxPassword.Text == "" null : textBoxPassword.Text);
        hostname = textBoxHostname.Text;
        if (hostname != ""hostname += "/";
    }

    protected void SetNamingContext() {
        using (DirectoryEntry de = new DirectoryEntry()) {
            string path = "LDAP://" + hostname + "root";
            de.Username = username;
            de.Password = password;
            de.Path = path;

            schemaNamingContext = de.Properties["schemaNamingContext"][0].ToString();
            defaultNamingContext = de.Properties["defaultNamingContext"][0].ToString();
        }
    }

    protected void SetUserProperties(string schemaNamingContext) {
        List<string> properties = new List<string>();
        string[] data = GetSchemaProperties(schemaNamingContext, "User");
        properties.AddRange(GetSchemaProperties(schemaNamingContext, "Organizational-Person"));
        listBoxProperties.Items.Clear();
        foreach (string s in properties) {
            listBoxProperties.Items.Add(s);
        }
    }

    protected string[] GetSchemaProperties(string schemaNamingContext, string objectType) {
        string[] data;
        using (DirectoryEntry de = new DirectoryEntry()) {
            de.Username = username;
            de.Password = password;

            de.Path = "LDAP://" + hostname + "CN=" + objectType + "," + schemaNamingContext;

            PropertyCollection properties = de.Properties;
            PropertyValueCollection values = properties["systemMayContain"];

            data = new String[values.Count];
            values.CopyTo(data, 0);
        }
        return data;
    }

    private void OnLoadProperties(object sender, EventArgs e) {
        SetLogonInformation();
        SetNamingContext();

        SetUserProperties(schemaNamingContext);
    }

    private void OnSearch(object sender, EventArgs e) {
        FillResult();
    }

    protected string[] GetProperties() {
        string[] properties = new string[listBoxProperties.SelectedItems.Count];
        int i = 0;
        foreach (string s in listBoxProperties.SelectedItems) {
            properties[i++= s;
        }
        return properties;
    }

    protected void FillResult() {
        using (DirectoryEntry root = new DirectoryEntry()) {
            root.Username = username;
            root.Password = password;
            root.Path = "LDAP://" + hostname + defaultNamingContext;

            using (DirectorySearcher searcher = new DirectorySearcher()) {
                searcher.SearchRoot = root;
                searcher.SearchScope = SearchScope.Subtree;
                searcher.Filter = textBoxFilter.Text;
                searcher.PropertiesToLoad.AddRange(GetProperties());

                SearchResultCollection results = searcher.FindAll();
                StringBuilder summary = new StringBuilder();
                foreach (SearchResult result in results) {
                    foreach (string propName in result.Properties.PropertyNames) {
                        foreach (string s in result.Properties[propName]) {
                            summary.Append(" " + propName + ": " + s + "\r\n");
                        }
                    }
                    summary.Append("\r\n");
                }
                textBoxResults.Text = summary.ToString();
            }
        }
    }


    private void InitializeComponent() {
        this.splitContainer1 = new System.Windows.Forms.SplitContainer();
        this.buttonSearch = new System.Windows.Forms.Button();
        this.label9 = new System.Windows.Forms.Label();
        this.textBoxFilter = new System.Windows.Forms.TextBox();
        this.label8 = new System.Windows.Forms.Label();
        this.label7 = new System.Windows.Forms.Label();
        this.listBoxProperties = new System.Windows.Forms.ListBox();
        this.label6 = new System.Windows.Forms.Label();
        this.buttonLoadProperties = new System.Windows.Forms.Button();
        this.label5 = new System.Windows.Forms.Label();
        this.groupBox1 = new System.Windows.Forms.GroupBox();
        this.textBoxPassword = new System.Windows.Forms.TextBox();
        this.textBoxUsername = new System.Windows.Forms.TextBox();
        this.label4 = new System.Windows.Forms.Label();
        this.label3 = new System.Windows.Forms.Label();
        this.textBoxHostname = new System.Windows.Forms.TextBox();
        this.label2 = new System.Windows.Forms.Label();
        this.label1 = new System.Windows.Forms.Label();
        this.textBoxResults = new System.Windows.Forms.TextBox();
        this.splitContainer1.Panel1.SuspendLayout();
        this.splitContainer1.Panel2.SuspendLayout();
        this.splitContainer1.SuspendLayout();
        this.groupBox1.SuspendLayout();
        this.SuspendLayout();
        // 
        // splitContainer1
        // 
        this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.splitContainer1.Location = new System.Drawing.Point(00);
        this.splitContainer1.Name = "splitContainer1";
        // 
        // Panel1
        // 
        this.splitContainer1.Panel1.Controls.Add(this.buttonSearch);
        this.splitContainer1.Panel1.Controls.Add(this.label9);
        this.splitContainer1.Panel1.Controls.Add(this.textBoxFilter);
        this.splitContainer1.Panel1.Controls.Add(this.label8);
        this.splitContainer1.Panel1.Controls.Add(this.label7);
        this.splitContainer1.Panel1.Controls.Add(this.listBoxProperties);
        this.splitContainer1.Panel1.Controls.Add(this.label6);
        this.splitContainer1.Panel1.Controls.Add(this.buttonLoadProperties);
        this.splitContainer1.Panel1.Controls.Add(this.label5);
        this.splitContainer1.Panel1.Controls.Add(this.groupBox1);
        this.splitContainer1.Panel1.Controls.Add(this.textBoxHostname);
        this.splitContainer1.Panel1.Controls.Add(this.label2);
        this.splitContainer1.Panel1.Controls.Add(this.label1);
        // 
        // Panel2
        // 
        this.splitContainer1.Panel2.Controls.Add(this.textBoxResults);
        this.splitContainer1.Size = new System.Drawing.Size(721550);
        this.splitContainer1.SplitterDistance = 370;
        this.splitContainer1.TabIndex = 0;
        this.splitContainer1.Text = "splitContainer1";
        // 
        // buttonSearch
        // 
        this.buttonSearch.Location = new System.Drawing.Point(190489);
        this.buttonSearch.Name = "buttonSearch";
        this.buttonSearch.TabIndex = 12;
        this.buttonSearch.Text = "Search";
        this.buttonSearch.Click += new System.EventHandler(this.OnSearch);
        // 
        // label9
        // 
        this.label9.AutoSize = true;
        this.label9.Location = new System.Drawing.Point(22489);
        this.label9.Name = "label9";
        this.label9.Size = new System.Drawing.Size(9814);
        this.label9.TabIndex = 11;
        this.label9.Text = "5. Start the Search";
        // 
        // textBoxFilter
        // 
        this.textBoxFilter.Location = new System.Drawing.Point(190445);
        this.textBoxFilter.Name = "textBoxFilter";
        this.textBoxFilter.TabIndex = 10;
        this.textBoxFilter.Text = "(objectClass=user)";
        // 
        // label8
        // 
        this.label8.AutoSize = true;
        this.label8.Location = new System.Drawing.Point(22452);
        this.label8.Name = "label8";
        this.label8.Size = new System.Drawing.Size(3314);
        this.label8.TabIndex = 9;
        this.label8.Text = "Filter:";
        // 
        // label7
        // 
        this.label7.AutoSize = true;
        this.label7.Location = new System.Drawing.Point(22420);
        this.label7.Name = "label7";
        this.label7.Size = new System.Drawing.Size(12714);
        this.label7.TabIndex = 8;
        this.label7.Text = "4. Enter the LDAP Filter:";
        // 
        // listBoxProperties
        // 
        this.listBoxProperties.FormattingEnabled = true;
        this.listBoxProperties.Location = new System.Drawing.Point(190289);
        this.listBoxProperties.Name = "listBoxProperties";
        this.listBoxProperties.Size = new System.Drawing.Size(12095);
        this.listBoxProperties.TabIndex = 7;
        // 
        // label6
        // 
        this.label6.AutoSize = true;
        this.label6.Location = new System.Drawing.Point(22289);
        this.label6.Name = "label6";
        this.label6.Size = new System.Drawing.Size(12927);
        this.label6.TabIndex = 6;
        this.label6.Text = "3. Choose the Properties \r\nto Display";
        // 
        // buttonLoadProperties
        // 
        this.buttonLoadProperties.Location = new System.Drawing.Point(190238);
        this.buttonLoadProperties.Name = "buttonLoadProperties";
        this.buttonLoadProperties.Size = new System.Drawing.Size(11623);
        this.buttonLoadProperties.TabIndex = 5;
        this.buttonLoadProperties.Text = "Load Properties";
        this.buttonLoadProperties.Click += new System.EventHandler(this.OnLoadProperties);
        // 
        // label5
        // 
        this.label5.AutoSize = true;
        this.label5.Location = new System.Drawing.Point(29238);
        this.label5.Name = "label5";
        this.label5.Size = new System.Drawing.Size(11814);
        this.label5.TabIndex = 4;
        this.label5.Text = "2. Load the Properties:";
        // 
        // groupBox1
        // 
        this.groupBox1.Controls.Add(this.textBoxPassword);
        this.groupBox1.Controls.Add(this.textBoxUsername);
        this.groupBox1.Controls.Add(this.label4);
        this.groupBox1.Controls.Add(this.label3);
        this.groupBox1.Location = new System.Drawing.Point(22115);
        this.groupBox1.Name = "groupBox1";
        this.groupBox1.Size = new System.Drawing.Size(304100);
        this.groupBox1.TabIndex = 3;
        this.groupBox1.TabStop = false;
        this.groupBox1.Text = "Logon [optional]";
        // 
        // textBoxPassword
        // 
        this.textBoxPassword.Location = new System.Drawing.Point(16860);
        this.textBoxPassword.Name = "textBoxPassword";
        this.textBoxPassword.PasswordChar = '*';
        this.textBoxPassword.Size = new System.Drawing.Size(11620);
        this.textBoxPassword.TabIndex = 3;
        // 
        // textBoxUsername
        // 
        this.textBoxUsername.Location = new System.Drawing.Point(16823);
        this.textBoxUsername.Name = "textBoxUsername";
        this.textBoxUsername.Size = new System.Drawing.Size(11620);
        this.textBoxUsername.TabIndex = 2;
        // 
        // label4
        // 
        this.label4.AutoSize = true;
        this.label4.Location = new System.Drawing.Point(760);
        this.label4.Name = "label4";
        this.label4.Size = new System.Drawing.Size(5714);
        this.label4.TabIndex = 1;
        this.label4.Text = "Password:";
        // 
        // label3
        // 
        this.label3.AutoSize = true;
        this.label3.Location = new System.Drawing.Point(729);
        this.label3.Name = "label3";
        this.label3.Size = new System.Drawing.Size(6014);
        this.label3.TabIndex = 0;
        this.label3.Text = "Username:";
        // 
        // textBoxHostname
        // 
        this.textBoxHostname.Location = new System.Drawing.Point(19070);
        this.textBoxHostname.Name = "textBoxHostname";
        this.textBoxHostname.Size = new System.Drawing.Size(13620);
        this.textBoxHostname.TabIndex = 2;
        // 
        // label2
        // 
        this.label2.AutoSize = true;
        this.label2.Location = new System.Drawing.Point(2270);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(14514);
        this.label2.TabIndex = 1;
        this.label2.Text = "Domain Controller [optional]";
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(2231);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(25414);
        this.label1.TabIndex = 0;
        this.label1.Text = "1. Enter Domain Controller and Logon Information";
        // 
        // textBoxResults
        // 
        this.textBoxResults.Dock = System.Windows.Forms.DockStyle.Fill;
        this.textBoxResults.Location = new System.Drawing.Point(00);
        this.textBoxResults.Multiline = true;
        this.textBoxResults.Name = "textBoxResults";
        this.textBoxResults.Size = new System.Drawing.Size(347550);
        this.textBoxResults.TabIndex = 0;
        // 
        // UserSearchForm
        // 
        this.AutoScaleBaseSize = new System.Drawing.Size(513);
        this.ClientSize = new System.Drawing.Size(721550);
        this.Controls.Add(this.splitContainer1);
        this.Name = "UserSearchForm";
        this.Text = "User Search";
        this.splitContainer1.Panel1.ResumeLayout(false);
        this.splitContainer1.Panel1.PerformLayout();
        this.splitContainer1.Panel2.ResumeLayout(false);
        this.splitContainer1.Panel2.PerformLayout();
        this.splitContainer1.ResumeLayout(false);
        this.groupBox1.ResumeLayout(false);
        this.groupBox1.PerformLayout();
        this.ResumeLayout(false);
    }
    private System.Windows.Forms.SplitContainer splitContainer1;
    private System.Windows.Forms.TextBox textBoxHostname;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.GroupBox groupBox1;
    private System.Windows.Forms.TextBox textBoxUsername;
    private System.Windows.Forms.Label label4;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.TextBox textBoxPassword;
    private System.Windows.Forms.Button buttonLoadProperties;
    private System.Windows.Forms.Label label5;
    private System.Windows.Forms.Label label6;
    private System.Windows.Forms.ListBox listBoxProperties;
    private System.Windows.Forms.TextBox textBoxFilter;
    private System.Windows.Forms.Label label8;
    private System.Windows.Forms.Label label7;
    private System.Windows.Forms.Button buttonSearch;
    private System.Windows.Forms.Label label9;
    private System.Windows.Forms.TextBox textBoxResults;


    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.Run(new UserSearchForm());
    }
}

   
  
Related examples in the same category
1.new DirectorySearcher()
2.DirectorySearcher.PropertiesToLoad
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.