Indicate whether the user exist in AD

 static bool IsExistInAD(UserInfo user)
        {
            DirectorySearcher search = new DirectorySearcher();
            search.Filter = String.Format("(SAMAccountName={0})", user.SAPID);
            SearchResult result = search.FindOne();

            if (result == null)
            {
                return false;
            }
            else
            {
                DirectoryEntry entry = result.GetDirectoryEntry();
                //foreach (var temp in entry.Properties.PropertyNames)
                //{
                //    Console.Write("" + temp.ToString() + ": " + entry.Properties[temp.ToString()][0] + Environment.NewLine);
                //}
                (entry.Properties["title"]).Value = user.Title;
                (entry.Properties["company"]).Value = "Company";
                (entry.Properties["telephoneNumber"]).Value ="+86 (21) "+ user.Tele;
                (entry.Properties["mobile"]).Value = "+86 "+user.Mobile;
                (entry.Properties["streetAddress"]).Value = "........";
                (entry.Properties["postalCode"]).Value = "222221";
               
                entry.CommitChanges();
                Console.WriteLine(user.SAPID);
                return true;
            }
        }

posted @ 2011-03-08 16:12  流流  阅读(189)  评论(0编辑  收藏  举报