【问题标题】:Using C# to authenticate user against LDAP使用 C# 对 LDAP 用户进行身份验证
【发布时间】:2012-07-18 16:30:22
【问题描述】:

我正在使用 DirectorySearcher 在 LDAP 服务器中搜索用户条目。

DirectoryEntry de = new DirectoryEntry();
de.Path = "LDAP://myserver/OU=People,O=mycompany";
de.AuthenticationType = AuthenticationTypes.None;

DirectorySearcher deSearch = new DirectorySearcher();

deSearch.SearchRoot = de;
deSearch.Filter = "(uid=" + model.UserName + ")";

SearchResult result = deSearch.FindOne();

我能够在结果变量中获得预期的输出。
但是,如果我尝试通过在目录条目中提供密码来验证同一用户,我总是会收到以下错误。

“用户名或密码不正确。”

DirectoryEntry entry = new DirectoryEntry("LDAP://myserver/OU=People,O=mycompany", username, password);
DirectorySearcher search = new DirectorySearcher(
    entry,
    "(uid=" + username + ")",
    new string[] { "uid" }
);

search.SearchScope = System.DirectoryServices.SearchScope.Subtree;
SearchResult found = search.FindOne();   ->>>>>this is where I get wrong credential error.

用户名和密码用于我要验证的用户。

谁能告诉我我在这里做错了什么或如何调试它。

【问题讨论】:

  • 您的 LDAP 服务器在查询之前是否需要身份验证?
  • 不,搜索不需要身份验证。我也可以以匿名用户身份搜索。我有一个基于 Web 的工具,我需要在其中实现 LDAP 身份验证,以便只有真正的用户才能访问它。

标签: c# authentication ldap


【解决方案1】:

此用户名、密码在这一行:

DirectoryEntry("LDAP://myserver/OU=People,O=mycompany", username, password);

应该用于具有目录查找权限的帐户。它可以是服务帐户或测试目的,请尝试使用您自己的帐户。这不应该是您尝试进行身份验证的人的用户/通行证。

如果要进行身份验证,可以使用 PrincipalContext 执行以下步骤:

using(var context = new PrincipalContext(ContextType.Domain, "mydomain", "mydomain\serviceAcct", "serviceAcctPass")) {
 //Username and password for authentication.
 return context.ValidateCredentials(username, password); 
}

"serviceAcct" = 域用户中具有目录查找权限的帐户。 "serviceAcctPass" = 该服务帐户的密码。 正如我所说,对于测试,您可以尝试使用自己的用户/传递上下文。

另外,请确保提供的用户名具有“域\用户名”或“用户名@域”格式。

【讨论】:

  • 好的,所以我在这里提供的是错误的。你能告诉我应该怎么做才能通过提供用户名和密码来验证任何普通用户的身份吗?或将我重定向到一些教程或链接。谢谢
  • 从您在上面发布的另一条评论中读取,您的域似乎不需要经过身份验证的用户进行查找。如果是这种情况,您可能可以使用 PrincipalContext 的默认构造函数而不是用户/传递构造函数。
  • 如何同时使用directoryEntry和principlacontext? bcz 如果我尝试在 principalcontext 中使用 Ldap,它会带来无法连接到系统的错误
  • 这怎么会指向位于LDAP://myserver/OU=People,O=mycompany 的LDAP 服务器?看起来 LDAP 服务器应该在当前域中才能正常工作。
  • 注意 DirectoryEntry("LDAP://myserver/OU=People,O=mycompany", username, password);同时针对 SamAccountName 或“mail”进行身份验证,也就是说,电子邮件地址也可以用作用户名。
【解决方案2】:

这里我们获取活动目录用户详细信息,我们可以使用 web.config 文件中的 DomainName 和 UserRole

bool isAdmin = false;
        RegisterInput model = new RegisterInput();
        NewUserInput usr = new NewUserInput();
        SearchResultCollection results;
        string mobileNumber = string.Empty;
        using (DirectoryEntry domainEntry = new DirectoryEntry("LDAP://" + AppSettings.DomainName))
        {
            using (DirectorySearcher searcher = new DirectorySearcher(domainEntry, "userPrincipalName=" + userName + "@" + AppSettings.DomainName) { Filter = string.Format("(&(objectClass=user)(samaccountname={0}))", userName) })
            {
               results = searcher.FindAll();

                if (results.Count > 0)
                {
                    usr.FirstName = results[0].GetDirectoryEntry().Properties["givenName"].Value.ToString();
                    usr.LastName = results[0].GetDirectoryEntry().Properties["sn"].Value?.ToString();
                    usr.EmailAddress = results[0].GetDirectoryEntry().Properties["mail"].Value?.ToString();
                    mobileNumber = results[0].GetDirectoryEntry().Properties["mobile"]?.Value?.ToString();
                    dynamic userRoleList = results[0].GetDirectoryEntry().Properties["memberOf"];

                    if (userRoleList != null)
                    {
                        foreach (var role in userRoleList)
                        {
                            string[] split = role.ToString().Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                            bool result = split.Any(x => x.ToLowerInvariant() == AppSettings.UserRole.ToLowerInvariant());
                            if (result)
                            {
                                isAdmin = true;
                                break;
                            }
                        }
                    }
                }
            }
        }

        model.NewUser = usr;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-28
    • 2012-08-23
    • 2012-10-02
    • 1970-01-01
    相关资源
    最近更新 更多