Active Directory 该对象已存在,使用 SetPassword 和新对象 []{}
Active Directory The object already exists using SetPassword and new object []{}
我希望有人能向我解释这一点,因为我正竭尽全力解决我遇到的问题。
我收到的错误是“对象已经存在”。每当我尝试 运行 我们的 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();
}
我希望有人能向我解释这一点,因为我正竭尽全力解决我遇到的问题。
我收到的错误是“对象已经存在”。每当我尝试 运行 我们的 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();
}