从活动目录检索组下的用户列表时出错(C#中的System.DirectoryServices.AccountManagement)

时间:2014-12-04 10:24:53

标签: c# .net authentication active-directory

从Active Directory检索组下的用户时出现错误。错误描述是 {“无法检索有关域的信息(1355)”}。尝试使用.Net 4.0和.Net 4.5。 我收到错误的行被注释错误消息。

public List<DirectoryUser> GetUsersUnderGroup(string groupName)
    {
        var directoryUserList = new List<DirectoryUser>();
        string directoryServerIp="192.168.1.xxx";
        string ouName="xxxOuName";
        string domainComponents="DC=xxxComopnent1,DC=xxxComponent2";
        string directoryAdminUserId="directoryAdminuser";
        string directoryAdminPassword="directoryAdminPassword";
        using (var principalContext = principalContext = new PrincipalContext(ContextType.Domain, directoryServerIp, string.Format("OU={0},{1}", ouName, domainComponents), directoryAdminUserId, directoryAdminPassword);)
        {
            using (var group = GroupPrincipal.FindByIdentity(principalContext, groupName))
            {
                if (group != null)
                {                       
                    var users = group.GetMembers(true);
                    //Works fine till the above line. variable users is having not null value but
                    //exception while iterating through the loop.Following is the exception
                    //{"Information about the domain could not be retrieved (1355)."}
                    foreach(var user in users)
                    {                            
                        Console.Write(user.DistinguishedName);
                    }
                }
            }
        }
        return directoryUserList;
    }

创建PrincipalContext的代码在其他方案中运行良好(例如获取组列表和OU等)

1 个答案:

答案 0 :(得分:1)

本文介绍了您的问题的原因和可能的解决方案:link。基本上,如果您运行的代码不是来自域控制器所在的机器,则必须使用 DirectoryEntry 类。 本文应该可以帮助您理解该课程:link