[C#]创建Windows用户及组
2014-02-28 09:24
232 查看
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Configuration; using System.DirectoryServices.AccountManagement; using System.Collections; namespace UserTrans { public static class Commons { static Configuration config; static AppSettingsSection appSetting; public static ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); static bool CreateLocalWindowsAccount(string userName, string passWord, string displayName, string description, string groupName, bool canChangePwd, bool pwdExpires) { bool retIsSuccess = false; try { PrincipalContext context = new PrincipalContext(ContextType.Machine); UserPrincipal user = new UserPrincipal(context); user.SetPassword(passWord); user.DisplayName = displayName; user.Name = userName; user.Description = description; user.UserCannotChangePassword = canChangePwd; user.PasswordNeverExpires = pwdExpires; user.Save(); GroupPrincipal group = GroupPrincipal.FindByIdentity(context, groupName); group.Members.Add(user); group.Save(); retIsSuccess = true; } catch (Exception ex) { retIsSuccess = false; } return retIsSuccess; } static GroupPrincipal CreateGroup(string groupName, Boolean isSecurityGroup) { GroupPrincipal retGroup = null; try { retGroup = IsGroupExist(groupName); if (retGroup == null) { PrincipalContext ctx = new PrincipalContext(ContextType.Machine); GroupPrincipal insGroupPrincipal = new GroupPrincipal(ctx); insGroupPrincipal.Name = groupName; insGroupPrincipal.IsSecurityGroup = isSecurityGroup; insGroupPrincipal.GroupScope = GroupScope.Local; insGroupPrincipal.Save(); retGroup = insGroupPrincipal; } } catch (Exception ex) { } return retGroup; } static GroupPrincipal IsGroupExist(string groupName) { GroupPrincipal retGroup = null; try { PrincipalContext ctx = new PrincipalContext(ContextType.Machine); GroupPrincipal qbeGroup = new GroupPrincipal(ctx); PrincipalSearcher srch = new PrincipalSearcher(qbeGroup); foreach (GroupPrincipal ingrp in srch.FindAll()) { if (ingrp != null && ingrp.Name.Equals(groupName)) { retGroup = ingrp; break; } } } catch (Exception ex) { } return retGroup; }
public static int UpdateGroupUsers(string groupName, List<string> usersName) { List<string> addedUsers = new List<string>(); int retAddCount = 0; GroupPrincipal qbeGroup = CreateGroup(groupName, false); foreach (UserPrincipal user in qbeGroup.GetMembers()) { if (usersName.Contains(user.Name)) { addedUsers.Add(user.Name); retAddCount++; } else { user.Delete(); } } foreach (string addedUserName in addedUsers) { usersName.Remove(addedUserName); } foreach (string addUserName in usersName) { bool isSuccess = CreateLocalWindowsAccount(addUserName, "password", addUserName, "", groupName, false, false); if (isSuccess) retAddCount++; }return retAddCount; }
} }
参考
http://msdn.microsoft.com/zh-cn/library/bb924542(v=vs.90).aspx
相关文章推荐
- [C#]创建Windows用户及组
- 用C#在WINDOWS中实现新用户帐号的创建
- 使用c#创建windows本地用户帐号
- NET C#创建WINDOWS系统用户
- 使用c#创建windows本地用户帐号
- C#创建windows系统用户的方法
- 用C#创建Windows服务(Windows Services)
- Visual Studio 创建C++或C#Windows程序
- C# 创建Windows 服务简单实例
- [译]使用C#的用户控件创建ActiveX
- C#创建windows服务:提示未能加载程序集,指定的路径找不到
- 用C#创建Windows服务(Windows Services)
- 用C#创建Windows Service
- C#动态创建数据库并只允许特定用户登录该数据库
- 用C#创建Windows服务(Windows Services)
- C# LDAP 管理(创建新用户)
- C#创建Windows Service(Windows 服务)基础教程
- 用VS(c#)创建、调试windows service以及部署卸载
- 设置c#windows服务描述及允许服务与桌面交互的几种方法(作者博客还有一大堆C#创建服务的文章)
- C#创建Windows Service(Windows 服务)基础教程