解決刪除域使用者Exception from HRESULT: 0x80072030

SamShum發表於2013-11-27

解決刪除域使用者異常問題。


System.DirectoryServices.DirectoryServicesCOMException was unhandled
  Message=在伺服器上沒有這樣一個物件。 (Exception from HRESULT: 0x80072030)
  Source=System.DirectoryServices
  ErrorCode=-2147016656
  ExtendedError=8333
  ExtendedErrorMessage=0000208D: NameErr: DSID-031001CD, problem 2001 (NO_OBJECT), data 0, best match of:
    'CN=Users,DC=samshum,DC=com'

 

解決方案程式碼:

        private const string AdPath= "LDAP://192.168.8.151";
        private const string AdUser= "administrator";
        private const string AdPassWord= "1231112123";

        /// <summary>
        /// 刪除指定使用者
        /// </summary>
        /// <param name="sAMAccountName"></param>
        public void DeleteUserByAccount(string sAMAccountName)
        {
            GetCommonName(sAMAccountName);
        }

        /// <summary>
        /// 刪除指定使用者,通過使用者全稱
        /// </summary>
        /// <param name="NickName"></param>
        public void DeleteUserByNickName(string NickName)
        {
            using (DirectoryEntry entry = new DirectoryEntry(ADPath, ADUser, ADPassword))
            {
                using (DirectorySearcher Search = new DirectorySearcher())
                {
                    Search.SearchRoot = entry;
                    Search.Filter = "(&(objectClass=user) (cn=" + NickName + "))";
                    SearchResult Result = Search.FindOne();
                    using (DirectoryEntry child = Result.GetDirectoryEntry())
                    {
                        child.DeleteTree();
                        child.Close();
                        child.Dispose();
                    }
                    Search.Dispose();
                }
                entry.Close();
                entry.Dispose();
            }
        }

  

相關文章