【问题标题】:Active Directory description field values not showing upActive Directory 描述字段值未显示
【发布时间】:2014-02-23 03:01:44
【问题描述】:

我正在从 SSIS 中的 C#(3.5 框架)中的脚本任务从 Active Directory 中获取值。完成此操作的代码似乎工作正常。我使用嵌套循环,外部循环提取非多值的值并将它们插入到带有存储过程的表中,然后将该行的 PK 传递回第二个循环中用作 FK 到第二个将用户所属的所有组插入第二个表的存储过程。

我的问题是我无法从我认为应该包含每个组描述的 Description 字段中获取任何值。如果我单步执行并检查描述对象的计数,它显示为 0。使用相同的代码检查 Memberof 的计数,我得到用户所属的组数。我不明白的另一部分是我从外部循环中的 Description 字段中获取值,但它更接近于用户记录的 cmets,而不是各个组的描述。好像我的Description 不是多值的。但是其他人可以通过 AD 门户确认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

【问题讨论】:

  • 添加代码以显示如何在编辑中填充 sResultSet,@billinkc
  • 您能否与 ADSI Edit technet.microsoft.com/en-us/library/cc773354(v=ws.10).aspx 联系并亲自验证该字段中的内容?
  • 我还没有尝试过(不再工作了),但我确实有人通过 AD 门户验证描述是每个组名
  • 我发现它经常为我节省很多精力来绝对确定
  • 我必须在不安装软件的情况下完成此操作。我没有在我的电脑上安装任何东西的权限,当我的权限被提升时,我希望能找到解决办法。

标签: c# sql-server-2008 tsql ssis active-directory


【解决方案1】:

LDAP 搜索查询有点像 SQL 查询,我知道它没有明确记录,但您最好声明要通过搜索检索的属性。可以试试加吗

DirectorySearcher Dsearch = new DirectorySearcher(entry);
...
Dsearch.PropertiesToLoad.Add("description");
...
Dsearch.Filter = "(&(objectClass=User))"; 

在我的代码中,我确实添加了我需要的所有属性。我同意它在大多数情况下都可以正常工作,但在大多数情况下。

【讨论】:

    猜你喜欢
    • 2018-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-01
    • 1970-01-01
    • 2020-09-15
    相关资源
    最近更新 更多