sho*_*app 6 c# directoryservices change-password active-directory
在Vista开发机器上,我成功使用此代码更改用户"管理员"密码:
directoryEntry.Invoke("SetPassword", "new");
Run Code Online (Sandbox Code Playgroud)
当我将它移动到我的Server 2008开发机器上时,代码不起作用,我被迫使用以下代码:
directoryEntry.Invoke("ChangePassword", new object[] { "old", "new" });
Run Code Online (Sandbox Code Playgroud)
我的问题是,为什么?
对于这两种情况,我都是这样创建了DirectoryEntry对象:
DirectoryEntry directoryEntry = new DirectoryEntry(string.Format("WinNT://{0}/{1}", computerName, username));
Run Code Online (Sandbox Code Playgroud)
谢谢!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" });
}
}
}
Run Code Online (Sandbox Code Playgroud)
你(或者你可以升级到).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();
Run Code Online (Sandbox Code Playgroud)
你完成了!该WinNT:供应商是非常有限什么可以做,如果有的话应尽可能避免.
| 归档时间: |
|
| 查看次数: |
10991 次 |
| 最近记录: |