【问题标题】:C# to Active Directory getting User / Group / DescriptionC# 到 Active Directory 获取用户/组/描述
【发布时间】:2014-02-04 22:59:57
【问题描述】:

这与this post 相关,我正在尝试做同样的事情。因为我无法让它发挥作用,所以我正在尝试一种不同的方法。我的目标是让所有用户都在一个集合中,并循环通过集合加载参数,以实现类似于前一个帖子的存储过程。

这次我再次尝试 AccountManagement 类,我可以获取用户的所有属性,但是这次我不知道获取组或组描述的语法。我假设我需要简单地加载来自 UserPrincipal 的某种类型的集合,例如 up.GetGroups() 并枚举它,但我在语法上苦苦挣扎。使用该代码,我还需要可以访问描述的内容。

            PrincipalContext AD = new PrincipalContext(ContextType.Domain, "[my domain", "[my path]");
            UserPrincipal ADUser = new UserPrincipal(AD);
            PrincipalSearcher ps = new PrincipalSearcher();
            ps.QueryFilter = ADUser;

            PrincipalSearchResult<Principal> result = ps.FindAll();

            foreach (Principal p in result)
                using (UserPrincipal up = (UserPrincipal)p)
                {

                    if (up.AccountExpirationDate.HasValue)
                        Debug.WriteLine(up.AccountExpirationDate.ToString());
                    if (up.AccountLockoutTime.HasValue)
                        Debug.WriteLine(up.BadLogonCount.ToString());
                    if (up.DisplayName != null)
                        Debug.WriteLine(up.DisplayName.ToString());
                    if (up.DistinguishedName != null)
                        Debug.WriteLine(up.DistinguishedName.ToString());
                    if (up.EmailAddress != null)
                        Debug.WriteLine(up.EmailAddress.ToString());
                    if (up.EmployeeId != null)
                        Debug.WriteLine(up.EmployeeId.ToString());
                    if (up.Enabled.HasValue)
                        if (up.Enabled == true)
                            Debug.WriteLine("User is active");
                        else
                            Debug.WriteLine("User is deactivated");
                    if (up.GivenName != null)
                        Debug.WriteLine(up.GivenName.ToString());
                    if (up.LastBadPasswordAttempt.HasValue)
                        Debug.WriteLine(up.LastBadPasswordAttempt.ToString());
                    if (up.LastLogon.HasValue)
                        Debug.WriteLine(up.LastLogon.ToString());
                    if (up.LastPasswordSet.HasValue)
                        Debug.WriteLine(up.LastPasswordSet.ToString());
                    if (up.MiddleName != null)
                        Debug.WriteLine(up.MiddleName.ToString());
                    if (up.Name != null)
                        Debug.WriteLine(up.Name.ToString());
                    if (up.PasswordNeverExpires != null)
                        if (up.PasswordNeverExpires == true)
                            Debug.Print("User Password Never Expires");
                        else
                            Debug.WriteLine("User Password Expires");
                    if (up.SamAccountName != null)
                        Debug.WriteLine(up.SamAccountName.ToString());
                    if (up.Sid != null)
                        Debug.WriteLine(up.Sid.ToString());
                    if (up.Surname != null)
                        Debug.WriteLine(up.Surname.ToString());
                    if (up.UserPrincipalName != null)
                        Debug.WriteLine(up.UserPrincipalName.ToString());
                    if (up.VoiceTelephoneNumber != null)
                        Debug.WriteLine(up.VoiceTelephoneNumber.ToString());

                }

我尝试使用GroupPrincipal,但正如它所写的那样,我可以看到描述字段,但我看不到其他任何内容。我尝试了该代码:

     //PrincipalContext AD = new PrincipalContext(ContextType.Domain, "[my domain]", "[my path]");
            GroupPrincipal theGroup = new GroupPrincipal(AD);
            PrincipalSearcher gps = new PrincipalSearcher(theGroup);
            foreach (var found in gps.FindAll())
            {
                if (found.Description != null)
                {                       
                    Debug.WriteLine(found.Description.ToString());
                }
                if (found.DisplayName != null)
                {
                    Debug.WriteLine(found.DisplayName.ToString());
                }                    
            }

这段代码可以很好地获取组的描述,但我看不到其他任何内容,因为所有其他字段都是空的。

我们将不胜感激任何和所有的帮助。

