UserPrincipal.Save()的神奇BUG

來源:互聯網
上載者:User

      我今天有夠淒慘的,誤刪了客戶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方法後消失。

      哎……

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.