[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 ...
随机推荐
- android 点击返回键 以及 加载activity 生命周期 记录。。。,一目了然
简叙 Activity 生命周期及android 返回按钮捕捉 @Override protected void onPostCreate(Bundle savedInstanceState) { ...
- bzoj 4069~4071 APIO2015
T1 从高到底按位确定答案 A=1时f[i]表示前i个数合法的划分至少需要分出几段,时间复杂度$O(n^2log(ans))$ A>1时f[i][j]表示前i个数划分为j段是否可能合法,转移显然 ...
- Linux Expect 简介和使用实例
expect简介和使用实例 1 expect 简介 expect 是用来进行自动化控制和测试的工具. 主要是和交互式软件telnet ftp passwd fsck rlogin ssh tip 等进 ...
- 在 Laravel 5 中集成七牛云存储实现云存储功能(非上传)
本扩展包基于https://github.com/qiniu/php-sdk开发,是七牛云储存 Laravel 5 Storage版,通过本扩展包可以在Laravel 5中集成七牛云存储功能. 1.安 ...
- Hive组件以及执行过程
对Hive的基本组成进行了总结: 1.组件: 元存储(Metastore )-存储“系统目录以及关于表.列.分区等的元数据”的组件.驱动(Driver )- 控制 HiveQL 生命周期的组件,当 H ...
- Redux 处理异步 Action
redux-promise-utils What redux-promise-utils 是一个基于 redux-thunk 和 redux-actions 的工具,符合 FSA 规范,方便开发者处理 ...
- 一次使用 Redis 优化查询性能的实践
因为我的个人网站 restran.net 已经启用,博客园的内容已经不再更新.请访问我的个人网站获取这篇文章的最新内容,一次使用 Redis 优化查询性能的实践 应用背景 有一个应用需要上传一组ID到 ...
- Microsoft Office Professional Plus 2013全套
Microsoft Office Professional Plus 2013全套产品,全激活版本 包括Access Word Excel Powerpoint Publisher Skyd ...
- Spring Boot日志集成
Spring Boot日志框架 Spring Boot支持Java Util Logging,Log4j2,Lockback作为日志框架,如果你使用starters启动器,Spring Boot将使用 ...
- Remove “Quick Access” entry in Eclipse Juno
Here is a quick hack which doesn't require any plugin installation, instead you just need to add a f ...