Pregunta

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;
}
¿Fue útil?

Solución

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();
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top