C#寫的對系統使用者和組操作的類(可以實現使用者和組的添加、刪除、修改)

來源:互聯網
上載者:User

using System;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;

namespace SystemUserOperationDll
{
    public class SystemUserOperationClass
    {        
        private DirectoryEntry AD;
        

        /// <summary>
        /// 初始化方法
        /// </summary>
        /// <param name="strMachineName">機器名</param>
        /// <param name="strLoginName">登入使用者名稱(如:Administrator)</param>
        /// <param name="strLoginPassword">登入密碼</param>
        public SystemUserOperationClass(string strMachineName, string strLoginName, string strLoginPassword)
        {
            AD = new DirectoryEntry("WinNT://" + strMachineName + ",computer", ".\\" + strLoginName, strLoginPassword);
        }
        /// <summary>
        /// 添加系統(域)使用者
        /// </summary>
        /// <param name="strUserName">使用者名稱</param>
        /// <param name="strPassword">密碼</param>
        /// <param name="strDescription">描述</param>
        /// <returns></returns>
        public bool AddDomainUser(string strUserName, string strPassword, string strDescription)
        {
            try
            {
                DirectoryEntry NewUser;
                NewUser = AD.Children.Add(strUserName, "user"); //添加使用者
                NewUser.Invoke("SetPassword", new Object[] { strPassword });  //設定密碼
                NewUser.Properties["description"].Add(strDescription);  //添加描述
                NewUser.CommitChanges();

                AD.Close();

            }
            catch
            {
                throw;
            }
            return true;

        }
        /// <summary>
        /// 修改系統(域)使用者
        /// </summary>
        /// <param name="strUserName">使用者名稱</param>
        /// <param name="strPassword">密碼</param>
        /// <param name="strDescription">描述</param>
        /// <returns></returns>
        public bool ModifyDomainUser(string strUserName, string strPassword, string strDescription)
        {
            try
            {
                DirectoryEntry User = AD.Children.Find(strUserName);
                if (User.Name != null)
                {
                    //修改密碼
                    User.Invoke("SetPassword", new Object[] { strPassword });
                    //修改描述
                    User.Properties["description"].Value = strDescription;
                    User.CommitChanges();
                }
                AD.Close();
            }
            catch
            {
                throw;
            }
            return true;
        }
        /// <summary>
        /// 刪除系統(域)使用者
        /// </summary>
        /// <param name="strUserName">使用者名稱</param>
        /// <returns></returns>
        public bool DeleteDomainUser(string strUserName)
        {
            try
            {
                DirectoryEntry User = AD.Children.Find(strUserName, "user");//找到要刪除的使用者
                if (User.Name != null)
                {
                    AD.Children.Remove(User);
                }
                AD.Close();
            }
            catch
            {
                throw;
            }
            return true;
        }
        /// <summary>
        /// 添加系統(域)組
        /// </summary>
        /// <param name="strGroupName">組名</param>
        /// <param name="strDescription">描述</param>
        /// <returns></returns>
        public bool AddDomainGroup(string strGroupName, string strDescription)
        {
            try
            {
                DirectoryEntry Group;
                Group = AD.Children.Add(strGroupName, "group");
                Group.Properties["description"].Add(strDescription);
                Group.CommitChanges();
                AD.Close();
            }
            catch
            {
                throw;
            }
            return true;
        }
        /// <summary>
        /// 修改系統(域)組
        /// </summary>
        /// <param name="strGroupName">組名</param>
        /// <param name="strDescription">描述</param>
        /// <returns></returns>
        public bool ModifyDomainGroup(string strGroupName, string strDescription)
        {
            try
            {
                DirectoryEntry Group = AD.Children.Find(strGroupName);
                if (Group.Name != null)
                {
                    Group.Properties["description"].Value = strDescription;
                    Group.CommitChanges();
                }
                AD.Close();
            }
            catch
            {
                throw;
            }
            return true;
        }
        /// <summary>
        /// 刪除系統(域)組
        /// </summary>
        /// <param name="strGroupName">組名</param>
        /// <returns></returns>
        public bool DeleteDomainGroup(string strGroupName)
        {
            try
            {
                DirectoryEntry Group = AD.Children.Find(strGroupName, "group");
                if (Group.Name != null)
                {
                    AD.Children.Remove(Group);
                }
                AD.Close();
            }
            catch
            {
                throw;
            }
            return true;
        }
        /// <summary>
        /// 添加組使用者
        /// </summary>
        /// <param name="strGroupName">組名</param>
        /// <param name="strUserName">使用者名稱</param>
        /// <returns></returns>
        public bool AddGroupUser(string strGroupName,string strUserName)
        {
            try
            {
                DirectoryEntry Group = AD.Children.Find(strGroupName, "group");// 找到組
                DirectoryEntry User = AD.Children.Find(strUserName, "user");//找到使用者
                if (Group.Name != null && User.Name != null)
                {
                    Group.Invoke("Add", new Object[] { User.Path });
                }
                AD.Close();
            }
            catch
            {
                throw;
            }
            return true;
        }
        /// <summary>
        /// 移除組使用者
        /// </summary>
        /// <param name="strGroupName">組名</param>
        /// <param name="strUserName">使用者名稱</param>
        /// <returns></returns>
        public bool RemoveGroupUser(string strGroupName,string strUserName)
        {
            try
            {
                DirectoryEntry Group = AD.Children.Find(strGroupName, "group");// 找到組
               
                object members = Group.Invoke("Members", null);
                foreach (object member in (System.Collections.IEnumerable)members)
                {
                    //擷取該組的每個成員
                    DirectoryEntry x = new DirectoryEntry(member);

                    if (strUserName == x.Name) //要移除的使用者存在的話,則從該組中移除。
                    {
                        DirectoryEntry User = AD.Children.Find(strUserName, "user");//找到該使用者
                        Group.Invoke("Remove", new Object[] { User.Path });
                    }
                }
                AD.Close();               
            }
            catch
            {
                throw;
            }
            return true;
        }
              
    }

 

第一次做這方面的東西,有不足之處還請不吝指教。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.