简体   繁体   English

C# 未能在 LDAP 中加载正确的用户属性

[英]C# failed to load right user attribute in LDAP

i'm not able to retrieve the right user attributes from LDAP using the code below:我无法使用以下代码从 LDAP 检索正确的用户属性:

        string login = "UID=" + txtUsername.Text + ",DC=example,DC=com";
        string password = txtPwd.Text;
        string domain = txtDomain.Text;
        int port = Convert.ToInt32(txtPort.Text);
        string searchBase = "DC=example,DC=com";
        string searchFilter = "(objectclass=person)";

        LdapConnection conn = new LdapConnection();

        try
        {
            conn.Connect(domain, port);
            conn.Bind(login, password); 

            HashSet<string> users = new HashSet<string>();
            LdapSearchResults searchResults = conn.Search(searchBase,
                                                LdapConnection.SCOPE_SUB,
                                                searchFilter,
                                                null,
                                                false);

            while (searchResults.hasMore())
            {
                var nextEntry = searchResults.next();
                nextEntry.getAttributeSet();
                var attr = nextEntry.getAttribute("cn");

                if (attr == null)
                {
                    users.Add(nextEntry.getAttribute("mail").StringValue);
                }
                else
                {
                    users.Add(attr.StringValue);
                }

                Session["Name"] = users.First();

                Response.Redirect("~/default.aspx");
            }
        }
        catch (LdapException ex)
        {
            lblErr.Visible = true;
            lblErr.Text = "Error authenticating: " + ex.LdapErrorMessage;
            return;
        }
        catch (Exception ex)
        {
            lblErr.Visible = true;
            lblErr.Text = "Error authenticating: " + ex.Message;
        }
        finally
        {
            conn.Disconnect();
        }

for example i want to get attributes of user named Albert Einstein but i always get attributes of Isaac Newton no matter what username i inputted例如,我想获取名为 Albert Einstein 的用户的属性,但无论我输入什么用户名,我总是得到 Isaac Newton 的属性

i'm using this reference: How to find a User's Group with LDAP in C# Core 2我正在使用这个参考: How to find a User's Group with LDAP in C# Core 2

i'm using ForumSYS's public LDAP server, for domain it should be ldap.forumsys.com and port is 389我正在使用 ForumSYS 的公共 LDAP 服务器,对于域它应该是ldap.forumsys.com和端口是389

When you say "no matter what username i inputted", are you referring to txtUsername.Text ?当您说“无论我输入什么用户名”时,您指的是txtUsername.Text吗? Because you're using that only to authenticate, not to search.因为您仅使用它进行身份验证,而不是搜索。 You're searching for every user in the directory because you set the filter to (objectclass=person) .您正在搜索目录中的每个用户,因为您将过滤器设置为(objectclass=person)

If you only want to find one user, then set the filter to only find that one user.如果您只想查找一位用户,则将过滤器设置为仅查找该一位用户。 For example:例如:

string searchFilter = "(cn=Albert Einstein)";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM