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

编程入门 行业动态 更新时间:2024-10-24 21:24:45
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组和用户管理的资料请关注其它相关文章!

  • 0
  • 0
  • 0
  • 0
  • 0

更多推荐

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

本文发布于:2023-06-10 22:55:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/620840.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:示例   代码   用户   Windows

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!