Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更改密码的DirectoryEntry:Vista/Server2008之间的不同行为_C#_Active Directory_Directoryservices_Change Password - Fatal编程技术网

C# 更改密码的DirectoryEntry:Vista/Server2008之间的不同行为

C# 更改密码的DirectoryEntry:Vista/Server2008之间的不同行为,c#,active-directory,directoryservices,change-password,C#,Active Directory,Directoryservices,Change Password,在Vista dev计算机上,我成功地使用此代码更改了用户“管理员”密码: directoryEntry.Invoke("SetPassword", "new"); 当我将其移到我的Server 2008 dev机器上时,该代码不起作用,我被迫使用以下代码: directoryEntry.Invoke("ChangePassword", new object[] { "old", "new" }); 我的问题是,为什么 对于这两种情况,我都创建了DirectoryEntry对象: Direc

在Vista dev计算机上,我成功地使用此代码更改了用户“管理员”密码:

directoryEntry.Invoke("SetPassword", "new");
当我将其移到我的Server 2008 dev机器上时,该代码不起作用,我被迫使用以下代码:

directoryEntry.Invoke("ChangePassword", new object[] { "old", "new" });
我的问题是,为什么

对于这两种情况,我都创建了DirectoryEntry对象:

DirectoryEntry directoryEntry = new DirectoryEntry(string.Format("WinNT://{0}/{1}", computerName, username));
谢谢!(八)

如果你们觉得有用,下面是实际代码

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" });
        }
    }
}
您是否(或是否可以升级到).NET3.5?在.NET3.5中,用户、组和计算机的广告集成得到了极大的改进-有关详细信息,请查看MSDN文章

在您的情况下,您可以执行以下操作:

// 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:
提供程序的功能非常有限,应尽可能避免使用。

如果需要,请检查要为其设置密码的用户属性

用户无法更改密码


属性,则无法使用directoryEntry.Invoke(“SetPassword”、“new”)设置密码在创建DirectoryEntry对象时使用管理员凭据,或取消选中“用户无法更改密码”属性

您如何知道它失败的任何详细信息-例如,是否存在异常?哦,如果存在异常,请发布整个异常?也就是说,发布ex.ToString()的完整输出。谢谢!我喜欢此解决方案,并将使用它来交换WinNT提供程序。我是否需要旧密码来设置新密码?