using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.DirectoryServices; namespace ADTest {
public class Form1 : System.Windows.Forms.Form { private System.ComponentModel.Container components = null; private System.Windows.Forms.Button btnConnect; private System.Windows.Forms.TextBox txtUserName; private System.Windows.Forms.Button btnGetUserDetails; private System.Windows.Forms.ListBox lstGroups; private System.Windows.Forms.Button btnGetADGroups; private System.Windows.Forms.ListBox lstGroupUsers; private System.Windows.Forms.Label lblGroupNames; private System.Windows.Forms.Label lblUserName; private System.Windows.Forms.Label lblGroupUsers; private ArrayList groupList; private ADUtilities adUtilities = null; private bool isConnected = false; public Form1() { InitializeComponent(); } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.btnConnect = new System.Windows.Forms.Button(); this.txtUserName = new System.Windows.Forms.TextBox(); this.btnGetUserDetails = new System.Windows.Forms.Button(); this.lstGroups = new System.Windows.Forms.ListBox(); this.btnGetADGroups = new System.Windows.Forms.Button(); this.lstGroupUsers = new System.Windows.Forms.ListBox(); this.lblGroupNames = new System.Windows.Forms.Label(); this.lblUserName = new System.Windows.Forms.Label(); this.lblGroupUsers = new System.Windows.Forms.Label(); this.SuspendLayout(); // // btnConnect // this.btnConnect.Location = new System.Drawing.Point(1040, 56); this.btnConnect.Name = "btnConnect"; this.btnConnect.Size = new System.Drawing.Size(96, 32); this.btnConnect.TabIndex = 0; this.btnConnect.Text = "Not Connected"; this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click); // // txtUserName // this.txtUserName.Location = new System.Drawing.Point(112, 56); this.txtUserName.Name = "txtUserName"; this.txtUserName.Size = new System.Drawing.Size(256, 20); this.txtUserName.TabIndex = 1; this.txtUserName.Text = ""; // // btnGetUserDetails // this.btnGetUserDetails.Location = new System.Drawing.Point(112, 104); this.btnGetUserDetails.Name = "btnGetUserDetails"; this.btnGetUserDetails.Size = new System.Drawing.Size(112, 32); this.btnGetUserDetails.TabIndex = 2; this.btnGetUserDetails.Text = "Get Domain Name"; this.btnGetUserDetails.Click += new System.EventHandler(this.btnGetUserDetails_Click); // // lstGroups // this.lstGroups.Location = new System.Drawing.Point(448, 56); this.lstGroups.Name = "lstGroups"; this.lstGroups.Size = new System.Drawing.Size(256, 108); this.lstGroups.TabIndex = 3; this.lstGroups.DoubleClick += new System.EventHandler(this.lstGroups_DoubleClick); // // btnGetADGroups // this.btnGetADGroups.Location = new System.Drawing.Point(448, 192); this.btnGetADGroups.Name = "btnGetADGroups"; this.btnGetADGroups.Size = new System.Drawing.Size(96, 40); this.btnGetADGroups.TabIndex = 4; this.btnGetADGroups.Text = "Get Groups"; this.btnGetADGroups.Click += new System.EventHandler(this.btnGetADGroups_Click); // // lstGroupUsers // this.lstGroupUsers.Location = new System.Drawing.Point(768, 56); this.lstGroupUsers.Name = "lstGroupUsers"; this.lstGroupUsers.Size = new System.Drawing.Size(224, 108); this.lstGroupUsers.TabIndex = 5; // // lblGroupNames // this.lblGroupNames.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.lblGroupNames.Location = new System.Drawing.Point(456, 16); this.lblGroupNames.Name = "lblGroupNames"; this.lblGroupNames.Size = new System.Drawing.Size(152, 24); this.lblGroupNames.TabIndex = 6; this.lblGroupNames.Text = "Active Directory Groups"; // // lblUserName // this.lblUserName.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.lblUserName.Location = new System.Drawing.Point(16, 56); this.lblUserName.Name = "lblUserName"; this.lblUserName.Size = new System.Drawing.Size(72, 24); this.lblUserName.TabIndex = 7; this.lblUserName.Text = "User Name:"; // // lblGroupUsers // this.lblGroupUsers.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.lblGroupUsers.Location = new System.Drawing.Point(768, 16); this.lblGroupUsers.Name = "lblGroupUsers"; this.lblGroupUsers.Size = new System.Drawing.Size(152, 24); this.lblGroupUsers.TabIndex = 8; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(1232, 266); this.Controls.Add(this.lblGroupUsers); this.Controls.Add(this.lblUserName); this.Controls.Add(this.lblGroupNames); this.Controls.Add(this.lstGroupUsers); this.Controls.Add(this.btnGetADGroups); this.Controls.Add(this.lstGroups); this.Controls.Add(this.btnGetUserDetails); this.Controls.Add(this.txtUserName); this.Controls.Add(this.btnConnect); this.Name = "Form1"; this.Text = "Active Directory"; this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); }
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.DirectoryServices; namespace ADTest {
public class Form1 : System.Windows.Forms.Form { private System.ComponentModel.Container components = null; private System.Windows.Forms.Button btnConnect; private System.Windows.Forms.TextBox txtUserName; private System.Windows.Forms.Button btnGetUserDetails; private System.Windows.Forms.ListBox lstGroups; private System.Windows.Forms.Button btnGetADGroups; private System.Windows.Forms.ListBox lstGroupUsers; private System.Windows.Forms.Label lblGroupNames; private System.Windows.Forms.Label lblUserName; private System.Windows.Forms.Label lblGroupUsers; private ArrayList groupList; private ADUtilities adUtilities = null; private bool isConnected = false; public Form1() { InitializeComponent(); } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.btnConnect = new System.Windows.Forms.Button(); this.txtUserName = new System.Windows.Forms.TextBox(); this.btnGetUserDetails = new System.Windows.Forms.Button(); this.lstGroups = new System.Windows.Forms.ListBox(); this.btnGetADGroups = new System.Windows.Forms.Button(); this.lstGroupUsers = new System.Windows.Forms.ListBox(); this.lblGroupNames = new System.Windows.Forms.Label(); this.lblUserName = new System.Windows.Forms.Label(); this.lblGroupUsers = new System.Windows.Forms.Label(); this.SuspendLayout(); // // btnConnect // this.btnConnect.Location = new System.Drawing.Point(1040, 56); this.btnConnect.Name = "btnConnect"; this.btnConnect.Size = new System.Drawing.Size(96, 32); this.btnConnect.TabIndex = 0; this.btnConnect.Text = "Not Connected"; this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click); // // txtUserName // this.txtUserName.Location = new System.Drawing.Point(112, 56); this.txtUserName.Name = "txtUserName"; this.txtUserName.Size = new System.Drawing.Size(256, 20); this.txtUserName.TabIndex = 1; this.txtUserName.Text = ""; // // btnGetUserDetails // this.btnGetUserDetails.Location = new System.Drawing.Point(112, 104); this.btnGetUserDetails.Name = "btnGetUserDetails"; this.btnGetUserDetails.Size = new System.Drawing.Size(112, 32); this.btnGetUserDetails.TabIndex = 2; this.btnGetUserDetails.Text = "Get Domain Name"; this.btnGetUserDetails.Click += new System.EventHandler(this.btnGetUserDetails_Click); // // lstGroups // this.lstGroups.Location = new System.Drawing.Point(448, 56); this.lstGroups.Name = "lstGroups"; this.lstGroups.Size = new System.Drawing.Size(256, 108); this.lstGroups.TabIndex = 3; this.lstGroups.DoubleClick += new System.EventHandler(this.lstGroups_DoubleClick); // // btnGetADGroups // this.btnGetADGroups.Location = new System.Drawing.Point(448, 192); this.btnGetADGroups.Name = "btnGetADGroups"; this.btnGetADGroups.Size = new System.Drawing.Size(96, 40); this.btnGetADGroups.TabIndex = 4; this.btnGetADGroups.Text = "Get Groups"; this.btnGetADGroups.Click += new System.EventHandler(this.btnGetADGroups_Click); // // lstGroupUsers // this.lstGroupUsers.Location = new System.Drawing.Point(768, 56); this.lstGroupUsers.Name = "lstGroupUsers"; this.lstGroupUsers.Size = new System.Drawing.Size(224, 108); this.lstGroupUsers.TabIndex = 5; // // lblGroupNames // this.lblGroupNames.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.lblGroupNames.Location = new System.Drawing.Point(456, 16); this.lblGroupNames.Name = "lblGroupNames"; this.lblGroupNames.Size = new System.Drawing.Size(152, 24); this.lblGroupNames.TabIndex = 6; this.lblGroupNames.Text = "Active Directory Groups"; // // lblUserName // this.lblUserName.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.lblUserName.Location = new System.Drawing.Point(16, 56); this.lblUserName.Name = "lblUserName"; this.lblUserName.Size = new System.Drawing.Size(72, 24); this.lblUserName.TabIndex = 7; this.lblUserName.Text = "User Name:"; // // lblGroupUsers // this.lblGroupUsers.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.lblGroupUsers.Location = new System.Drawing.Point(768, 16); this.lblGroupUsers.Name = "lblGroupUsers"; this.lblGroupUsers.Size = new System.Drawing.Size(152, 24); this.lblGroupUsers.TabIndex = 8; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(1232, 266); this.Controls.Add(this.lblGroupUsers); this.Controls.Add(this.lblUserName); this.Controls.Add(this.lblGroupNames); this.Controls.Add(this.lstGroupUsers); this.Controls.Add(this.btnGetADGroups); this.Controls.Add(this.lstGroups); this.Controls.Add(this.btnGetUserDetails); this.Controls.Add(this.txtUserName); this.Controls.Add(this.btnConnect); this.Name = "Form1"; this.Text = "Active Directory"; this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); }
你google一下结果一大片,用加密cookies
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.DirectoryServices; namespace ADTest
{
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Button btnConnect;
private System.Windows.Forms.TextBox txtUserName;
private System.Windows.Forms.Button btnGetUserDetails;
private System.Windows.Forms.ListBox lstGroups;
private System.Windows.Forms.Button btnGetADGroups;
private System.Windows.Forms.ListBox lstGroupUsers;
private System.Windows.Forms.Label lblGroupNames;
private System.Windows.Forms.Label lblUserName;
private System.Windows.Forms.Label lblGroupUsers; private ArrayList groupList;
private ADUtilities adUtilities = null;
private bool isConnected = false;
public Form1()
{
InitializeComponent();
} /// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnConnect = new System.Windows.Forms.Button();
this.txtUserName = new System.Windows.Forms.TextBox();
this.btnGetUserDetails = new System.Windows.Forms.Button();
this.lstGroups = new System.Windows.Forms.ListBox();
this.btnGetADGroups = new System.Windows.Forms.Button();
this.lstGroupUsers = new System.Windows.Forms.ListBox();
this.lblGroupNames = new System.Windows.Forms.Label();
this.lblUserName = new System.Windows.Forms.Label();
this.lblGroupUsers = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// btnConnect
//
this.btnConnect.Location = new System.Drawing.Point(1040, 56);
this.btnConnect.Name = "btnConnect";
this.btnConnect.Size = new System.Drawing.Size(96, 32);
this.btnConnect.TabIndex = 0;
this.btnConnect.Text = "Not Connected";
this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click);
//
// txtUserName
//
this.txtUserName.Location = new System.Drawing.Point(112, 56);
this.txtUserName.Name = "txtUserName";
this.txtUserName.Size = new System.Drawing.Size(256, 20);
this.txtUserName.TabIndex = 1;
this.txtUserName.Text = "";
//
// btnGetUserDetails
//
this.btnGetUserDetails.Location = new System.Drawing.Point(112, 104);
this.btnGetUserDetails.Name = "btnGetUserDetails";
this.btnGetUserDetails.Size = new System.Drawing.Size(112, 32);
this.btnGetUserDetails.TabIndex = 2;
this.btnGetUserDetails.Text = "Get Domain Name";
this.btnGetUserDetails.Click += new System.EventHandler(this.btnGetUserDetails_Click);
//
// lstGroups
//
this.lstGroups.Location = new System.Drawing.Point(448, 56);
this.lstGroups.Name = "lstGroups";
this.lstGroups.Size = new System.Drawing.Size(256, 108);
this.lstGroups.TabIndex = 3;
this.lstGroups.DoubleClick += new System.EventHandler(this.lstGroups_DoubleClick);
//
// btnGetADGroups
//
this.btnGetADGroups.Location = new System.Drawing.Point(448, 192);
this.btnGetADGroups.Name = "btnGetADGroups";
this.btnGetADGroups.Size = new System.Drawing.Size(96, 40);
this.btnGetADGroups.TabIndex = 4;
this.btnGetADGroups.Text = "Get Groups";
this.btnGetADGroups.Click += new System.EventHandler(this.btnGetADGroups_Click);
//
// lstGroupUsers
//
this.lstGroupUsers.Location = new System.Drawing.Point(768, 56);
this.lstGroupUsers.Name = "lstGroupUsers";
this.lstGroupUsers.Size = new System.Drawing.Size(224, 108);
this.lstGroupUsers.TabIndex = 5;
//
// lblGroupNames
//
this.lblGroupNames.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblGroupNames.Location = new System.Drawing.Point(456, 16);
this.lblGroupNames.Name = "lblGroupNames";
this.lblGroupNames.Size = new System.Drawing.Size(152, 24);
this.lblGroupNames.TabIndex = 6;
this.lblGroupNames.Text = "Active Directory Groups";
//
// lblUserName
//
this.lblUserName.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblUserName.Location = new System.Drawing.Point(16, 56);
this.lblUserName.Name = "lblUserName";
this.lblUserName.Size = new System.Drawing.Size(72, 24);
this.lblUserName.TabIndex = 7;
this.lblUserName.Text = "User Name:";
//
// lblGroupUsers
//
this.lblGroupUsers.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblGroupUsers.Location = new System.Drawing.Point(768, 16);
this.lblGroupUsers.Name = "lblGroupUsers";
this.lblGroupUsers.Size = new System.Drawing.Size(152, 24);
this.lblGroupUsers.TabIndex = 8;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(1232, 266);
this.Controls.Add(this.lblGroupUsers);
this.Controls.Add(this.lblUserName);
this.Controls.Add(this.lblGroupNames);
this.Controls.Add(this.lstGroupUsers);
this.Controls.Add(this.btnGetADGroups);
this.Controls.Add(this.lstGroups);
this.Controls.Add(this.btnGetUserDetails);
this.Controls.Add(this.txtUserName);
this.Controls.Add(this.btnConnect);
this.Name = "Form1";
this.Text = "Active Directory";
this.ResumeLayout(false); }
#endregion /// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void btnConnect_Click(object sender, System.EventArgs e)
{
if(!isConnected)
{
try
{
adUtilities = new ADUtilities();
isConnected = true;
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
isConnected = false;
} btnConnect.Text = "Connected";
}
}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.DirectoryServices; namespace ADTest
{
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Button btnConnect;
private System.Windows.Forms.TextBox txtUserName;
private System.Windows.Forms.Button btnGetUserDetails;
private System.Windows.Forms.ListBox lstGroups;
private System.Windows.Forms.Button btnGetADGroups;
private System.Windows.Forms.ListBox lstGroupUsers;
private System.Windows.Forms.Label lblGroupNames;
private System.Windows.Forms.Label lblUserName;
private System.Windows.Forms.Label lblGroupUsers; private ArrayList groupList;
private ADUtilities adUtilities = null;
private bool isConnected = false;
public Form1()
{
InitializeComponent();
} /// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnConnect = new System.Windows.Forms.Button();
this.txtUserName = new System.Windows.Forms.TextBox();
this.btnGetUserDetails = new System.Windows.Forms.Button();
this.lstGroups = new System.Windows.Forms.ListBox();
this.btnGetADGroups = new System.Windows.Forms.Button();
this.lstGroupUsers = new System.Windows.Forms.ListBox();
this.lblGroupNames = new System.Windows.Forms.Label();
this.lblUserName = new System.Windows.Forms.Label();
this.lblGroupUsers = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// btnConnect
//
this.btnConnect.Location = new System.Drawing.Point(1040, 56);
this.btnConnect.Name = "btnConnect";
this.btnConnect.Size = new System.Drawing.Size(96, 32);
this.btnConnect.TabIndex = 0;
this.btnConnect.Text = "Not Connected";
this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click);
//
// txtUserName
//
this.txtUserName.Location = new System.Drawing.Point(112, 56);
this.txtUserName.Name = "txtUserName";
this.txtUserName.Size = new System.Drawing.Size(256, 20);
this.txtUserName.TabIndex = 1;
this.txtUserName.Text = "";
//
// btnGetUserDetails
//
this.btnGetUserDetails.Location = new System.Drawing.Point(112, 104);
this.btnGetUserDetails.Name = "btnGetUserDetails";
this.btnGetUserDetails.Size = new System.Drawing.Size(112, 32);
this.btnGetUserDetails.TabIndex = 2;
this.btnGetUserDetails.Text = "Get Domain Name";
this.btnGetUserDetails.Click += new System.EventHandler(this.btnGetUserDetails_Click);
//
// lstGroups
//
this.lstGroups.Location = new System.Drawing.Point(448, 56);
this.lstGroups.Name = "lstGroups";
this.lstGroups.Size = new System.Drawing.Size(256, 108);
this.lstGroups.TabIndex = 3;
this.lstGroups.DoubleClick += new System.EventHandler(this.lstGroups_DoubleClick);
//
// btnGetADGroups
//
this.btnGetADGroups.Location = new System.Drawing.Point(448, 192);
this.btnGetADGroups.Name = "btnGetADGroups";
this.btnGetADGroups.Size = new System.Drawing.Size(96, 40);
this.btnGetADGroups.TabIndex = 4;
this.btnGetADGroups.Text = "Get Groups";
this.btnGetADGroups.Click += new System.EventHandler(this.btnGetADGroups_Click);
//
// lstGroupUsers
//
this.lstGroupUsers.Location = new System.Drawing.Point(768, 56);
this.lstGroupUsers.Name = "lstGroupUsers";
this.lstGroupUsers.Size = new System.Drawing.Size(224, 108);
this.lstGroupUsers.TabIndex = 5;
//
// lblGroupNames
//
this.lblGroupNames.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblGroupNames.Location = new System.Drawing.Point(456, 16);
this.lblGroupNames.Name = "lblGroupNames";
this.lblGroupNames.Size = new System.Drawing.Size(152, 24);
this.lblGroupNames.TabIndex = 6;
this.lblGroupNames.Text = "Active Directory Groups";
//
// lblUserName
//
this.lblUserName.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblUserName.Location = new System.Drawing.Point(16, 56);
this.lblUserName.Name = "lblUserName";
this.lblUserName.Size = new System.Drawing.Size(72, 24);
this.lblUserName.TabIndex = 7;
this.lblUserName.Text = "User Name:";
//
// lblGroupUsers
//
this.lblGroupUsers.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblGroupUsers.Location = new System.Drawing.Point(768, 16);
this.lblGroupUsers.Name = "lblGroupUsers";
this.lblGroupUsers.Size = new System.Drawing.Size(152, 24);
this.lblGroupUsers.TabIndex = 8;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(1232, 266);
this.Controls.Add(this.lblGroupUsers);
this.Controls.Add(this.lblUserName);
this.Controls.Add(this.lblGroupNames);
this.Controls.Add(this.lstGroupUsers);
this.Controls.Add(this.btnGetADGroups);
this.Controls.Add(this.lstGroups);
this.Controls.Add(this.btnGetUserDetails);
this.Controls.Add(this.txtUserName);
this.Controls.Add(this.btnConnect);
this.Name = "Form1";
this.Text = "Active Directory";
this.ResumeLayout(false); }
#endregion /// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void btnConnect_Click(object sender, System.EventArgs e)
{
if(!isConnected)
{
try
{
adUtilities = new ADUtilities();
isConnected = true;
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
isConnected = false;
} btnConnect.Text = "Connected";
}
}
{
if(!isConnected)
{
MessageBox.Show("Not connected to the Active Directory","Active Directory Sample Test");
return;
}
else if(txtUserName.Text.Trim().Equals(String.Empty))
{
MessageBox.Show("User Name cannot be blank","Active Directory Sample Test");
return;
} string domain = String.Empty; try
{
domain = adUtilities.GetUserDomain(txtUserName.Text.Trim());
}
catch(Exception ex)
{
MessageBox.Show("Invalid User","Active Directory Sample Test");
return;
} if(domain !=null && domain != String.Empty)
MessageBox.Show("The Domain Name is "+domain,"Active Directory Sample Test");
else
MessageBox.Show("This User does not exist","Active Directory Sample Test"); } private void btnGetADGroups_Click(object sender, System.EventArgs e)
{
lstGroups.Items.Clear();
groupList = null; if(!isConnected)
{
MessageBox.Show("Please click on the connect button to get connected to the Active Directory","Active Directory Sample Test");
return;
} groupList = adUtilities.GetGroupNames();
for(int i=0; i<groupList.Count; i++)
lstGroups.Items.Add(((DirectoryEntry)groupList[i]).Name);
lstGroups.SelectedIndex = 0;
} private void lstGroups_DoubleClick(object sender, System.EventArgs e)
{
lstGroupUsers.Items.Clear();
string groupName = lstGroups.SelectedItem.ToString();
DirectoryEntry directoryEntry = (DirectoryEntry)groupList[lstGroups.SelectedIndex];
ArrayList groupUsersList = adUtilities.GetUsersInGroup(directoryEntry);
if(groupUsersList.Count > 0)
{
lblGroupUsers.Text = "Displaying Users For "+lstGroups.SelectedItem.ToString();
for(int i=0; i<groupUsersList.Count; i++)
lstGroupUsers.Items.Add(((DirectoryEntry)groupUsersList[i]).Name);
}
}
}
}
非常感谢!