//启用账户
        public static bool EnableAdUser(string account)
        {
            try
            {
                DirectoryEntry NewUser = GetDirectoryEntryByAccount(account);
                if (NewUser != null)
                {
                    try
                    {
                        ActiveDs.IADsUser user = (ActiveDs.IADsUser)NewUser.NativeObject;

                        user.AccountDisabled = false;

                        user.SetInfo();

                        NewUser.CommitChanges();
                        return(true);
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
            catch
            {
            }
            return(false);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 创建域用户,"administrator","Ccc2008neu","administrator","Ccc2008neu"
        /// </summary>
        /// <param name="loginName"></param>
        /// <param name="displayName"></param>
        /// <param name="description"></param>
        /// <param name="pwd"></param>
        public static bool AddUser(string loginName, string displayName, string email, string phone, string pwd, string topPath, string groupName, string schoolName, bool enabled)
        {
            string ouPath = AddOU(topPath, schoolName);
            bool   result;
            string content = "";
            //先加安全组,帐号重复会出错;否则会出现错误
            DirectoryEntry grp = AddGroup(new DirectoryEntry(topPath), groupName);

            using (DirectoryEntry AD = new DirectoryEntry(ouPath))
            {
                try
                {
                    using (DirectoryEntry NewUser = AD.Children.Add("CN=" + loginName, "user"))
                    {
                        NewUser.Properties["displayName"].Add(displayName);
                        NewUser.Properties["name"].Add(displayName);
                        NewUser.Properties["sAMAccountName"].Add(loginName);
                        NewUser.Properties["userPrincipalName"].Add(loginName + DomainName);
                        if (phone != "")
                        {
                            NewUser.Properties["telephoneNumber"].Add(phone);
                        }
                        if (email != "")
                        {
                            NewUser.Properties["mail"].Add(email);
                        }
                        NewUser.CommitChanges();
                        try
                        {
                            ActiveDs.IADsUser user = (ActiveDs.IADsUser)NewUser.NativeObject;
                            user.AccountDisabled = !enabled;
                            user.SetPassword(pwd);
                            //密码永不过期
                            dynamic flag = user.Get("userAccountControl");

                            int newFlag = 0X10000;
                            user.Put("userAccountControl", newFlag);
                            user.SetInfo();

                            NewUser.CommitChanges();
                        }
                        catch (Exception ex)
                        {
                            content += ex.ToString() + "\r\f";
                        }
                        if (groupName != "")
                        {
                            AddUserToGroup(grp, NewUser);
                        }
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    content += ex.ToString();
                    result   = false;
                }
            }
            return(result);
        }
Exemplo n.º 3
0
        //删除用户
        public static bool DeleteAdUser(string account)
        {
            try
            {
                //DirectoryEntry lgUser = GetDirectoryEntryByAccount(account);
                //if (lgUser != null)
                //{
                //    lgUser.DeleteTree();
                //    return true;
                //}
                DirectoryEntry NewUser = GetDirectoryEntryByAccount(account);
                if (NewUser != null)
                {
                    try
                    {
                        ActiveDs.IADsUser user = (ActiveDs.IADsUser)NewUser.NativeObject;

                        user.AccountDisabled = true;

                        user.SetInfo();

                        NewUser.CommitChanges();
                        return(true);
                    }
                    catch (Exception ex)
                    {
                    }
                    //lgUser.DeleteTree();
                }
            }
            catch
            {
            }
            return(false);
        }
Exemplo n.º 4
0
 /// <summary>
 /// 修改AD中的用户信息,启动帐号(帐号禁用帐号)
 /// </summary>
 /// <param name="loginName"></param>
 /// <returns></returns>
 public static bool EnabledUser(string loginName, bool userEnabled)
 {
     using (DirectoryEntry NewUser = ADHelper.GetDirectoryEntryByAccount(loginName))
     {
         try
         {
             ActiveDs.IADsUser user = (ActiveDs.IADsUser)NewUser.NativeObject;
             user.AccountDisabled = !userEnabled;
             user.SetInfo();
             NewUser.CommitChanges();
             return(true);
         }
         catch
         {
             return(false);
         }
     }
 }