[C#]创建Windows用户及组
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用户及组的更多相关文章
- Linux(CentOS6.5)下创建新用户和组,并制定用户和组ID
		
相关命令: groupadd -g 888 comexgroup useradd comex -d /comexHome -g comexgroup -u 888 cp /etc/skel/.* /c ...
 - 管理员技术(四): 配置NTP网络时间客户端、 创建一个备份包、 配置用户和组账号、配置一个cron任务
		
一. 配置NTP网络时间客户端 目标: 本例要求配置虚拟机 server0,能够自动校对系统时间.相关信息如下: 1> NTP服务器位于 classroom.example.com ...
 - 使用NetUserAdd API函数创建远程用户
		
http://apps.hi.baidu.com/share/detail/33407620 使用NetUserAdd编程创建远程用户Windows API NetUserAdd()可以创建Windo ...
 - TFS 用户与组管理(转)
		
作者:frank.liu kaka.zhou 安装 Team Foundation Server 后,会创建以下全局组.可以使用这些全局组来控制 Team Foundation 用户的权限. 组 权限 ...
 - TFS创建登录用户并连接TFS
		
简单使用 1.创建TFS组(Windows的组) 2.在“TFS管理控制台”中设置“组成员资格” 3.给“Team Foundation Administrators”组设置“属性” 4.给“Team ...
 - C# 操作本地用户和组(基本全功能)
		
今天学习了下怎么用.Net操作本地用户和组,因为目前网上还没看到一篇比较完整的文章,所以整理了下也分享出来,最后附带参考文档,方便深究的童鞋继续学习.========== 原创作品 作者:Yo ...
 - 009-centos6.6 用户以及组操作
		
1.查看用户所属组 id admin uid=500(admin) gid=500(admin) 组=500(admin) 2.查看用户信息 用户列表文件:cat /etc/passwd 查看系统中有 ...
 - 管理用户和组 、 tar备份与恢复 、 cron计划任务-云计算学习(4)
		
配置用户和组账号 问题 本例要求创建下列用户.组以及组的成员关系: 新建用户 alex,其用户ID为3456,密码是flectrag 创建一个名为 adminuser 的组 创建一个名为 natash ...
 - SharePoint 2013 SqlException (0x80131904):找不到Windows NT 用户或组xxxx\administrator
		
过程描述: 在SharePoint 2013里配置创建搜索服务应用程序时报错: 配置 Search Service 应用程序期间遇到错误. System.Data.SqlClient.SqlExcep ...
 
随机推荐
- JVM参数之-XX:+HeapDumpOnOutOfMemoryError(导出内存溢出的堆信息(hprof文件))
			
当出现HeapDumpOnOutOfMemoryError错误时,我们需要分析原因,下面的程序就是模拟这个错误并导出dump文件,好让你分析 GcTest.java package gc; publi ...
 - PHP CRC16 校验码的算法怎么使用
			
PHP CRC16 校验码的算法如何使用最近用到CRC16, 我现在就是要把 010301180001 算出CRC16的校验码,通过其他工具,可以得到 校验码是 05F1 最后完整的代码就是 0103 ...
 - html链接路径
			
html链接的相对路径与绝对路径 绝对路径 完整的一个路径就是绝对路径,即包含schema://host[:port#]/path/.../[?query-string][#anchor] 例:htt ...
 - Django学习---原生ajax
			
Ajax 原生ajax Ajax主要就是使用 [XmlHttpRequest]对象来完成请求的操作,该对象在主流浏览器中均存在(除早起的IE),Ajax首次出现IE5.5中存在(ActiveX控件). ...
 - 【理论】python使用celery异步处理请求
			
Flask中使用celery队列处理执行时间较长的请求. 一. 安装celery pip install celery flask redis 二. celery简介 Celery是个异步分布式任务队 ...
 - Spring IO Platform介绍
			
为什么要用Spring IO Platform 今天无意间看到了一个关键词:"Spring IO Platform",第一直觉是不是有关于IO方面的框架或者包呢,查了一下,居然是为 ...
 - s2选择框的全选和反选jQuery
			
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
 - 听说去了BAT的 Linuxers 都做过这套面试题!
			
本部分为单项选择题,每道题仅有一个答案,每题2分. 1. cron 后台常驻程序 (daemon) 用于: A. 负责文件在网络中的共享 B. 管理打印子系统 C. 跟踪管理系统信息和错误 D. ...
 - 用logger在控制台打印信息
			
第一步: 导入jar包,maven项目可以直接添加 <dependency> <groupId>log4j</groupId> <artifactId> ...
 - 用 AutoHotKey 随时记录所想
			
别被标题咋呼了,其实很简单,按下快捷键自动打开指定文本文档,自动加上当前时间日期,适合像我这种无聊的人记录生活. ;Alt+X 调出 !X:: ;获取当前日期时间并保存到剪贴板 d = @rhinoc ...