asp.net讀取Windows域(AD)資訊

iDotNetSpace發表於2010-01-12
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;
              
        }
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-624905/,如需轉載,請註明出處,否則將追究法律責任。

相關文章