write mobile to ad

  public  static string path = "LDAP://192.168.8.1";
        public  static string admin = "administrator";
        public  static string pwd = "";


        public static DirectoryEntry createDirectoryEntry(string admin, string pwd)
        {
            DirectoryEntry ldapConnection = new DirectoryEntry();
            ldapConnection.Path = path;
            ldapConnection.AuthenticationType = AuthenticationTypes.Secure;
            ldapConnection.Username = admin;
            ldapConnection.Password = pwd;
            return ldapConnection;
        }

        public static DirectoryEntry FindByName(string name)
        {
            var de = createDirectoryEntry(admin, pwd);
            DirectorySearcher search = new DirectorySearcher(de);
            search.Filter = "(cn=" + name + ")";
            SearchResult result = search.FindOne();
            if (result != null)
                return result.GetDirectoryEntry();
            else
                return null;
        }

        public static bool SetMobile(string name, string mobile)
        {
            try
            {
                var de = FindByName(name);
                de.Properties["mobile"].Value = mobile;
                de.CommitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

  

posted @ 2012-12-21 09:48  zyip  阅读(204)  评论(0编辑  收藏  举报