【问题讨论】:

  • 除了名称和描述之外,您还想从群组中获得什么?
  • 不幸的是,我需要上面第一段代码中列出的所有字段。除了我遇到的问题之外,似乎即使我从这篇文章中解决了问题,其中一些字段在 UserPrincipal 中也将不可用。我将不得不在 UserPrincipal 和“ExtensionGet”方法之间使用某种类型的混合。我对此了解不多,但我看到其他人在其他帖子中谈论他们是如何解决它的,当我到达那里时,我会越过那座桥。
  • 您为用户列出的一半内容不适用于组,只会为空,其余部分无论如何都不会真正使用,仍然为空。我相信您的代码没有任何问题,您的问题只是您的组不存在这些信息。
  • @Ashigore,我确定我在回答中遇到了这个问题。我找不到或不知道的最大的事情是PrincipalSearchResult&lt;Principal&gt; userGroups = CurrentUser.GetGroups();因为我不知道如何访问每个用户所属的组。一旦我知道了,我就没事了,剩下的就可以解决了。但是您提到的问题仍然存在,我仍在努力寻找解决方案。无论哪种方式,我仍然需要这段代码才能进入下一步,希望很快就能完成。谢谢

标签: c# active-directory


【解决方案1】:

我相信我解决了这篇文章的问题。下面是填充 Active Directory 中的对象并进入嵌套循环的代码。第一个循环遍历每个用户,第二个循环遍历用户所属的每个组,然后再移动到下一个用户。在每个循环中,我将参数传递给存储过程,这些存储过程执行插入语句,并将值发送到两个单独的表,一个用于用户属性,第二个用于组属性。第一个存储过程将每行的标识传回以用作第二个表中的 FK。

对于某些参数,是“NotFoundYet”字样,因为 UserPrincipal 没有像 DirectorySearcher 在我第一次尝试编写本文时那样提供的那些字段。这也回答了我来自Active Directory description field values not showing up 的问题,也是我发布的。

