Question

I've read that the below code is the way to authenticate a user against WinNT. I've been trying to authenticate a user on my local machine. For what ever reason, root.NativeObject never throws an exception whether my local user's password or username is correct or not. Any idea what could be the problem ?

try
{
  using (var root = new DirectoryEntry("WinNT://" + _root, domainAndUsername, _password))
  {
     var root = root.NativeObject;
  }
}
catch
{
  return false;
}
Was it helpful?

Solution

If you want to authenticate against Active-Directory (as your tags refered to) you can test :

try
{
  using (var root = new DirectoryEntry("LDAP://societe.fr/dc=societe,dc=fr", domainAndUsername, _password))
  {
     var root = root.NativeObject;
  }
}
catch
{
  return false;
}

You can use WinNT to add a local user (SAM) on a computer as shown here under, but in this case you are logged on this computer as an administrator.

DirectoryEntry deComputer = new DirectoryEntry("WinNT://JPBASUSF1,computer");
DirectoryEntry deUser = deComputer.Children.Add("JPB", "user");
deUser.Invoke("SetPassword", new object[] { "test.2011" }); 
deUser.Properties["Description"].Add("user $userName");
deUser.Properties["userflags"].Add(512);
deUser.Properties["passwordExpired"].Add(1);
deUser.Properties["LoginScript"].Add("start.cmd");
deUser.CommitChanges();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top