На Vista dev машина я использовал этот код успешно для изменения пользовательского пароля "Администратора":
directoryEntry.Invoke("SetPassword", "new");
Когда я переместил его в мой Сервер 2008 dev машина, что код не работал, и я был вынужден использовать следующий код:
directoryEntry.Invoke("ChangePassword", new object[] { "old", "new" });
Мой вопрос, почему?
Для обоих случаев я создал свой объект DirectoryEntry как таковой:
DirectoryEntry directoryEntry = new DirectoryEntry(string.Format("WinNT://{0}/{1}", computerName, username));
Спасибо! 8)
В случае, если Вы, парни находят это полезным, вот фактический код.
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" });
}
}
}
Вы (или можете ли вы выполнить обновление до) .NET 3.5? Интеграция AD для пользователей, групп и компьютеров в .NET 3.5 была значительно улучшена - подробности можно найти в статье MSDN Управление принципами безопасности каталогов в .NET Framework 3.5 .
В вашем случае вы можете сделать что-то вроде:
// establish context for local machine
PrincipalContext ctx = new PrincipalContext(ContextType.Machine);
// find the "Administrator" account
UserPrincipal admin = UserPrincipal.FindByIdentity(ctx, "Administrator");
// set the password to a new value
admin.SetPassword("new-top-secret-password");
admin.Save();
и готово! Провайдер WinNT:
очень ограничен в возможностях, и его следует по возможности избегать.