技术开发 频道

在asp.net如何取得ActiveDirectory域中用户的信息


【IT168技术文档】

using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.DirectoryServices ; using Palau.DAL ; namespace WindowsApplication1 { /**//// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.Windows.Forms.RichTextBox richTextBox1; private System.Windows.Forms.TextBox textBox1; /**//// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /**//// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码 /**//// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.richTextBox1 = new System.Windows.Forms.RichTextBox(); this.textBox1 = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(328, 232); this.button1.Name = "button1"; this.button1.TabIndex = 0; this.button1.Text = "UPDATE"; this.button1.Click += new System.EventHandler(this.button1_Click); // // richTextBox1 // this.richTextBox1.Location = new System.Drawing.Point(24, 16); this.richTextBox1.Name = "richTextBox1"; this.richTextBox1.Size = new System.Drawing.Size(448, 192); this.richTextBox1.TabIndex = 1; this.richTextBox1.Text = ""; // // textBox1 // this.textBox1.Location = new System.Drawing.Point(144, 232); this.textBox1.Name = "textBox1"; this.textBox1.TabIndex = 2; this.textBox1.Text = "GES"; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(496, 273); this.Controls.Add(this.textBox1); this.Controls.Add(this.richTextBox1); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Create User"; this.ResumeLayout(false); } #endregion /**//// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void AddUser(DirectoryEntry obj,PersistBroker broker) { string sSQL = @"INSERT INTO TBLSYSUSER([USE_CODE],[USE_NAME],[USE_PWD],[USE_WORKNO],[USE_DOMAIN],[USE_EMAIL],[USE_LEVEL]," +"[USE_MSN], [USE_QQ], [USE_EXTPHONE], [USE_MOBILE], [USE_HOMEPHONE], [USE_BIRTHDAY], [USE_LOVE], [USE_ADDR], [USE_WEMAIL]," +"[USE_HOMEPAGE], [USE_KNOWLEDGE],[USE_ACTIVE],[USE_SEX],[TEM_CODE],[USE_IFCUSTOMER],[DEP_CODE],[USE_CREATEDATE]) VALUES(@CODE,@NAME, @PWD,@WORKNO,@DOMAIN,@EMAIL, @LEVEL, @MSN,@QQ,@EXTPHONE,@MOBILE," + "@HOMEPHONE,@BIRTHDAY,@LOVE,@ADDR,@WEMAIL,@HOMEPAGE,@KNOWLEDGE,@ACTIVE,@SEX,@TEM_CODE,@IFCUSTOMER,@DEP_CODE,@CREATEDATE) "; string[] paramNames = new string[]{"@CODE","@NAME","@PWD","@WORKNO","@DOMAIN","@EMAIL","@LEVEL","@MSN","@QQ","@EXTPHONE", "@MOBILE","@HOMEPHONE","@BIRTHDAY","@LOVE","@ADDR","@WEMAIL","@HOMEPAGE","@KNOWLEDGE", "@ACTIVE","@SEX","@TEM_CODE","@IFCUSTOMER","@DEP_CODE","@CREATEDATE"}; string SSQL = @"SELECT 1 FROM TBLSYSUSER WHERE USE_CODE=@CODE"; string[] paramsName = new string[]{"@CODE"}; if(obj.SchemaClassName.Trim().ToUpper()=="ORGANIZATIONALUNIT") { string strDept = obj.Name.Trim().Split('=')[1] ; DirectoryEntries subList = obj.Children ; foreach(DirectoryEntry objSubDE in subList) { if(objSubDE.SchemaClassName.Trim().ToUpper()=="USER") { AddUser(objSubDE,broker); } } } else if(obj.SchemaClassName.Trim().ToUpper()=="USER") { string Code = obj.Properties["name"].Value==null?"":obj.Properties["name"].Value.ToString() ; string Name = obj.Properties["sn"].Value == null ? "" : obj.Properties["sn"].Value.ToString(); string department = obj.Properties["department"].Value == null ? "" : obj.Properties["department"].Value.ToString(); string workno = obj.Properties["wWWHomePage"].Value == null ? "" : obj.Properties["wWWHomePage"].Value.ToString(); if(workno == "http://personal/kevin jiang/") {workno="0406102";} string phone = obj.Properties["telephoneNumber"].Value == null ? "" : obj.Properties["telephoneNumber"].Value.ToString(); string Level = obj.Properties["title"].Value == null ? "" : obj.Properties["title"].Value.ToString(); string address = obj.Properties["co"].Value == null ? "" : obj.Properties["co"].Value.ToString() + " " + obj.Properties["l"].Value == null ? "" : obj.Properties["l"].Value.ToString(); string createdate = obj.Properties["whenCreated"].Value == null ? "" : obj.Properties["whenCreated"].Value.ToString() ; string mail = obj.Properties["mail"].Value == null ? "" : obj.Properties["mail"].Value.ToString(); if(workno != string.Empty || Code != "asf" ) { object[] paramValues = new object[]{Code,Name,"9D6C539199026AF7",workno,"BQC",mail,Level,"","",phone, "","","1970-1-1","","",address,"","","Y","1",department,"N",department,createdate}; object[] paramsValue = new object[]{Code}; DataSet ds = broker.ExecuteDataset(SSQL,CommandType.Text,paramsName,paramsValue); if(ds.Tables[0].Rows.Count == 0) { broker.ExecuteNonQuery(sSQL,CommandType.Text,paramNames,paramValues) ; } else { string sSQLU = @"UPDATE TBLSYSUSER SET [USE_NAME]=@NAME, [USE_WORKNO]=@WORKNO, [USE_DOMAIN]=@DOMAIN, "+ "[USE_EMAIL]=@EMAIL, [USE_LEVEL]=@LEVEL, [USE_EXTPHONE]=@EXTPHONE,"+ "[USE_ADDR]=@ADDR, [USE_ACTIVE]=@ACTIVE,[DEP_CODE] = @DEP_CODE,[USE_CREATEDATE]=@CREATEDATE,"+ "[USE_IFCUSTOMER]=@IFCUSTOMER WHERE [USE_CODE]=@CODE"; broker.ExecuteNonQuery(sSQLU,CommandType.Text,paramNames,paramValues) ; } } this.richTextBox1.AppendText( department + "\t" + Code + "\t" + Name + "\r\n") ; } } private void button1_Click(object sender, System.EventArgs e) { this.richTextBox1.Text = ""; try { string conn = System.Configuration.ConfigurationSettings.AppSettings["CONN"]; PersistBroker broker = PersistBroker.Instance(conn,"SQLSERVER") ; broker.Open() ; DirectoryEntry de = new DirectoryEntry() ; string dept = this.textBox1.Text; if(dept != string.Empty) { dept= "OU="+dept+","; } de.Path = "LDAP://"+dept+"OU=G00,OU=Users Guru,DC=mycom,dc=corp,dc=com" ; de.Username= "mycom\\andy luo";//System.Configuration.ConfigurationSettings.AppSettings["USER"]; de.Password = "";// System.Configuration.ConfigurationSettings.AppSettings["PWD"] ; DirectoryEntries list = de.Children ; foreach(DirectoryEntry objDE in list) { this.AddUser(objDE,broker); broker.Close() ; } catch(Exception ex) { MessageBox.Show(ex.Message) ; } } } }
0
相关文章