C# Active directory列表组-LdapConnection起作用,PrincipalContext不起作用

C# Active directory列表组-LdapConnection起作用,PrincipalContext不起作用,c#,active-directory,ldap,C#,Active Directory,Ldap,创建一个与Active Directory对话的web服务,以验证用户并确定他们所属的组 我从验证过程开始,并实现了这一点: public bool AuthenticateAdUser(string username, string password) { //in the real code, these come from config string domain = "TestDomain"; string server =

创建一个与Active Directory对话的web服务,以验证用户并确定他们所属的组

我从验证过程开始,并实现了这一点:

    public bool AuthenticateAdUser(string username, string password)
    {
        //in the real code, these come from config
        string domain = "TestDomain";
        string server = 666.666.666.666;
        string authType = "Basic";
        string useSsl = "false";

            AuthType atype = (AuthType)Enum.Parse(typeof(AuthType), authType);

            using (var ldapConnection = new LdapConnection(server))
            {
                var networkCredential = new NetworkCredential(username, password, domain);
                ldapConnection.SessionOptions.SecureSocketLayer = Convert.ToBoolean(useSsl);
                ldapConnection.AutoBind = false;
                ldapConnection.AuthType = atype;
                ldapConnection.Bind(networkCredential);
            }

            // If the bind succeeds, the credentials are valid
            return true;
    }
但是,我不清楚如何使用LdapConnection对象处理组。文档和示例建议您为此使用PrinicpalContext。所以我试过这个

        string domain = "TestDomain";

            using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, domain))
            {
                using (PrincipalSearchResult<Principal> src = UserPrincipal.FindByIdentity(pc, username).GetGroups(pc))
                {
                    src.ToList().ForEach(sr => result.Add(sr.SamAccountName));
                }
            }
string domain=“TestDomain”;
使用(PrincipalContext pc=new PrincipalContext(ContextType.Domain,Domain))
{
使用(PrincipalSearchResult src=UserPrincipal.FindByIdentity(pc,用户名).GetGroups(pc))
{
src.ToList().ForEach(sr=>result.Add(sr.SamAccountName));
}
}
此操作失败,声称无法联系Active Directory服务器。使用DNS样式名称(“TestDomain.local”)似乎没有帮助

这至少会加速网络主体:

        string server = "666.666.666.666";

            using (PrincipalContext pc = new PrincipalContext(ContextType.Machine, server))
            {
                using (PrincipalSearchResult<Principal> src = UserPrincipal.FindByIdentity(pc, username).GetGroups(pc))
                {
                    src.ToList().ForEach(sr => result.Add(sr.SamAccountName));
                }
            }
string server=“666.666.666.666”;
使用(PrincipalContext pc=new PrincipalContext(ContextType.Machine,server))
{
使用(PrincipalSearchResult src=UserPrincipal.FindByIdentity(pc,用户名).GetGroups(pc))
{
src.ToList().ForEach(sr=>result.Add(sr.SamAccountName));
}
}
但当您尝试使用它做任何事情时,它都会失败,出现“未找到网络路径”


关于主体无法工作的原因,或者我如何使用LdapConnection查询组,有什么想法吗?

运行程序的计算机是否已加入测试域?没有。但它应该可以访问它。这个软件最终应该促进不同域之间的通信。如果我在一台未加入域的计算机上尝试你的代码,我会得到同样的例外。堆栈跟踪显示一个内部帐户管理类无法确定域控制器。如果我从一台加入域的计算机上下载了代码,一切都很好。谢谢你的检查-这至少节省了我尝试错误解决方案的时间。将转而使用DirectoryServices。如果你把那条评论作为回答,我会打勾。