6

Vista 開発マシンでは、このコードを使用してユーザーの「管理者」パスワードを正常に変更しました。

directoryEntry.Invoke("SetPassword", "new");

Server 2008 開発マシンに移動したところ、そのコードは機能せず、次のコードを使用せざるを得ませんでした。

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" });
        }
    }
}
4

2 に答える 2

5

.NET 3.5 を使用していますか (またはアップグレードできますか)。ユーザー、グループ、コンピューターの AD 統合は、.NET 3.5 で大幅に改善されました。詳細については、MSDN の記事「Managing Directory Security Principals in the .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:可能な限り避ける必要があります。

于 2010-04-11T06:51:40.253 に答える
0

パスワードを設定するユーザー プロパティを確認します。

ユーザーはパスワードを変更できません

プロパティがチェックされてdirectoryEntry.Invoke("SetPassword", "new");いる場合、DirectoryEntry オブジェクトの作成中に管理者資格情報を使用するか、「ユーザーはパスワードを変更できません」プロパティをオフにしてパスワードを設定できません

于 2013-02-01T07:10:02.857 に答える