zoukankan      html  css  js  c++  java
  • UserPrincipal.Save()的神奇BUG

          我今天有够凄惨的,误删了客户AD上的上千个用户,发现误删后我一直觉得很纳闷,因为根据程序记录的日志上来看,对误删的用户执行的操作全都是新增,执行新增的结果是捕获到“PrincipalExistsException,对象已存在”的异常,新增失败后没有执行任何操作AD的代码。很仔细的检查了程序,是不可能执行了删除操作的,奇异的是,每次执行UserPrincipal.Save()方法后,账号就消失了!!?

         接下来做了个小程序仔细测试,先把测试用的代码贴上:

        static void Main(string[] args)
        {
            string container = "DC=contosocorp,DC=local";
            using (PrincipalContext context = new PrincipalContext(ContextType.Domain, "contosocorp.local", container,
                ContextOptions.Negotiate, "administrator", "p@ssw0rd"))
            {
                string emailPrefix = "zhanqing";
                string password = "p@ssw0rd";
                string name = "詹青";
                string id = "56447";
    
                UserPrincipal userPri = new UserPrincipal(context, emailPrefix, password, true);
                userPri.Description = id;
                userPri.DisplayName = name + id;
                userPri.Name = name + id;
                userPri.SamAccountName = emailPrefix;
                userPri.UserPrincipalName = emailPrefix + "@" + "contosocorp.local";
    
                try
                {
                    userPri.Save();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
    
            Console.Read();
        }

          用了好几个AD中现存的帐号测试,没有出现一保存后账户就消失的情况,后面仔细分析,发现与正式程序唯一不同的地方在于:测试代码中context关联的容器始终是AD根目录容器,而正式发布程序中context关联的容器是账户所在OU的容器,把container的值改一下试试,与账户"zhanqing”所在OU保持一致:

          string container = "DC=信息技术总部,DC=contosocorp,DC=local";

          结果着实让我吃惊,当执行到Console.WriteLine(ex.Message)后,AD中的账户"zhangqing”被删除了!

          这是我无法理解的现象。

          之后再进行了一些尝试,发现当把 userPri.Name = name + id 注释掉后,哪怕context关联的容器与AD中账户所在OU的容器一致,调用Save方法时账户不会被删除,然而只要userPri.Name的值与AD中账户的Name一致,且container与所在OU的容器一致,账户就会在调用Save方法后消失。

          哎……

  • 相关阅读:
    How to convert VirtualBox vdi to KVM qcow2
    (OK)(OK) adb -s emulator-5554 shell
    (OK)(OK) using adb with a NAT'ed VM
    (OK) How to access a NAT guest from host with VirtualBox
    (OK) Creating manually one VMs from an existing VDI file in CLI (VBoxManage) in Fedora 23
    (OK)(OK) Creating VMs from an existing VDI file in CLI (VBoxManage) in Fedora 23
    (OK) Creating_VMs_from_an_existing_VDI_file.txt
    (OK) Creating VMs from an existing VDI file —— in OS X
    (OK) install_IBM_SERVER.txt
    (OK) install chrome & busybox in android-x86_64 —— uninstall chrome
  • 原文地址:https://www.cnblogs.com/sdlfx/p/2032172.html
Copyright © 2011-2022 走看看