//对于域的操作
代码
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,该方法不存在,但不影响对于域操作的理解
{
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;
}
}
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;
}
}