更改密码 Windows AD C#
Change Password Windows AD C#
下面是我正在使用的代码:即使我正在模拟 Administrators 组中的帐户,我的访问也被拒绝。
SafeTokenHandle safeTokenHandle;
string userName, domainName;
// Get the user token for the specified user, domain, and password using the
// unmanaged LogonUser method.
// The local machine name can be used for the domain name to impersonate a user on this machine.
const int LOGON32_PROVIDER_DEFAULT = 0;
//This parameter causes LogonUser to create a primary token.
const int LOGON32_LOGON_INTERACTIVE = 2;
// Call LogonUser to obtain a handle to an access token.
bool returnValue = LogonUser(username, domain, password,
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, out safeTokenHandle);
if (false == returnValue)
{
int ret = Marshal.GetLastWin32Error();
}
using (safeTokenHandle)
{
using (WindowsImpersonationContext impersonatedUser = WindowsIdentity.Impersonate(safeTokenHandle.DangerousGetHandle()))
{
string x = WindowsIdentity.GetCurrent().Name;
PrincipalContext pc = new PrincipalContext(ContextType.Domain);
UserPrincipal up = UserPrincipal.FindByIdentity(pc, username);
up.SetPassword(txtNewChangedPassword.Text);
}
这周模仿是怎么回事? PrincipalContext
对象有一个接受用户凭据的构造函数。您需要做的就是:
PrincipalContext pc = new PrincipalContext(ContextType.Domain, domain, username, password);
UserPrincipal up = UserPrincipal.FindByIdentity(pc, username);
up.SetPassword(txtNewChangedPassword.Text);
SetPassword
要求您的代码是 运行 的用户才能成为 Active Directory 中的管理员。由于您已经有了旧密码,请尝试替换此行:
up.SetPassword(txtNewChangedPassword.Text);
有了这个:
up.ChangePassword(password, txtNewChangedPassword.Text);
up.Save();
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, domain, username, password))
{
//PrincipalContext pc = new PrincipalContext(ContextType.Domain, domain, username, password);
UserPrincipal up = new UserPrincipal(pc);
up.SetPassword(newPassword);
}
下面是我正在使用的代码:即使我正在模拟 Administrators 组中的帐户,我的访问也被拒绝。
SafeTokenHandle safeTokenHandle;
string userName, domainName;
// Get the user token for the specified user, domain, and password using the
// unmanaged LogonUser method.
// The local machine name can be used for the domain name to impersonate a user on this machine.
const int LOGON32_PROVIDER_DEFAULT = 0;
//This parameter causes LogonUser to create a primary token.
const int LOGON32_LOGON_INTERACTIVE = 2;
// Call LogonUser to obtain a handle to an access token.
bool returnValue = LogonUser(username, domain, password,
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, out safeTokenHandle);
if (false == returnValue)
{
int ret = Marshal.GetLastWin32Error();
}
using (safeTokenHandle)
{
using (WindowsImpersonationContext impersonatedUser = WindowsIdentity.Impersonate(safeTokenHandle.DangerousGetHandle()))
{
string x = WindowsIdentity.GetCurrent().Name;
PrincipalContext pc = new PrincipalContext(ContextType.Domain);
UserPrincipal up = UserPrincipal.FindByIdentity(pc, username);
up.SetPassword(txtNewChangedPassword.Text);
}
这周模仿是怎么回事? PrincipalContext
对象有一个接受用户凭据的构造函数。您需要做的就是:
PrincipalContext pc = new PrincipalContext(ContextType.Domain, domain, username, password);
UserPrincipal up = UserPrincipal.FindByIdentity(pc, username);
up.SetPassword(txtNewChangedPassword.Text);
SetPassword
要求您的代码是 运行 的用户才能成为 Active Directory 中的管理员。由于您已经有了旧密码,请尝试替换此行:
up.SetPassword(txtNewChangedPassword.Text);
有了这个:
up.ChangePassword(password, txtNewChangedPassword.Text);
up.Save();
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, domain, username, password))
{
//PrincipalContext pc = new PrincipalContext(ContextType.Domain, domain, username, password);
UserPrincipal up = new UserPrincipal(pc);
up.SetPassword(newPassword);
}