i'm not able to retrieve the right user attributes from LDAP using the code below:
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
i'm using this reference: 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