ITPub博客

首页 > Linux操作系统 > Linux操作系统 > asp.net读取Windows域(AD)信息

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

原创 Linux操作系统 作者:iDotNetSpace 时间:2010-01-12 14:08:28 0 删除 编辑
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博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-624904/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-01-04

  • 博文量
    2376
  • 访问量
    5766109