简体   繁体   中英

how to get the username of a SearchResultEntry

I am implementing the dirSync polling for changes within active directory, using ldap. It is working fine however i would like to get the username attribute ("samaccountname") associated with the SearchResultEntry object in addition to the changes that would be detected

You must request the attributes as part of the search request. If the attribute(s) you desire are transmitted to the server as past of the search request, and if the server allows the authentication state of the connection to retrieve those attributes, then those attributes will be included in the search result. For more information, see LDAP: Programming Practices and Using ldapsearch . The latter refers to the command-line ldapsearch tool, but the concepts are valid for any language.

SearchResult result; // this would've been defined elsewhere
if(result != null)
{
    DirectoryEntry entry = result.GetDirectoryEntry();
    string name = (string)entry["SAMAccountName"].Value;
}

This will allow you to get the name from the user and stores it in name and assumes you have a SearchResult named result already populated from some search on the directory.

Edit: I realized that this isn't what you asked for, you are using SearchResultEntry and not the DirectoryServices.SearchResult. I'll leave this here just in case it may help but I apologize for not properly reading the original question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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