I would like to use the AccountManagement
namespace introduced in .NET 3.5 to find a user and set their password. However, the ADLDS server is not part of our company domain so I'm using ContextType.Machine
. When I search for the user it's never found (I suspect it's searching in the wrong container, but according to the documentation when using ContextType.Machine
you can't specify a container).
using (var context = new PrincipalContext(ContextType.Machine, "test-server", null, "username", "password")) {
using (var u = UserPrincipal.FindByIdentity(context, "SuperAdmin")) {
//u is always null. :(
}
}
However, I know I can find the user using plain ol' DirectoryEntry
:
using (var de = new DirectoryEntry("LDAP://test-server:389/CN=SuperAdmin,CN=SuperUsers,OU=test-server,DC=foo,DC=bar,DC=com", "username", "password", AuthenticationTypes.Secure)) {
//The user is found, but SetPassword fails with "The directory property cannot be found in the cache"
de.Invoke("SetPassword", new object[] { "foobar" });
}
One last thing to point out is that I can use ADSI Edit to change the password with these same credentials. Is it possible to use the newer directory objects to perform this search?