首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从C#到Active获取用户/组/描述

从C#到Active获取用户/组/描述
EN

Stack Overflow用户
提问于 2014-02-03 17:57:04
回答 1查看 1.9K关注 0票数 0

这是与this post相关的,我正在尝试做同样的事情。因为我无法让它起作用,我正在尝试一种不同的方法。我的目标是让集合中的所有用户循环通过集合加载参数,以便存储过程模拟到透水的post。

这一次我再次尝试AccountManagement类,我可以获得用户的所有属性,但这次我不知道获取组或组描述的语法。我假设我需要简单地加载一些从UserPrincipal (如up.GetGroups() )中提取出来的集合,并对其进行枚举,但我在语法上苦苦挣扎。有了这段代码,我还需要什么才能让我获得描述。

代码语言:javascript
复制
            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,但在编写它时,我可以看到Description字段,但看不到任何其他内容。我尝试使用以下方法编写该代码:

代码语言:javascript
复制
     //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());
                }                    
            }

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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-04 14:59:57

我相信我解决了这个帖子的问题。下面是从填充对象并进入嵌套循环的代码。在移动到下一个用户之前,第一个循环通过每个用户,第二个循环通过用户所属的每个组。在每个循环中,我将参数传递给存储过程,这些存储过程使用值执行insert语句,其中一个用于用户属性,另一个用于组属性。第一个存储过程传回第二个表中用作FK的每一行的标识。

因为其中一些参数是"NotFoundYet“,因为UserPrincipal没有像DirectorySearcher第一次尝试编写这些字段时那样可用的字段。这也回答了我的问题,从Active Directory description field values not showing up,也是由我张贴。

从技术上讲,我仍然需要代码来查找那些缺失的属性,但我认为这可能会帮助其他人,而我仍在努力寻找我没有的属性。我过会儿再报告。特别感谢杰夫·罗内在这方面的帮助。

代码语言:javascript
复制
    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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21534222

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档