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用户及组的更多相关文章

  1. Linux(CentOS6.5)下创建新用户和组,并制定用户和组ID

    相关命令: groupadd -g 888 comexgroup useradd comex -d /comexHome -g comexgroup -u 888 cp /etc/skel/.* /c ...

  2. 管理员技术(四): 配置NTP网络时间客户端、 创建一个备份包、 配置用户和组账号、配置一个cron任务

    一. 配置NTP网络时间客户端 目标: 本例要求配置虚拟机 server0,能够自动校对系统时间.相关信息如下: 1> NTP服务器位于 classroom.example.com        ...

  3. 使用NetUserAdd API函数创建远程用户

    http://apps.hi.baidu.com/share/detail/33407620 使用NetUserAdd编程创建远程用户Windows API NetUserAdd()可以创建Windo ...

  4. TFS 用户与组管理(转)

    作者:frank.liu kaka.zhou 安装 Team Foundation Server 后,会创建以下全局组.可以使用这些全局组来控制 Team Foundation 用户的权限. 组 权限 ...

  5. TFS创建登录用户并连接TFS

    简单使用 1.创建TFS组(Windows的组) 2.在“TFS管理控制台”中设置“组成员资格” 3.给“Team Foundation Administrators”组设置“属性” 4.给“Team ...

  6. C# 操作本地用户和组(基本全功能)

    今天学习了下怎么用.Net操作本地用户和组,因为目前网上还没看到一篇比较完整的文章,所以整理了下也分享出来,最后附带参考文档,方便深究的童鞋继续学习.==========  原创作品    作者:Yo ...

  7. 009-centos6.6 用户以及组操作

    1.查看用户所属组 id admin uid=500(admin) gid=500(admin) 组=500(admin) 2.查看用户信息 用户列表文件:cat /etc/passwd 查看系统中有 ...

  8. 管理用户和组 、 tar备份与恢复 、 cron计划任务-云计算学习(4)

    配置用户和组账号 问题 本例要求创建下列用户.组以及组的成员关系: 新建用户 alex,其用户ID为3456,密码是flectrag 创建一个名为 adminuser 的组 创建一个名为 natash ...

  9. SharePoint 2013 SqlException (0x80131904):找不到Windows NT 用户或组xxxx\administrator

    过程描述: 在SharePoint 2013里配置创建搜索服务应用程序时报错: 配置 Search Service 应用程序期间遇到错误. System.Data.SqlClient.SqlExcep ...

随机推荐

  1. java web 程序---javabean实例--登陆界面并显示用户名和密码

    重点:注意大小写,不注意细节,这点小事,还需要请教 发现一个问题,也是老师当时写的时候,发现代码没错,但是就是运行问题. 大家看,那个java类,我们要求是所有属性均为私有变量,但是方法为公有的,如果 ...

  2. python使用scikit-learn计算TF-IDF

    1 Scikit-learn下载安装 1.1 简介 1.2 安装软件 2 TF-IDF基础知识 2.1 TF-IDF概念 2.2 举例说明计算 3 Scikit-Learn中计算TF-IDF 3.1 ...

  3. 安装特定版本 sysbench 以及安装时指定特定的mysql客户端

    step1: 下载安装源码 https://github.com/akopytov/sysbench (git clone https://github.com/akopytov/sysbench.g ...

  4. LeetCode 177. Nth Highest Salary

    https://leetcode.com/problems/nth-highest-salary/description/ Write a SQL query to get the nth highe ...

  5. mysql-1安装和数据库的管理

    1.安装 直接docker安装,客户端使用Navicat Premium. docker run -d --name csjmysql -p 3306:3306 -e MYSQL_ROOT_PASSW ...

  6. Repeater更具条件为每行数据背景填充颜色

    后台代码 protected void RptPosterManager_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.I ...

  7. OpenCL 事件的使用,以及回调函数

    ▶ 事件的两种使用方法.第一种是用事件 a 标记进入命令队列的操作 A,于是后续进入命令队列的操作 B 可以被要求等到前面事件 a 完成(即操作 A 完成)以后才能开始调度执行.第二种是使用用户自定义 ...

  8. C#中StreamWriter与BinaryWriter的区别兼谈编码。

    原文:http://www.cnblogs.com/ybwang/archive/2010/06/12/1757409.html 参考: 1. <C#高级编程>第六版 2.  文件流和数据 ...

  9. sass compact方法的实现

    不想依赖compass的编译器,但sass的又没有compact方法,于是自己造轮子.最早时在stackoverflow看到一个 @function compact($var-1, $var-2: f ...

  10. jquey下eq()的使用注意事项

    写在开始的话: 今天在公司路经同事工位,发现在写jquery代码,刚好遇见一个bug,于是驻足看了一会,发现了jq遍历方法中eq()的使用的一个容易犯错的地方. 同事的代码大概意思是这样的: < ...