LDAPS DirectorySearcher.FindOne抛出异常

时间:2018-05-21 17:01:57

标签: c# ldap

我正在尝试使用安全的LDAP查询验证用户。我的代码在调用

时抛出异常
SearchResult result = search.FindOne;

完整代码:

public bool IsValidUser(object username)
{
    isValidUser = false;

    try
    {
        DirectoryEntry AD = new DirectoryEntry("LDAP://office:636/", "myaccount", "mypassword");
        AD.AuthenticationType = AuthenticationTypes.SecureSocketsLayer;

        DirectorySearcher search = new DirectorySearcher(AD);
        search.SearchRoot = AD;
        search.SearchScope = SearchScope.Subtree;

        if (username == DBNull.Value == false & username != "")
        {
            search.Filter = "(&(objectClass=user) (sAMAccountName=" + username + "))";
            SearchResult result = search.FindOne();

            if ((result == null))
                isValidUser = false;
            else
                isValidUser = true;
        }
        else
            isValidUser = false;
    }
    catch (Exception ex)
    {
       // log(ex);
    }

    return isValidUser;
}

如果我没有提供端口号636,它会正常工作但我不认为LDAP是在安全网络下完成的。

1 个答案:

答案 0 :(得分:0)

也许它可以帮助某人(或将来对我有帮助)-域控制器有时会损坏,如果这样做会使其无法使用安全端口636,请尝试更改为非安全端口389,以查看情况是否如此。

另请参阅:"The server is not operational" domain controller configuration error when you configure a server by using Server Manager