Warm tip: This article is reproduced from serverfault.com, please click

c#-一位用户无法从我们的应用程序连接LDAP服务器

(c# - One user is not able to connect LDAP server from our application)

发布于 2020-12-05 11:31:50

我有一个内置于ASP.Net 3.0中的应用程序,并已升级到4.7 .Net Framework,并已部署在Web服务器上。在应用程序的“登录”页面中,我正在通过WebAPI通过LDAP验证用户凭据。

每个人都可以登录到该应用程序,但是对LDAP的身份验证仅对一个用户失败。该用户可以通过其他应用程序连接到LDAP。他能够访问那些也通过同一LDAP域对其进行身份验证的应用程序。

当我们与AD小组核对时,他们无法找到该用户的任何日志,因此怀疑LDAP服务器本身并未受到该用户的攻击。

我们正在使用以下代码连接到LDAP服务器。

 public static bool IsActiveDirectoryLoginValid(string username, string password, out string errorMessage)
    {
        bool authenticated = false;
        string _activeDirectoryDomain = "domain controller name";

        try
        {
            directoryEntry = new DirectoryEntry(string.Format("LDAP://{0}", _activeDirectoryDomain), username, password);
            DirectorySearcher ds = new DirectorySearcher(directoryEntry);
            ds.FindOne();

            authenticated = directoryEntry.NativeObject != null;
            errorMessage = !authenticated ? "Unable to authenticate user with provided username and password!" : null;

        }
        catch(Exception ex)
        {
            Logger.Error("ActiveDirectoryAuthenticationHelper : IsActiveDirectoryLoginValid " + username, ex);
            errorMessage = ex.Message;
            authenticated = false; // most commonly user name/ password incorrect
        }

        return authenticated;
    }
Questioner
Amit
Viewed
1
Amit 2020-12-11 17:54:08

如果密码中包含“#”符号,密码将从MVC发送到WebAPI,该密码将被截断。我们已更正并解决了问题。

同样,正如Fredrik建议的那样,NativeObject正在检查用户是否被授权读取活动目录信息。我删除了NativeObject并使用FindOne()方法进行搜索。