DirectoryEntry para cambiar la contraseña: comportamiento diferente entre Vista / Server2008
En una máquina de desarrollo de Vista utilicé este código con éxito para cambiar la contraseña de "Administrador" del usuario:
directoryEntry.Invoke("SetPassword", "new");
Cuando lo moví a mi máquina de desarrollo Server 2008, ese código no funcionó, y me vi obligado a usar el siguiente código:
directoryEntry.Invoke("ChangePassword", new object[] { "old", "new" });
Mi pregunta es, ¿por qué?
Para ambos casos, creé mi objeto DirectoryEntry como tal:
DirectoryEntry directoryEntry = new DirectoryEntry(string.Format("WinNT://{0}/{1}", computerName, username));
¡Gracias! 8)
En caso de que lo encuentren útil, aquí está el código real.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.DirectoryServices;
using System.Security.Principal;
namespace AccountMod
{
class Program
{
static void Main()
{
Console.WriteLine("Attempting reset...\n");
try
{
String machineNameAndUser = WindowsIdentity.GetCurrent().Name.ToString();
String machineName = WindowsIdentity.GetCurrent().Name.ToString().Substring(0, machineNameAndUser.IndexOf('\\'));
Console.WriteLine("Computer's name: " + machineName);
ResetPassword(machineName, "Administrator", "new");
//ChangePassword("Administrator", "current", "new"); Console.WriteLine("Finished...");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
Console.WriteLine(e.InnerException);
}
Console.ReadKey();
}
public static void ResetPassword(string computerName, string username, string newPassword)
{
DirectoryEntry directoryEntry = new DirectoryEntry(string.Format("WinNT://{0}/{1}", computerName, username));
directoryEntry.Invoke("SetPassword", newPassword);
//directoryEntry.Invoke("ChangePassword", new object[] { "current", "new" });
}
}
}