简体   繁体   中英

Check list of users exist in Active Directory using LDAP in C#

I need to pass a list of user's SamAcountName / UserPrincipalName and check if those users do exist in Active Directory.

I know that we can check only one item whether it exist or not using following method. But I need to pass a whole list of SamAcountName / UserPrincipalName and check whether users exist.

public bool UserExist(string samAccountName)
{
    try
    {
        using (var domainContext = GetPrincipalContext())
        {
            using (var foundUser = UserPrincipal.FindByIdentity(domainContext, IdentityType.SamAccountName, samAccountName))
            {
                return foundUser != null;
            }
        }
    }
    catch (Exception e)
    {
        _logger.LogWarning($"{nameof(CheckUserExistBySAM)}: Exception: {e}");
        return false;
    }
}

Try use something like:

UserPrincipal u = new UserPrincipal(domainContext);
PrincipalSearcher search = new PrincipalSearcher(u);
var usersInYourAD = search.FindAll();    //merge this with your list of users etc.

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