在asp.net如何取得ActiveDirectory域中用户的信息,比如工号,邮件地址等等

using  System;
None.gif
using  System.Drawing;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Windows.Forms;
None.gif
using  System.Data;
None.gif
using  System.DirectoryServices ;
None.gif
using  Palau.DAL ;
None.gif
None.gif
namespace  WindowsApplication1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Form1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Form1 : System.Windows.Forms.Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private System.Windows.Forms.Button button1;
InBlock.gif        
private System.Windows.Forms.RichTextBox richTextBox1;
InBlock.gif        
private System.Windows.Forms.TextBox textBox1;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 必需的设计器变量。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private System.ComponentModel.Container components = null;
InBlock.gif
InBlock.gif        
public Form1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// Windows 窗体设计器支持所必需的
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif
InBlock.gif            
//
InBlock.gif            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 清理所有正在使用的资源。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        protected override void Dispose( bool disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if( disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (components != null
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    components.Dispose();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.Dispose( disposing );
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.button1 = new System.Windows.Forms.Button();
InBlock.gif            
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
InBlock.gif            
this.textBox1 = new System.Windows.Forms.TextBox();
InBlock.gif            
this.SuspendLayout();
InBlock.gif            
// 
InBlock.gif            
// button1
InBlock.gif            
// 
InBlock.gif
            this.button1.Location = new System.Drawing.Point(328232);
InBlock.gif            
this.button1.Name = "button1";
InBlock.gif            
this.button1.TabIndex = 0;
InBlock.gif            
this.button1.Text = "UPDATE";
InBlock.gif            
this.button1.Click += new System.EventHandler(this.button1_Click);
InBlock.gif            
// 
InBlock.gif            
// richTextBox1
InBlock.gif            
// 
InBlock.gif
            this.richTextBox1.Location = new System.Drawing.Point(2416);
InBlock.gif            
this.richTextBox1.Name = "richTextBox1";
InBlock.gif            
this.richTextBox1.Size = new System.Drawing.Size(448192);
InBlock.gif            
this.richTextBox1.TabIndex = 1;
InBlock.gif            
this.richTextBox1.Text = "";
InBlock.gif            
// 
InBlock.gif            
// textBox1
InBlock.gif            
// 
InBlock.gif
            this.textBox1.Location = new System.Drawing.Point(144232);
InBlock.gif            
this.textBox1.Name = "textBox1";
InBlock.gif            
this.textBox1.TabIndex = 2;
InBlock.gif            
this.textBox1.Text = "GES";
InBlock.gif            
// 
InBlock.gif            
// Form1
InBlock.gif            
// 
InBlock.gif
            this.AutoScaleBaseSize = new System.Drawing.Size(614);
InBlock.gif            
this.ClientSize = new System.Drawing.Size(496273);
InBlock.gif            
this.Controls.Add(this.textBox1);
InBlock.gif            
this.Controls.Add(this.richTextBox1);
InBlock.gif            
this.Controls.Add(this.button1);
InBlock.gif            
this.Name = "Form1";
InBlock.gif            
this.Text = "Create User";
InBlock.gif            
this.ResumeLayout(false);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 应用程序的主入口点。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [STAThread]
InBlock.gif        
static void Main() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Application.Run(
new Form1());
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void AddUser(DirectoryEntry obj,PersistBroker broker)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string sSQL = @"INSERT INTO TBLSYSUSER([USE_CODE],[USE_NAME],[USE_PWD],[USE_WORKNO],[USE_DOMAIN],[USE_EMAIL],[USE_LEVEL],"
InBlock.gif                
+"[USE_MSN], [USE_QQ], [USE_EXTPHONE], [USE_MOBILE], [USE_HOMEPHONE], [USE_BIRTHDAY], [USE_LOVE], [USE_ADDR], [USE_WEMAIL],"
InBlock.gif                
+"[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,"
InBlock.gif                
+ "@HOMEPHONE,@BIRTHDAY,@LOVE,@ADDR,@WEMAIL,@HOMEPAGE,@KNOWLEDGE,@ACTIVE,@SEX,@TEM_CODE,@IFCUSTOMER,@DEP_CODE,@CREATEDATE) ";
ExpandedSubBlockStart.gifContractedSubBlock.gif            
string[] paramNames = new string[]dot.gif{"@CODE","@NAME","@PWD","@WORKNO","@DOMAIN","@EMAIL","@LEVEL","@MSN","@QQ","@EXTPHONE",
InBlock.gif                                                  
"@MOBILE","@HOMEPHONE","@BIRTHDAY","@LOVE","@ADDR","@WEMAIL","@HOMEPAGE","@KNOWLEDGE",
ExpandedSubBlockEnd.gif                                                  
"@ACTIVE","@SEX","@TEM_CODE","@IFCUSTOMER","@DEP_CODE","@CREATEDATE"}
;
InBlock.gif            
string SSQL = @"SELECT 1 FROM TBLSYSUSER WHERE USE_CODE=@CODE";
ExpandedSubBlockStart.gifContractedSubBlock.gif            
string[] paramsName = new string[]dot.gif{"@CODE"};
InBlock.gif            
InBlock.gif
InBlock.gif            
if(obj.SchemaClassName.Trim().ToUpper()=="ORGANIZATIONALUNIT")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string strDept = obj.Name.Trim().Split('=')[1] ;
InBlock.gif                DirectoryEntries subList 
= obj.Children ;
InBlock.gif                
foreach(DirectoryEntry objSubDE in subList)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if(objSubDE.SchemaClassName.Trim().ToUpper()=="USER")
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        AddUser(objSubDE,broker);
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
else if(obj.SchemaClassName.Trim().ToUpper()=="USER")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
InBlock.gif                
string Code = obj.Properties["name"].Value==null?"":obj.Properties["name"].Value.ToString() ;
InBlock.gif

InBlock.gif
                string Name = obj.Properties["sn"].Value == null ? "" : obj.Properties["sn"].Value.ToString();                
InBlock.gif                
string department = obj.Properties["department"].Value == null ? "" : obj.Properties["department"].Value.ToString();
InBlock.gif                
string workno = obj.Properties["wWWHomePage"].Value == null ? "" : obj.Properties["wWWHomePage"].Value.ToString();
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if(workno == "http://personal/kevin jiang/"dot.gif{workno="0406102";} 
InBlock.gif                
string phone = obj.Properties["telephoneNumber"].Value == null ? "" : obj.Properties["telephoneNumber"].Value.ToString();
InBlock.gif                
string Level = obj.Properties["title"].Value == null ? "" : obj.Properties["title"].Value.ToString();
InBlock.gif                
InBlock.gif                
string address = obj.Properties["co"].Value == null ? "" : obj.Properties["co"].Value.ToString() + " " + obj.Properties["l"].Value == null ? "" : obj.Properties["l"].Value.ToString();
InBlock.gif                
string createdate = obj.Properties["whenCreated"].Value == null ? "" : obj.Properties["whenCreated"].Value.ToString() ;
InBlock.gif                
string mail = obj.Properties["mail"].Value == null ? "" : obj.Properties["mail"].Value.ToString();
InBlock.gif                
if(workno != string.Empty || Code != "asf" )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
object[] paramValues = new object[]dot.gif{Code,Name,"9D6C539199026AF7",workno,"BQC",mail,Level,"","",phone,
ExpandedSubBlockEnd.gif                                                           
"","","1970-1-1","","",address,"","","Y","1",department,"N",department,createdate}
;
InBlock.gif                    
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
object[] paramsValue = new object[]dot.gif{Code};
InBlock.gif                    DataSet ds 
= broker.ExecuteDataset(SSQL,CommandType.Text,paramsName,paramsValue);
InBlock.gif                    
if(ds.Tables[0].Rows.Count == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        broker.ExecuteNonQuery(sSQL,CommandType.Text,paramNames,paramValues) ;
InBlock.gif                    
InBlock.gif                    
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
string sSQLU = @"UPDATE TBLSYSUSER SET [USE_NAME]=@NAME, [USE_WORKNO]=@WORKNO, [USE_DOMAIN]=@DOMAIN, "+
InBlock.gif                            
"[USE_EMAIL]=@EMAIL, [USE_LEVEL]=@LEVEL, [USE_EXTPHONE]=@EXTPHONE,"+
InBlock.gif                            
"[USE_ADDR]=@ADDR, [USE_ACTIVE]=@ACTIVE,[DEP_CODE] = @DEP_CODE,[USE_CREATEDATE]=@CREATEDATE,"+
InBlock.gif                            
"[USE_IFCUSTOMER]=@IFCUSTOMER WHERE [USE_CODE]=@CODE";
InBlock.gif                        broker.ExecuteNonQuery(sSQLU,CommandType.Text,paramNames,paramValues) ;
InBlock.gif                
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
this.richTextBox1.AppendText( department + "\t" +  Code  + "\t" + Name + "\r\n") ;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void button1_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.richTextBox1.Text = "";
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string conn = System.Configuration.ConfigurationSettings.AppSettings["CONN"];
InBlock.gif                PersistBroker broker 
= PersistBroker.Instance(conn,"SQLSERVER") ;
InBlock.gif                broker.Open() ;

InBlock.gif

InBlock.gif                DirectoryEntry de 
= new DirectoryEntry() ;
InBlock.gif
                string dept = this.textBox1.Text;
InBlock.gif                
if(dept != string.Empty)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    dept
= "OU="+dept+",";
ExpandedSubBlockEnd.gif                }

InBlock.gif                de.Path 
= "LDAP://"+dept+"OU=G00,OU=Users Guru,DC=mycom,dc=corp,dc=com" ;
InBlock.gif                de.Username
= "mycom\\andy luo";//System.Configuration.ConfigurationSettings.AppSettings["USER"];
InBlock.gif
                de.Password = "";// System.Configuration.ConfigurationSettings.AppSettings["PWD"] ;
InBlock.gif
                DirectoryEntries list = de.Children ;
InBlock.gif                
foreach(DirectoryEntry objDE in list)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this.AddUser(objDE,broker);
                                
InBlock.gif
InBlock.gif                broker.Close() ;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MessageBox.Show(ex.Message) ;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/xiongeee/archive/2006/08/30/490263.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值