Form1.cs
上传用户:lxycoco
上传日期:2022-07-21
资源大小:38457k
文件大小:15k
源码类别:

C#编程

开发平台:

Others

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.DirectoryServices;
  8. using DS = System.DirectoryServices;
  9. using System.Collections.Specialized;
  10. using System.Text;
  11. namespace UserSearch
  12. {
  13. /// <summary>
  14. /// Summary description for Form1.
  15. /// </summary>
  16. public class Form1 : System.Windows.Forms.Form
  17. {
  18.       private System.Windows.Forms.Label label1;
  19.       private System.Windows.Forms.Label label2;
  20.       private System.Windows.Forms.TextBox textBoxHostname;
  21.       private System.Windows.Forms.TextBox textBoxUsername;
  22.       private System.Windows.Forms.Label label3;
  23.       private System.Windows.Forms.TextBox textBoxPassword;
  24.       private System.Windows.Forms.GroupBox groupBox1;
  25.       private System.Windows.Forms.Label label4;
  26.       private System.Windows.Forms.ListBox listBoxProperties;
  27.       private System.Windows.Forms.Label label5;
  28.       private System.Windows.Forms.Label label6;
  29.       private System.Windows.Forms.Label label7;
  30.       private System.Windows.Forms.Label label8;
  31.       private System.Windows.Forms.Label label9;
  32.       private System.Windows.Forms.TextBox textBoxFilter;
  33.       private System.Windows.Forms.Button buttonLoadProperties;
  34.       private System.Windows.Forms.Button buttonSearch;
  35.       private System.Windows.Forms.TextBox textBoxResults;
  36. /// <summary>
  37. /// Required designer variable.
  38. /// </summary>
  39. private System.ComponentModel.Container components = null;
  40. public Form1()
  41. {
  42. //
  43. // Required for Windows Form Designer support
  44. //
  45. InitializeComponent();
  46. //
  47. // TODO: Add any constructor code after InitializeComponent call
  48. //
  49. }
  50. /// <summary>
  51. /// Clean up any resources being used.
  52. /// </summary>
  53. protected override void Dispose( bool disposing )
  54. {
  55. if( disposing )
  56. {
  57. if (components != null) 
  58. {
  59. components.Dispose();
  60. }
  61. }
  62. base.Dispose( disposing );
  63. }
  64. #region Windows Form Designer generated code
  65. /// <summary>
  66. /// Required method for Designer support - do not modify
  67. /// the contents of this method with the code editor.
  68. /// </summary>
  69. private void InitializeComponent()
  70. {
  71. this.textBoxPassword = new System.Windows.Forms.TextBox();
  72. this.label8 = new System.Windows.Forms.Label();
  73. this.textBoxFilter = new System.Windows.Forms.TextBox();
  74. this.label4 = new System.Windows.Forms.Label();
  75. this.label5 = new System.Windows.Forms.Label();
  76. this.textBoxUsername = new System.Windows.Forms.TextBox();
  77. this.label7 = new System.Windows.Forms.Label();
  78. this.label1 = new System.Windows.Forms.Label();
  79. this.listBoxProperties = new System.Windows.Forms.ListBox();
  80. this.label3 = new System.Windows.Forms.Label();
  81. this.label9 = new System.Windows.Forms.Label();
  82. this.label6 = new System.Windows.Forms.Label();
  83. this.label2 = new System.Windows.Forms.Label();
  84. this.buttonLoadProperties = new System.Windows.Forms.Button();
  85. this.textBoxResults = new System.Windows.Forms.TextBox();
  86. this.groupBox1 = new System.Windows.Forms.GroupBox();
  87. this.buttonSearch = new System.Windows.Forms.Button();
  88. this.textBoxHostname = new System.Windows.Forms.TextBox();
  89. this.groupBox1.SuspendLayout();
  90. this.SuspendLayout();
  91. // 
  92. // textBoxPassword
  93. // 
  94. this.textBoxPassword.Location = new System.Drawing.Point(152, 56);
  95. this.textBoxPassword.Name = "textBoxPassword";
  96. this.textBoxPassword.PasswordChar = '*';
  97. this.textBoxPassword.Size = new System.Drawing.Size(120, 20);
  98. this.textBoxPassword.TabIndex = 1;
  99. this.textBoxPassword.Text = "";
  100. // 
  101. // label8
  102. // 
  103. this.label8.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
  104. this.label8.ForeColor = System.Drawing.SystemColors.HotTrack;
  105. this.label8.Location = new System.Drawing.Point(16, 352);
  106. this.label8.Name = "label8";
  107. this.label8.Size = new System.Drawing.Size(200, 23);
  108. this.label8.TabIndex = 7;
  109. this.label8.Text = "4. Enter the LDAP Filter";
  110. // 
  111. // textBoxFilter
  112. // 
  113. this.textBoxFilter.Location = new System.Drawing.Point(128, 384);
  114. this.textBoxFilter.Name = "textBoxFilter";
  115. this.textBoxFilter.Size = new System.Drawing.Size(200, 20);
  116. this.textBoxFilter.TabIndex = 4;
  117. this.textBoxFilter.Text = "(objectClass=user)";
  118. // 
  119. // label4
  120. // 
  121. this.label4.Location = new System.Drawing.Point(16, 384);
  122. this.label4.Name = "label4";
  123. this.label4.Size = new System.Drawing.Size(72, 23);
  124. this.label4.TabIndex = 3;
  125. this.label4.Text = "Filter:";
  126. // 
  127. // label5
  128. // 
  129. this.label5.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
  130. this.label5.ForeColor = System.Drawing.SystemColors.HotTrack;
  131. this.label5.Location = new System.Drawing.Point(16, 8);
  132. this.label5.Name = "label5";
  133. this.label5.Size = new System.Drawing.Size(288, 23);
  134. this.label5.TabIndex = 7;
  135. this.label5.Text = "1. Enter Domain Controller and Logon Information";
  136. // 
  137. // textBoxUsername
  138. // 
  139. this.textBoxUsername.Location = new System.Drawing.Point(152, 24);
  140. this.textBoxUsername.Name = "textBoxUsername";
  141. this.textBoxUsername.Size = new System.Drawing.Size(120, 20);
  142. this.textBoxUsername.TabIndex = 1;
  143. this.textBoxUsername.Text = "";
  144. // 
  145. // label7
  146. // 
  147. this.label7.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
  148. this.label7.ForeColor = System.Drawing.SystemColors.HotTrack;
  149. this.label7.Location = new System.Drawing.Point(16, 256);
  150. this.label7.Name = "label7";
  151. this.label7.Size = new System.Drawing.Size(104, 64);
  152. this.label7.TabIndex = 7;
  153. this.label7.Text = "3. Choose the Properties to Display";
  154. // 
  155. // label1
  156. // 
  157. this.label1.Location = new System.Drawing.Point(24, 48);
  158. this.label1.Name = "label1";
  159. this.label1.Size = new System.Drawing.Size(144, 24);
  160. this.label1.TabIndex = 0;
  161. this.label1.Text = "Domain Contoller [optional]:";
  162. // 
  163. // listBoxProperties
  164. // 
  165. this.listBoxProperties.Location = new System.Drawing.Point(144, 256);
  166. this.listBoxProperties.Name = "listBoxProperties";
  167. this.listBoxProperties.SelectionMode = System.Windows.Forms.SelectionMode.MultiSimple;
  168. this.listBoxProperties.Size = new System.Drawing.Size(184, 82);
  169. this.listBoxProperties.TabIndex = 6;
  170. // 
  171. // label3
  172. // 
  173. this.label3.Location = new System.Drawing.Point(8, 56);
  174. this.label3.Name = "label3";
  175. this.label3.Size = new System.Drawing.Size(96, 24);
  176. this.label3.TabIndex = 0;
  177. this.label3.Text = "Password:";
  178. // 
  179. // label9
  180. // 
  181. this.label9.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
  182. this.label9.ForeColor = System.Drawing.SystemColors.HotTrack;
  183. this.label9.Location = new System.Drawing.Point(16, 432);
  184. this.label9.Name = "label9";
  185. this.label9.Size = new System.Drawing.Size(128, 23);
  186. this.label9.TabIndex = 7;
  187. this.label9.Text = "5. Start the Search";
  188. // 
  189. // label6
  190. // 
  191. this.label6.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
  192. this.label6.ForeColor = System.Drawing.SystemColors.HotTrack;
  193. this.label6.Location = new System.Drawing.Point(16, 192);
  194. this.label6.Name = "label6";
  195. this.label6.Size = new System.Drawing.Size(144, 23);
  196. this.label6.TabIndex = 7;
  197. this.label6.Text = "2. Load the Properties";
  198. // 
  199. // label2
  200. // 
  201. this.label2.Location = new System.Drawing.Point(8, 24);
  202. this.label2.Name = "label2";
  203. this.label2.Size = new System.Drawing.Size(120, 24);
  204. this.label2.TabIndex = 0;
  205. this.label2.Text = "Username:";
  206. // 
  207. // buttonLoadProperties
  208. // 
  209. this.buttonLoadProperties.Location = new System.Drawing.Point(192, 192);
  210. this.buttonLoadProperties.Name = "buttonLoadProperties";
  211. this.buttonLoadProperties.Size = new System.Drawing.Size(112, 32);
  212. this.buttonLoadProperties.TabIndex = 5;
  213. this.buttonLoadProperties.Text = "Load Properties";
  214. this.buttonLoadProperties.Click += new System.EventHandler(this.buttonLoadProperties_Click);
  215. // 
  216. // textBoxResults
  217. // 
  218. this.textBoxResults.Location = new System.Drawing.Point(376, 16);
  219. this.textBoxResults.Multiline = true;
  220. this.textBoxResults.Name = "textBoxResults";
  221. this.textBoxResults.ReadOnly = true;
  222. this.textBoxResults.ScrollBars = System.Windows.Forms.ScrollBars.Both;
  223. this.textBoxResults.Size = new System.Drawing.Size(344, 464);
  224. this.textBoxResults.TabIndex = 9;
  225. this.textBoxResults.Text = "";
  226. // 
  227. // groupBox1
  228. // 
  229. this.groupBox1.Controls.Add(this.textBoxUsername);
  230. this.groupBox1.Controls.Add(this.textBoxPassword);
  231. this.groupBox1.Controls.Add(this.label2);
  232. this.groupBox1.Controls.Add(this.label3);
  233. this.groupBox1.Location = new System.Drawing.Point(24, 80);
  234. this.groupBox1.Name = "groupBox1";
  235. this.groupBox1.Size = new System.Drawing.Size(328, 96);
  236. this.groupBox1.TabIndex = 2;
  237. this.groupBox1.TabStop = false;
  238. this.groupBox1.Text = "Logon [optional]";
  239. // 
  240. // buttonSearch
  241. // 
  242. this.buttonSearch.Location = new System.Drawing.Point(176, 424);
  243. this.buttonSearch.Name = "buttonSearch";
  244. this.buttonSearch.Size = new System.Drawing.Size(160, 64);
  245. this.buttonSearch.TabIndex = 8;
  246. this.buttonSearch.Text = "Search";
  247. this.buttonSearch.Click += new System.EventHandler(this.buttonSearch_Click);
  248. // 
  249. // textBoxHostname
  250. // 
  251. this.textBoxHostname.Location = new System.Drawing.Point(184, 48);
  252. this.textBoxHostname.Name = "textBoxHostname";
  253. this.textBoxHostname.Size = new System.Drawing.Size(160, 20);
  254. this.textBoxHostname.TabIndex = 1;
  255. this.textBoxHostname.Text = "";
  256. // 
  257. // Form1
  258. // 
  259. this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  260. this.ClientSize = new System.Drawing.Size(736, 517);
  261. this.Controls.Add(this.textBoxResults);
  262. this.Controls.Add(this.buttonSearch);
  263. this.Controls.Add(this.label9);
  264. this.Controls.Add(this.label8);
  265. this.Controls.Add(this.label7);
  266. this.Controls.Add(this.label6);
  267. this.Controls.Add(this.label5);
  268. this.Controls.Add(this.listBoxProperties);
  269. this.Controls.Add(this.buttonLoadProperties);
  270. this.Controls.Add(this.textBoxFilter);
  271. this.Controls.Add(this.label4);
  272. this.Controls.Add(this.groupBox1);
  273. this.Controls.Add(this.textBoxHostname);
  274. this.Controls.Add(this.label1);
  275. this.Name = "Form1";
  276. this.Text = "User Search";
  277. this.groupBox1.ResumeLayout(false);
  278. this.ResumeLayout(false);
  279. }
  280. #endregion
  281. /// <summary>
  282. /// The main entry point for the application.
  283. /// </summary>
  284. [STAThread]
  285. static void Main() 
  286. {
  287. Application.Run(new Form1());
  288. }
  289.       private void buttonSearch_Click(object sender, System.EventArgs e)
  290.       {
  291.          try
  292.          {
  293.             FillResult();
  294.          }
  295.          catch (Exception ex)
  296.          {
  297.             MessageBox.Show("Check your input: " + ex.Message);
  298.          }
  299.       }
  300.       private void buttonLoadProperties_Click(object sender, System.EventArgs e)
  301.       {
  302.          try
  303.          {
  304.             SetLogonInformation();
  305.             SetNamingContext();
  306.             SetUserProperties(schemaNamingContext);
  307.          }
  308.          catch (Exception ex)
  309.          {
  310.             MessageBox.Show("Check your inputs! " + ex.Message);
  311.          }
  312.       }
  313.       protected string[] GetSchemaProperties(string schemaNamingContext, string objectType)
  314.       {
  315.          string[] data;
  316.          using (DirectoryEntry de = new DirectoryEntry())
  317.          {
  318.             de.Username = username;
  319.             de.Password = password;
  320.             de.Path = "LDAP://" + hostname + "CN=" + objectType + "," + schemaNamingContext;
  321.             DS.PropertyCollection properties = de.Properties;
  322.             DS.PropertyValueCollection values = properties["systemMayContain"];
  323.             data = new String[values.Count];
  324.             values.CopyTo(data, 0);
  325.          }
  326.          return data;
  327.       }
  328.       protected void SetUserProperties(string schemaNamingContext)
  329.       {
  330.          StringCollection properties = new StringCollection();
  331.          string[] data = GetSchemaProperties(schemaNamingContext, "User");
  332.          properties.AddRange(GetSchemaProperties(schemaNamingContext, "Organizational-Person"));
  333.          properties.AddRange(GetSchemaProperties(schemaNamingContext, "Person"));
  334.          properties.AddRange(GetSchemaProperties(schemaNamingContext, "Top"));
  335.         
  336.          listBoxProperties.Items.Clear();
  337.          foreach (string s in properties)
  338.          {
  339.             listBoxProperties.Items.Add(s);
  340.          }
  341.       }
  342.       protected void SetNamingContext()
  343.       {
  344.          using (DirectoryEntry de = new DirectoryEntry())
  345.          {
  346.             string path = "LDAP://" + hostname + "rootDSE";
  347.             de.Username = username;
  348.             de.Password = password;
  349.             de.Path = path;
  350.             schemaNamingContext = de.Properties["schemaNamingContext"][0].ToString();
  351.             defaultNamingContext = de.Properties["defaultNamingContext"][0].ToString();
  352.          }
  353.       }
  354.       protected void SetLogonInformation()
  355.       {
  356.          username = (textBoxUsername.Text == "" ? null : textBoxUsername.Text);
  357.          password = (textBoxPassword.Text == "" ? null : textBoxPassword.Text);
  358.          hostname = textBoxHostname.Text;
  359.          if (hostname != "") hostname += "/";
  360.       }
  361.       protected string[] GetProperties()
  362.       {
  363.          string[] properties = new string[listBoxProperties.SelectedItems.Count];
  364.          int i=0;
  365.          foreach (string s in listBoxProperties.SelectedItems)
  366.          {
  367.             properties[i++] = s;
  368.          }
  369.          return properties;
  370.       }
  371.       protected void FillResult()
  372.       {
  373.          using (DirectoryEntry root = new DirectoryEntry())
  374.          {
  375.             root.Username = username;
  376.             root.Password = password;
  377.             root.Path = "LDAP://" + hostname + defaultNamingContext;
  378.             using (DirectorySearcher searcher = new DirectorySearcher())
  379.             {
  380.                searcher.SearchRoot = root;
  381.                searcher.SearchScope = SearchScope.Subtree;
  382.                searcher.Filter = textBoxFilter.Text;
  383.                searcher.PropertiesToLoad.AddRange(GetProperties());
  384.                SearchResultCollection results = searcher.FindAll();
  385.                StringBuilder summary = new StringBuilder();
  386.                foreach (SearchResult result in results)
  387.                {
  388.                   foreach (string propName in result.Properties.PropertyNames)
  389.                   {
  390.                      foreach (string s in result.Properties[propName])
  391.                      {
  392.                         summary.Append(" " + propName + ": " + s + "rn");
  393.                      }
  394.                   }
  395.                   summary.Append("rn");
  396.                }
  397.                textBoxResults.Text = summary.ToString();
  398.             }
  399.          }
  400.       }
  401.       private string username;
  402.       private string password;
  403.       private string hostname;
  404.       private string schemaNamingContext;
  405.       private string defaultNamingContext;
  406. }
  407. }