解决删除域用户Exception from HRESULT: 0x80072030

解决删除域用户异常问题。


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();
            }
        }

  

posted @ 2013-11-27 15:49  Samgle  阅读(1196)  评论(0编辑  收藏  举报