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

使用C#实现Windows组和用户管理的示例代码

程序员文章站 2022-03-27 08:38:46
1、windowsaccounthelper类实现using system;using system.collections.generic;using system.directoryservice...

1、windowsaccounthelper类实现

using system;
using system.collections.generic;
using system.directoryservices.accountmanagement;
using system.linq;
 
public class windowsaccounthelper
{
    public static string lasterrormsg { get; private set; }
 
    public static list<string> getgroups()
    {
        var groups = new list<string>();
        try
        {
            var context = new principalcontext(contexttype.machine);
            var querygroup = new groupprincipal(context);
            var searcher = new principalsearcher(querygroup);
            searcher.findall().tolist().foreach(t => groups.add(t.name));
        }
        catch (exception)
        {
            groups.clear();
        }
 
        return groups;
    }
 
    public static list<string> getgroupusers(string groupname)
    {
        var group = getgroup(groupname);
        return getgroupusers(group);
    }
 
    public static list<string> getgroupusers(groupprincipal group)
    {
        var users = new list<string>();
         
        if (group == null)
        {
            return users;
        }
 
        group.getmembers().tolist().foreach(t => users.add(t.name));
        return users;
    }
 
    public static groupprincipal getgroup(string groupname)
    {
        groupprincipal group = null;
        try
        {
            var context = new principalcontext(contexttype.machine);
            var querygroup = new groupprincipal(context);
            var searcher = new principalsearcher(querygroup);
            foreach (var principal in searcher.findall())
            {
                var groupprincipal = (groupprincipal)principal;
                if (groupprincipal != null && groupprincipal.name.equals(groupname))
                {
                    group = groupprincipal;
                    break;
                }
            }
        }
        catch (exception)
        {
            // ignored
        }
 
        return group;
    }
 
    public static groupprincipal creategroup(string groupname, string description, bool issecuritygroup)
    {
        groupprincipal group;
        try
        {
            group = getgroup(groupname);
            if (group == null)
            {
                var context = new principalcontext(contexttype.machine);
                group = new groupprincipal(context)
                {
                    name = groupname,
                    description = description,
                    issecuritygroup = issecuritygroup,
                    groupscope = groupscope.local
                };
                group.save();
            }
        }
        catch (exception e)
        {
            lasterrormsg = e.message;
            group = null;
        }
 
        return group;
    }
 
    public static bool deletegroup(string groupname)
    {
        var group = getgroup(groupname);
        if (group == null)
        {
            return true;
        }
 
        var ret = true;
        try
        {
            group.delete();
        }
        catch (exception)
        {
            ret = false;
        }
 
        return ret;
    }
 
    public static bool createwindowsaccount(string username, string password,
        string displayname, string description, bool cannotchangepassword,
        bool passwordneverexpires, string groupname)
    {
        bool ret;
        try
        {
            var context = new principalcontext(contexttype.machine);
            var group = groupprincipal.findbyidentity(context, groupname);
            if (group == null)
            {
                return false;
            }
 
            ret = createwindowsaccount(username, password, displayname,
                description, cannotchangepassword, passwordneverexpires, group);
        }
        catch (exception)
        {
            ret = false;
        }
 
        return ret;
    }
 
    public static bool createwindowsaccount(string username, string password,
        string displayname, string description, bool cannotchangepassword,
        bool passwordneverexpires, groupprincipal group)
    {
        bool ret;
        try
        {
            if (group == null)
            {
                return false;
            }
 
            var context = new principalcontext(contexttype.machine);
            var user = userprincipal.findbyidentity(context, username)
                       ?? new userprincipal(context);
            user.setpassword(password);
            user.displayname = displayname;
            user.name = username;
            user.description = description;
            user.usercannotchangepassword = cannotchangepassword;
            user.passwordneverexpires = passwordneverexpires;
            user.save();
 
            group.members.add(user);
            group.save();
            ret = true;
        }
        catch (exception)
        {
            ret = false;
        }
 
        return ret;
    }
 
    public static bool deletewindowsaccount(list<string> usernamelist)
    {
        var ret = true;
        try
        {
            foreach (var username in usernamelist)
            {
                var context = new principalcontext(contexttype.machine);
                var user = userprincipal.findbyidentity(context, username);
                user?.delete();
            }
        }
        catch (exception)
        {
            ret = false;
        }
 
        return ret;
    }
 
    public static bool changeusergroup(string username, string groupname)
    {
        bool ret;
        try
        {
            var context = new principalcontext(contexttype.machine);
            var group = groupprincipal.findbyidentity(context, groupname);
            if (group == null)
            {
                return false;
            }
 
            ret = changeusergroup(username, group);
        }
        catch (exception)
        {
            ret = false;
        }
 
        return ret;
    }
 
    public static bool changeusergroup(string username, groupprincipal group)
    {
        bool ret;
        try
        {
            if (group == null)
            {
                return false;
            }
 
            var context = new principalcontext(contexttype.machine);
            var user = userprincipal.findbyidentity(context, username);
            if (user == null)
            {
                return false;
            }
 
            if (!group.members.contains(user))
            {
                group.members.add(user);
                group.save();
            }
 
            ret = true;
        }
        catch (exception)
        {
            ret = false;
        }
 
        return ret;
    }
 
    public static int updategroupusers(string groupname, list<string> usernames, string password = "")
    {
        var group = creategroup(groupname, string.empty, false);
        if (group == null)
        {
            return 0;
        }
 
        var usernamelist = new list<string>();
        usernamelist.addrange(usernames);
 
        var addedusers = new list<string>();
        int groupusercount;
 
        try
        {
            foreach (var principal in group.getmembers())
            {
                var user = (userprincipal)principal;
                if (user == null)
                {
                    continue;
                }
 
                if (usernamelist.contains(user.name))
                {
                    //已有用户
                    addedusers.add(user.name);
                }
                else
                {
                    user.delete();
                }
            }
 
            //已有用户数
            groupusercount = addedusers.count;
 
            //剩余的即为需要添加的用户集合
            foreach (var username in addedusers)
            {
                usernamelist.remove(username);
            }
 
            //创建用户
            foreach (var username in usernamelist)
            {
                if (createwindowsaccount(username, password,
                    username, string.empty,
                    false, false, group))
                {
                    groupusercount++;
                }
            }
        }
        catch (unauthorizedaccessexception)
        {
            groupusercount = 0;
        }
 
        return groupusercount;
    }
}

2、使用示例

private bool creategroupusers(string groupname, list<string> windowsuserlist,
    string password, int usercount)
{
    var group = windowsaccounthelper.creategroup(groupname, string.empty, true);
    if (group == null)
    {
        return false;
    }
 
    var usernames = windowsaccounthelper.getgroupusers(group);
    foreach (var username in windowsuserlist)
    {
        if (!usernames.contains(username))
        {
            if (!windowsaccounthelper.createwindowsaccount(username, password,
                username, string.empty,
                false, false, group))
            {
                return false;
            }
        }
    }
 
    return true;
}

以上就是使用c#实现windows组和用户管理的示例代码的详细内容,更多关于c#实现windows组和用户管理的资料请关注其它相关文章!