C# Active Directory说明字段值未显示
我正在从SSIS中C#(3.5框架)中的脚本任务中获取Active Directory中的值。实现这一点的代码似乎运行良好。我使用一个嵌套循环,外部循环提取非多值的值,并将它们插入一个具有存储过程的表中,然后将该行的PK作为第二个循环中的FK传递给第二个存储过程,该存储过程将用户所属的所有组插入第二个表中 我的问题是,我无法从我认为应该是C# Active Directory说明字段值未显示,c#,sql-server-2008,tsql,ssis,active-directory,C#,Sql Server 2008,Tsql,Ssis,Active Directory,我正在从SSIS中C#(3.5框架)中的脚本任务中获取Active Directory中的值。实现这一点的代码似乎运行良好。我使用一个嵌套循环,外部循环提取非多值的值,并将它们插入一个具有存储过程的表中,然后将该行的PK作为第二个循环中的FK传递给第二个存储过程,该存储过程将用户所属的所有组插入第二个表中 我的问题是,我无法从我认为应该是Description字段的内容中获取任何值,该字段应该包含每个组的描述。如果我单步通过它并检查描述对象的计数,它将显示0。使用相同的代码检查Memberof的
Description
字段的内容中获取任何值,该字段应该包含每个组的描述。如果我单步通过它并检查描述对象的计数,它将显示0。使用相同的代码检查Memberof
的计数,我得到了用户所属的组数。我不理解的另一部分是,我从外部循环中的Description
字段中获取值,但它更接近用户记录的注释,而不是单个组的描述。就好像我的描述
不是多值的。但是其他人可以通过广告门户确认Description
字段具有描述每个组的值。我有点被难住了。请参阅下面的代码
DirectoryEntry entry = new DirectoryEntry("LDAP:[my address]");
DirectorySearcher Dsearch = new DirectorySearcher(entry);
Dsearch.Filter = "(&(objectClass=User))";
foreach (SearchResult sResultSet in Dsearch.FindAll())
{
(code continues from original post)
using (SqlConnection dataConnection = new SqlConnection([mysqlconnection]))
{
using (SqlCommand dataCommand = dataConnection.CreateCommand())
{
dataCommand.CommandText = "ActiveDirectory.InsertParentRecords";
dataCommand.CommandType = CommandType.StoredProcedure;
dataCommand.Parameters.AddWithValue("@PackageLogId", Dts.Variables["PackageLogId"].Value.ToString());
dataCommand.Parameters.AddWithValue("@cn", GetProperty(sResultSet, "cn"));
dataCommand.Parameters.AddWithValue("@givenName", GetProperty(sResultSet, "givenName"));
dataCommand.Parameters.AddWithValue("@initials", GetProperty(sResultSet, "initials"));
dataCommand.Parameters.AddWithValue("@sn", GetProperty(sResultSet, "sn"));
dataCommand.Parameters.AddWithValue("@mail", GetProperty(sResultSet, "mail"));
dataCommand.Parameters.AddWithValue("@Name", GetProperty(sResultSet, "Name"));
dataCommand.Parameters.AddWithValue("@middleName", GetProperty(sResultSet, "middleName"));
dataCommand.Parameters.AddWithValue("@title", GetProperty(sResultSet, "title"));
dataCommand.Parameters.AddWithValue("@employeeID", GetProperty(sResultSet, "employeeID"));
dataCommand.Parameters.AddWithValue("@employeeNumber", GetProperty(sResultSet, "employeeNumber"));
dataCommand.Parameters.AddWithValue("@objectSid", ConvertSidToString((byte[])sResultSet.Properties["objectSid"][0]));
dataCommand.Parameters.AddWithValue("@userAccountControl", tempuserAccountControl);
dataCommand.Parameters.AddWithValue("@whenCreated", GetProperty(sResultSet, "whenCreated"));
dataCommand.Parameters.AddWithValue("@distinguishedName", GetProperty(sResultSet, "distinguishedName"));
dataCommand.Parameters.AddWithValue("@badPasswordTime", Convert.ToString(badPasswordTime)); //Issues!!
dataCommand.Parameters.AddWithValue("@badPwdCount", GetProperty(sResultSet, "badPwdCount"));
dataCommand.Parameters.AddWithValue("@memberof", GetProperty(sResultSet, "memberof"));
dataCommand.Parameters.AddWithValue("@samaccountname", GetProperty(sResultSet, "samaccountname"));
dataCommand.Parameters.AddWithValue("@Description", GetProperty(sResultSet, "Description"));
dataCommand.Parameters.AddWithValue("@maxPwdAge", GetProperty(sResultSet, "maxPwdAge")); //Issues!!
dataCommand.Parameters.AddWithValue("@pwdLastSet", pwdLastSet); //Issues!!
dataCommand.Parameters.AddWithValue("@LockOutTime", Convert.ToString(LockOutTime)); //Issues!!
if (disabled == false) //Issues!!
dataCommand.Parameters.AddWithValue("@Acctdisabled", '0');
else
dataCommand.Parameters.AddWithValue("@Acctdisabled", '1');
dataCommand.Parameters.AddWithValue("@displayname", GetProperty(sResultSet, "displayname"));
dataCommand.Parameters.AddWithValue("@twofactor", twofactor);
dataCommand.Parameters.AddWithValue("@skiprecord", skiprecord);
dataCommand.Parameters.Add("@DetailID", SqlDbType.Int);
dataCommand.Parameters["@DetailID"].Direction = ParameterDirection.Output;
dataConnection.Open();
dataCommand.ExecuteScalar();
dataConnection.Close();
Counter++;
DetailID = (int)dataCommand.Parameters["@DetailID"].Value;
}
}
using (SqlConnection dataConnection = new SqlConnection[mysqlconnection]))
{
using (SqlCommand dataCommand = dataConnection.CreateCommand())
{
dataConnection.Open();
int groupCount = sResultSet.Properties["memberOf"].Count;
int DescriptionCount = sResultSet.Properties["Description"].Count;
for (int counter = 0; counter < groupCount; counter++)
{
dataCommand.CommandText = "ActiveDirectory.InsertMemberOf";
dataCommand.CommandType = CommandType.StoredProcedure;
dataCommand.Parameters.Clear();
dataCommand.Parameters.AddWithValue("@PackageLogId", Dts.Variables["PackageLogId"].Value.ToString());
dataCommand.Parameters.AddWithValue("@DetailID", DetailID);
if (sResultSet.Properties.Contains("Description"))
{
dataCommand.Parameters.AddWithValue("@Group", sResultSet.Properties["Description"][counter].ToString());
}
else
{
dataCommand.Parameters.AddWithValue("@Group", "n/a");
}
dataCommand.Parameters.AddWithValue("@memberOf", sResultSet.Properties["memberOf"][counter]);
dataCommand.ExecuteScalar();
InnerCounter++;
}
} //End of DataCommand
} //End of Data Connection
Debug.WriteLine(GetProperty(sResultSet, "displayname") + " " + Counter + ", " + InnerCounter + ", " + GetProperty(sResultSet, "userAccountControl"));
InnerCounter = 0;
} //End of For Each Loop
DirectoryEntry=newdirectoryEntry(“LDAP:[我的地址]”);
DirectorySearch Dsearch=新的DirectorySearch(条目);
Dsearch.Filter=“(&(objectClass=User))”;
foreach(Dsearch.FindAll()中的SearchResult sResultSet)
{
(代码从原始帖子继续)
使用(SqlConnection-dataConnection=newsqlconnection([mysqlconnection]))
{
使用(SqlCommand dataCommand=dataConnection.CreateCommand())
{
dataCommand.CommandText=“ActiveDirectory.InsertParentRecords”;
dataCommand.CommandType=CommandType.StoredProcess;
dataCommand.Parameters.AddWithValue(“@PackageLogId”,Dts.Variables[“PackageLogId”].Value.ToString());
dataCommand.Parameters.AddWithValue(“@cn”,GetProperty(sResultSet,cn”);
dataCommand.Parameters.AddWithValue(“@givenName”,GetProperty(sResultSet,givenName”);
dataCommand.Parameters.AddWithValue(“@initials”,GetProperty(sResultSet,initials”);
dataCommand.Parameters.AddWithValue(“@sn”,GetProperty(sResultSet,sn”);
dataCommand.Parameters.AddWithValue(“@mail”,GetProperty(sResultSet,mail”);
dataCommand.Parameters.AddWithValue(“@Name”,GetProperty(sResultSet,Name”);
dataCommand.Parameters.AddWithValue(“@middleName”,GetProperty(sResultSet,middleName”);
dataCommand.Parameters.AddWithValue(“@title”,GetProperty(sResultSet,title”);
dataCommand.Parameters.AddWithValue(“@employeeID”,GetProperty(sResultSet,employeeID”);
dataCommand.Parameters.AddWithValue(“@employeeNumber”,GetProperty(sResultSet,“employeeNumber”);
dataCommand.Parameters.AddWithValue(@objectSid),ConvertSidToString((字节[])sResultSet.Properties[“objectSid”][0]);
dataCommand.Parameters.AddWithValue(“@userAccountControl”,tempuserAccountControl);
dataCommand.Parameters.AddWithValue(“@whenCreated”,GetProperty(sResultSet,“whenCreated”);
dataCommand.Parameters.AddWithValue(“@DifferentiedName”,GetProperty(sResultSet,DifferentiedName”);
dataCommand.Parameters.AddWithValue(“@badPasswordTime”,Convert.ToString(badPasswordTime));//问题!!
dataCommand.Parameters.AddWithValue(“@badPwdCount”,GetProperty(sResultSet,“badPwdCount”);
dataCommand.Parameters.AddWithValue(“@memberof”,GetProperty(sResultSet,memberof”);
dataCommand.Parameters.AddWithValue(“@samaccountname”,GetProperty(sResultSet,“samaccountname”);
dataCommand.Parameters.AddWithValue(“@Description”,GetProperty(sResultSet,Description”);
dataCommand.Parameters.AddWithValue(“@maxPwdAge”,GetProperty(sResultSet,“maxPwdAge”);//问题!!
dataCommand.Parameters.AddWithValue(“@pwdLastSet”,pwdLastSet);//问题!!
dataCommand.Parameters.AddWithValue(“@LockOutTime”,Convert.ToString(LockOutTime));//问题!!
if(disabled==false)//问题!!
dataCommand.Parameters.AddWithValue(“@Acctdisabled”,“0”);
其他的
dataCommand.Parameters.AddWithValue(“@Acctdisabled”,“1”);
dataCommand.Parameters.AddWithValue(“@displayname”,GetProperty(sResultSet,displayname”);
dataCommand.Parameters.AddWithValue(“@twofactor”,twofactor);
dataCommand.Parameters.AddWithValue(“@skiprecard”,skiprecard);
dataCommand.Parameters.Add(“@DetailID”,SqlDbType.Int);
dataCommand.Parameters[“@DetailID”].Direction=ParameterDirection.Output;
dataConnection.Open();
dataCommand.ExecuteScalar();
dataConnection.Close();
计数器++;
DetailID=(int)dataCommand.Parameters[“@DetailID”].Value;
}
}
使用(SqlConnection-dataConnection=newsqlconnection[mysqlconnection]))
{
使用(SqlCommand dataCommand=dataConnection.CreateCommand())
{
dataConnection.Open();
int groupCount=sResultSet.Properties[“memberOf”].Count;
int DescriptionCount=sResultSet.Properties[“Description”].Count;
用于(int计数器=0;计数器DirectorySearcher Dsearch = new DirectorySearcher(entry);
...
Dsearch.PropertiesToLoad.Add("description");
...
Dsearch.Filter = "(&(objectClass=User))";