How can I search Active Directory by username using C#?

后端 未结 5 1796
名媛妹妹
名媛妹妹 2021-01-05 03:53

I\'m trying to search active directory by the username \'admin\'. I know for a fact that there is a user with that username in the directory, but the search keeps coming bac

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-05 04:13

    if you want to stick to DirectorySearcher, try searching by cn or samaccountname instead

    var attributeName = "cn";
    var searchString = "admin"
    var ent = new DirectoryEntry("LDAP://"dc=corp,dc=contoso,dc=com")
    var mySearcher = new DirectorySearcher(ent);
    mySearcher.Filter = string.Format("(&(objectcategory=user)({0}={1}))", attributeName, searchString);
    
    var userResult = mySearcher.FindOne();
    

提交回复
热议问题