【问题标题】:Active Directory The object already exists using SetPassword and new object []{}Active Directory 对象已经存在,使用 SetPassword 和新对象 []{}
【发布时间】:2021-02-25 16:59:13
【问题描述】:

我希望有人可以向我解释这一点,因为我正在努力解决我遇到的问题。

我收到的错误是“对象已经存在”。每当我尝试运行我们的 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。

【问题讨论】:

    标签: c# asp.net active-directory


    【解决方案1】:

    您需要在 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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-08
      相关资源
      最近更新 更多