Active Directory对象已经存在,使用SetPassword和new object[]{}



我希望有人能给我解释一下,因为我正试图解决我遇到的一个问题。

我收到的错误是,"对象已经存在。"每当我试图运行我们的ResetPassword函数。奇怪的是,我只收到这个错误,如果用户帐户有他们的密码重置之前。如果我用新密码创建一个新帐户,或者在数据库中搜索一个帐户上没有调用ResetPassword函数的用户,那么它将允许我调用这个函数一次。注意:在这一次,它让我运行密码重置的功能。任何已经运行ResetPassword的帐户都会提示对象已经存在错误。

public static void ResetPassword(DirectoryEntry User, string password)
{
User.Invoke("SetPassword", new object[] { password });
User.Properties["LockOutTime"].Value = 0x0000; //unlock account
int val = (int)User.Properties["userAccountControl"].Value;
User.Properties["userAccountControl"].Value = val & ~0x2; //Enable account 
User.CommitChanges();
Logs.CreateLogEntry("Reset password", User);
User.Close();
}

可以看到,我们传入了一个DirectoryEntry用户,以及一个生成的新密码。我们在IIS网站的后端使用匿名登录,以获得足够高的管理员凭据来使用SetPassword。

您需要提供AD中具有管理员权限的用户的凭据来重置密码。

public static void ResetPassword(string username, string password)
{
string adminUser = "YourAdminUserIdInAD";
string adminPass = "YourAdminUserPasswordInAD";
string ldapString = "LDAP://YourLDAPString";
DirectoryEntry dEntry = new DirectoryEntry(ldapString , adminUser, adminPass, AuthenticationTypes.Secure);
DirectorySearcher deSearch = new DirectorySearcher(dEntry) {
SearchRoot = dEntry, 
Filter = "(&(objectCategory=user)(cn=" + username + "))"
};
var directoryEntry = deSearch.FindOne();
directoryEntry.Invoke("SetPassword", new object[] { password });
directoryEntry.Properties["LockOutTime"].Value = 0x0000; //unlock account
int val = (int)directoryEntry.Properties["userAccountControl"].Value;
directoryEntry.Properties["userAccountControl"].Value = val & ~0x2;
directoryEntry.CommitChanges();
Logs.CreateLogEntry("Reset password", User);
directoryEntry.Close();
}

最新更新