zoukankan      html  css  js  c++  java
  • 扫描不同域下的AD账户进行删除

     public ResultModel GetEntryOneToDel(string sAMAccountName)
            {
                bool del=false;
                ResultModel result = new ResultModel();
                foreach (var ldap in LDAPStore)
                {
                    ADHelper.DomainName = ldap.DomainName;
                    ADHelper.LDAPDomain = ldap.LDAPDomain; //ADHelper.DomainName = ldap.LDAPDomain;
                    ADHelper.ADPath = ldap.ADPath;
                    ADHelper.ADUser = ldap.ADUser;
                    ADHelper.ADPassword = ldap.ADPassword;
                    if (ADHelper.IsAccExists(sAMAccountName))
                    {
                        del = true;
                        break;
                    }
                }
                if (del == true)
                {
                    
                    DirectoryEntry de = null;
                    DirectoryEntry userEntry = new DirectoryEntry();
                    try
                    {
                        de = ADHelper.GetDirectoryObject();
                        DirectorySearcher deSearch = new DirectorySearcher(de);
                        deSearch.Filter = "(&(&(objectCategory=person)(objectClass=user))(sAMAccountName=" +
                            sAMAccountName + "))";       // LDAP 查询串
                        SearchResult sr = deSearch.FindOne();
                        userEntry = sr.GetDirectoryEntry();
                        de = userEntry.Parent;
                        de.Children.Remove(userEntry);
                        de.CommitChanges();
                        de.Dispose();
                        userEntry.Dispose();
                        result.ExecResult = ExecResult.Success;
                        return result;
                    }
                    catch (Exception ex)
                    {
                        de.Dispose();
                        userEntry.Dispose();
                        result.ExecResult = ExecResult.Failure;
                        LogHelper.WriteLog(new LogModel(Level.Error, DateTime.Now, "del ADAccount is error: " + ex.Message));
                        return result;
                    }
                }
                else
                {
                    result.ExecResult = ExecResult.Failure;
                    result.ResultMessage = "无此帐号";
                    LogHelper.WriteLog(new LogModel(Level.Error, DateTime.Now, "del ADAccount is not  Exists" ));
                    return result;
                }
    
            }
    

    利用ADHelper提供的共有方法,先扫描所有域(格式XML),如果存在SAMAccountName,则返回True,利用
    DirectoryEntry ,和过滤器等类方法进行删除,像是找一个同学,不需要知道年级、班,只需要知道学生编号就好,因为即使手里掌握不同的学校名(域名),因编号是唯一的,也能找到他。

    
    
  • 相关阅读:
    RecycleView使用心得【2】
    URL解析
    CSS 动画总结
    包含块 width 和 height 值的总结
    JS 获取页面大小
    常见跨域方法原理及其用例
    CSS 计数器
    JS 对象总结
    JS 原型以及原型链
    关于未能找到源文件“.NETFramework,Version=v4.0.AssemblyAttributes.cs”问题
  • 原文地址:https://www.cnblogs.com/shiningleo007/p/6907411.html
Copyright © 2011-2022 走看看