从技术上讲,我仍然需要代码来找到那些缺失的属性,但我虽然这可能会帮助其他人,而我仍在尝试找到我没有的东西。我稍后会更新。特别感谢 Jeff Ronay 在这方面的帮助。

    PrincipalContext AD = new PrincipalContext(ContextType.Domain, "[my domanin]", "[my path]");
            UserPrincipal ADUser = new UserPrincipal(AD);
            PrincipalSearcher ps = new PrincipalSearcher();
            ps.QueryFilter = ADUser;

            PrincipalSearchResult<Principal> result = ps.FindAll();

            foreach (UserPrincipal CurrentUser in result)
            {

                PrincipalSearchResult<Principal> userGroups = CurrentUser.GetGroups();


            using (SqlConnection dataConnection = new SqlConnection("[my sql connection]"))
            {
                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", "NotFoundYet");

                    if (CurrentUser.GivenName != null)
                    {
                        dataCommand.Parameters.AddWithValue("@givenName", CurrentUser.GivenName.ToString());
                    }
                    else
                    {
                        dataCommand.Parameters.AddWithValue("@givenName", "Empty");
                    }

                    dataCommand.Parameters.AddWithValue("@initials", "NotFoundYet");

                    if (CurrentUser.Surname != null)
                    {
                        dataCommand.Parameters.AddWithValue("@sn", CurrentUser.Surname.ToString());
                    }
                    else
                    {
                        dataCommand.Parameters.AddWithValue("@sn", "Empty");
                    }

                    if (CurrentUser.EmailAddress != null)
                    {
                        dataCommand.Parameters.AddWithValue("@mail", CurrentUser.EmailAddress.ToString());                        
                    }
                    else
                    {
                        dataCommand.Parameters.AddWithValue("@mail", "Empty");                        
                    }

                    if (CurrentUser.Name != null)
                    {
                        dataCommand.Parameters.AddWithValue("@Name", CurrentUser.Name.ToString());
                    }
                    else
                    {
                        dataCommand.Parameters.AddWithValue("@Name", "Empty");
                    }

                    if (CurrentUser.MiddleName != null)
                    {
                        dataCommand.Parameters.AddWithValue("@middleName", CurrentUser.MiddleName.ToString());
                    }
                    else
                    {
                        dataCommand.Parameters.AddWithValue("@middleName", "N/A");
                    }

                    dataCommand.Parameters.AddWithValue("@title", "NotFoundYet");

                    if (CurrentUser.EmployeeId != null)
                    {
                        dataCommand.Parameters.AddWithValue("@employeeID", CurrentUser.EmployeeId.ToString());
                    }
                    else
                    {
                        dataCommand.Parameters.AddWithValue("@employeeID", "Empty");    
                    }

                    dataCommand.Parameters.AddWithValue("@employeeNumber", "NotFoundYet");

                    if (CurrentUser.Sid != null)
                    {
                        dataCommand.Parameters.AddWithValue("@objectSid", CurrentUser.Sid.ToString());
                    }
                    else
                    {
                        dataCommand.Parameters.AddWithValue("@objectSid", "Empty");
                    }

                    dataCommand.Parameters.AddWithValue("@userAccountControl", "NotFoundYet" );
                    dataCommand.Parameters.AddWithValue("@whenCreated", "NotFoundYet");

                    if (CurrentUser.DistinguishedName != null)
                    {
                        dataCommand.Parameters.AddWithValue("@distinguishedName", CurrentUser.DistinguishedName.ToString());
                    }
                    else
                    {
                        dataCommand.Parameters.AddWithValue("@distinguishedName", "Empty");
                    }

                    dataCommand.Parameters.AddWithValue("@badPasswordTime", "NotFoundYet");  //Issues!!

                    if (CurrentUser.BadLogonCount != null)
                    {
                        dataCommand.Parameters.AddWithValue("@badPwdCount", CurrentUser.BadLogonCount.ToString());
                    }
                    else
                    {
                        dataCommand.Parameters.AddWithValue("@badPwdCount", "Empty");
                    }

                    dataCommand.Parameters.AddWithValue("@memberof", "Empty");

                    if (CurrentUser.SamAccountName != null)
                    {
                        dataCommand.Parameters.AddWithValue("@samaccountname", CurrentUser.SamAccountName.ToString());
                    }
                    else
                    {
                        dataCommand.Parameters.AddWithValue("@samaccountname", "Empty");
                    }

                    if (CurrentUser.Description != null)
                    {
                        dataCommand.Parameters.AddWithValue("@Description", CurrentUser.Description.ToString());
                    }
                    else
                    {
                        dataCommand.Parameters.AddWithValue("@Description", "Empty");
                    }

                    dataCommand.Parameters.AddWithValue("@maxPwdAge", "NotFoundYet");   //Issues!!                               

                    if (CurrentUser.LastPasswordSet != null)
                    {
                        dataCommand.Parameters.AddWithValue("@pwdLastSet", CurrentUser.LastPasswordSet.ToString());   //Issues!!
                    }
                    else
                    {
                        dataCommand.Parameters.AddWithValue("@pwdLastSet", "Empty");   //Issues!!
                    }

                    if (CurrentUser.AccountLockoutTime != null)
                    {
                        dataCommand.Parameters.AddWithValue("@LockOutTime", CurrentUser.AccountLockoutTime.ToString());     
                    }
                    else
                    {
                        dataCommand.Parameters.AddWithValue("@LockOutTime", "Empty");     //Issues!!
                    }

                    if (CurrentUser.Enabled == false)  //Issues!!
                    {
                        dataCommand.Parameters.AddWithValue("@Acctdisabled", '0');
                    }
                    else
                    {
                        dataCommand.Parameters.AddWithValue("@Acctdisabled", '1');
                    }

                    if (CurrentUser.DisplayName != null)
                    {
                        dataCommand.Parameters.AddWithValue("@displayname", CurrentUser.DisplayName.ToString());
                    }
                    else
                    {
                        dataCommand.Parameters.AddWithValue("@displayname", "Empty");
                    }

                    dataCommand.Parameters.AddWithValue("@twofactor", "NotFoundYet");     //Calculated from another field     

                    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;

                }  //End of Datacommand

            }  //End of Sql Connection



            using (SqlConnection dataConnection = new SqlConnection("[my sql connection]"))
            {
                using (SqlCommand dataCommand = dataConnection.CreateCommand())
                {
                    dataConnection.Open();

                    foreach (Principal group in userGroups)
                    {

                        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 (group.Description != null)
                        {
                            Debug.WriteLine(group.Description.ToString());
                            dataCommand.Parameters.AddWithValue("@GroupDescription", group.Description.ToString());                                
                        }
                        else
                        {
                            dataCommand.Parameters.AddWithValue("@GroupDescription", "Empty");                                
                        }

                        if (group.Name != null)
                        {
                            Debug.WriteLine(group.Name.ToString());
                            dataCommand.Parameters.AddWithValue("@memberOf", group.Name.ToString());                                
                        }
                        else
                        {
                            dataCommand.Parameters.AddWithValue("@memberOf", "Empty");                                
                        }

                        dataCommand.ExecuteScalar();
                        InnerCounter++;                                                        

                    }  //End of 'For Each Principle'

                }//End of DataCommand

            }  //End of Data Connection

        }   //End of 'For Each User' Loop

【讨论】:

  • 我重写了这段代码以使用函数来检查空值,但总的来说都是一样的。
猜你喜欢
  • 2018-06-26
  • 1970-01-01
  • 2019-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-14
  • 1970-01-01
相关资源
最近更新 更多