asp.net读取Windows域(AD)信息

 
//对于域的操作
ExpandedBlockStart.gif 代码
  class  Domain
    {
        
public    string  IsUserExistInDomain( string  ouInfo,  string  domainAdmin,  string  domainPwd)
        {
            Write_log.WriteLog(
" 开始 " );
            
string  strOUInfo  =  ouInfo;
            
string  strDomainAdmin  =  domainAdmin;
            
string  strDomainPassword  =  domainPwd;
            
string  strDomainIP  =  ConfigurationManager.AppSettings[ " DomainIP " ].ToString().Trim();

            
string  strTemp  =   " LDAP:// "   +  strDomainIP.Trim()  +   " / "   +  strOUInfo.Trim();
            DirectoryEntry objDE 
=   new  DirectoryEntry(strTemp, strDomainAdmin.Trim(), strDomainPassword.Trim());
            
string  strFilter  =   " (&(objectCategory=organizationalUnit)(objectClass=organizationalUnit)) " ;
            DirectorySearcher objSearcher 
=   new  DirectorySearcher(objDE, strFilter);
            
// objSearcher.Sort = new SortOption("name", SortDirection.Ascending);
            SearchResultCollection src  =   null ;
            
try
            {
                src 
=  objSearcher.FindAll();//判断域是否连接成功
            }
            
catch  (Exception err)
            {
                MessageBox.Show(
" LDAP,帐号,密码出错,请重新输入! "   +  err.Message);
                
return   " -1 " ;
            }
           
            OleDbTransaction tran 
=   null ;
            OleDbCommand cmd 
=   null ;
           
            OleDbConnection conn 
=   new  OleDbConnection();
            
string  connStr  =   new  DBHelper().ConnectionString( " bpm " );
            
string  strLFAccount  =   string .Empty;
            
string  hrID  =   string .Empty;
            
try
            {
                conn.ConnectionString 
=  connStr;
                conn.Open();
                tran 
=  conn.BeginTransaction();
                cmd 
=  conn.CreateCommand();
                cmd.Transaction 
=  tran;
                DeleteADUsers(cmd);
// delete all adusers that in bpm_AD_USER table,该方法不存在,但不影响对于域操作的理解
               
         // 对于域信息的操作,有些方法暂时没有提供,但不影响对于域操作的理解
                
foreach  (SearchResult sr  in  src)
                {
                    
string  strOrgName  =  sr.Properties[ " name " ][ 0 ].ToString();
                    DirectoryEntry de 
=  sr.GetDirectoryEntry();
                    
foreach  (DirectoryEntry child  in  de.Children)
                    {
                        
if  (child.SchemaClassName.Contains( " user " ))
                        {
                            
if  ( null   ==  child.Properties[ " description " ].Value  ||   ""   ==  child.Properties[ " description " ].Value.ToString().Trim())
                            {
                                
continue ;
                            }

                            
if  ( null   ==  child.Properties[ " userprincipalname " ].Value  ||   ""   ==  child.Properties[ " userprincipalname " ].Value.ToString().Trim())
                            {
                                
continue ;
                            }
                            hrID 
=  child.Properties[ " description " ].Value.ToString().Trim();
                            
// 用户的ou名称
                             string  ouName  =  child.Name.Split( new   char [] {  ' = '  })[ 1 ].ToString();
                            
// 用户的帐号
                             if  (ouName.Contains( " \\ " ))
                            {
                                
string  start  =  ouName.Split( new   char [] {  ' \\ '  })[ 0 ].ToString();
                                
string  end  =  ouName.Split( new   char [] {  ' \\ '  })[ 1 ].ToString().Substring( 1 );
                                ouName 
=  start  +   " _ "   +  end;
                            }
                            strLFAccount 
=  common.strLiveflowName(child.Properties[ " userprincipalname " ].Value.ToString().Trim()).ToLower();
                          
                            
string  USER_Mb  =   "" ;
                            
string  USER_Tel  =   "" ;
                            
string  USER_Email  =   "" ;
                            
if  (child.Properties[ " mail " ].Value  !=   null )
                            {
                                USER_Email 
=  child.Properties[ " mail " ].Value.ToString();
                            }
                            
if  (child.Properties[ " mobile " ].Value  !=   null )
                            {
                                USER_Mb 
=  child.Properties[ " mobile " ].Value.ToString();
                            }
                            
if  (child.Properties[ " telephonenumber " ].Value  !=   null )
                            {
                                USER_Tel 
=  child.Properties[ " telephonenumber " ].Value.ToString();
                            }
                            InsertADUsers(cmd,strLFAccount,hrID,ouName,USER_Email,USER_Mb,USER_Tel);
                        }
                    }
                }
               tran.Commit();
            }
            
catch
            {
                
if  (tran.Connection.State  ==  ConnectionState.Open)
                {
                   tran.Rollback();
                }
                
throw ;
            }
            
finally
            {
                conn.Close();
                conn.Dispose();
            }
           
            
// ArrayList listADOrgCode = new ArrayList();
            
            Write_log.WriteLog(
" 结束 " );
           
//  MessageBox.Show("域用户同步完成");
             return  strLFAccount;
              
        }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值