欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c# 对windows用户和组操作实例

程序员文章站 2023-12-17 09:20:58
复制代码 代码如下:using system;using system.collections.generic;using system.linq;using system...

复制代码 代码如下:

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.directoryservices;
namespace jh.framework.security
{
    ///

    /// 计算机用户和组操作类
    ///

    public class userandgrouphelper
    {
        private static readonly string path = "winnt://" + environment.machinename;
        ///

        /// 添加windows用户
        ///

        /// 用户名
        /// 密码
        /// 所属组
        /// 描述
        public static void adduser(string username, string password, string group, string description)
        {
            using (directoryentry dir = new directoryentry(path))
            {
                using (directoryentry user = dir.children.add(username, "user")) //增加用户名
                {
                    user.properties["fullname"].add(username); //用户全称
                    user.invoke("setpassword", password); //用户密码
                    user.invoke("put", "description", description);//用户详细描述
                    //user.invoke("put","passwordexpired",1); //用户下次登录需更改密码
                    user.invoke("put", "userflags", 66049); //密码永不过期
                    //user.invoke("put", "userflags", 0x0040);//用户不能更改密码s
                    user.commitchanges();//保存用户
                    using (directoryentry grp = dir.children.find(group, "group"))
                    {
                        if (grp.name != "")
                        {
                            grp.invoke("add", user.path.tostring());//将用户添加到某组
                        }
                    }
                }
            }
        }
        ///

        /// 更改windows用户密码
        ///

        /// 用户名
        /// 新密码
        public static void updateuserpassword(string username, string newpassword)
        {
            using (directoryentry dir = new directoryentry(path))
            {
                using (directoryentry user = dir.children.find(username, "user"))
                {
                    user.invoke("setpassword", new object[] { newpassword });
                    user.commitchanges();
                }
            }
        }
        ///

        /// 删除windows用户
        ///

        /// 用户名
        public static void removeuser(string username)
        {
            using (directoryentry dir = new directoryentry(path))
            {
                using (directoryentry user = dir.children.find(username, "user"))
                {
                    dir.children.remove(user);
                }
            }
        }
        ///

        /// 添加windows用户组
        ///

        /// 组名称
        /// 描述
        public static void addgroup(string groupname, string description)
        {
            using (directoryentry dir = new directoryentry(path))
            {
                using (directoryentry group = dir.children.add(groupname, "group"))
                {
                    group.invoke("put", new object[] { "description", description });
                    group.commitchanges();
                }
            }
        }
        ///

        /// 删除windows用户组
        ///

        /// 组名称
        public static void removegroup(string groupname)
        {
            using (directoryentry dir = new directoryentry(path))
            {
                using (directoryentry group = dir.children.find(groupname, "group"))
                {
                    dir.children.remove(group);
                }
            }
        }
    }
}

上一篇:

下一篇: