[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 ...
随机推荐
- 【ZZ】C++静态库与动态库 | 菜鸟教程
C++静态库与动态库 http://www.runoob.com/w3cnote/cpp-static-library-and-dynamic-library.html
- zookeeper 运维
尽管zookeeper在编程上有很多的阱陷,API也非常的难用,但zookeeper服务本身可以说是很牢靠的了,所以在网上貌似关于运维的文章比较少. 但省心并不代表不会出麻烦,下面总结下zookeep ...
- ROS使用国内的DDNS服务
未测试.转载余松老师的作品 虽然RouterOS 加入了cloud功能,但最近在配置RB2011的时候发现不好使,更新域名后无法正确解析到我的IP地址,虽然在cloud的public address中 ...
- servlet的登陆案例
Users.java package com.po; public class Users { private String username; private String password; pu ...
- Android TextView(EditView)文字底部或者中间 加横线
Android TextView(EditView)文字底部或者中间 加横线 tv = (TextView) this .findViewById(R.id. text_view ); 中间加横线 t ...
- Oracle RAC 集群环境下日志文件结构
Oracle RAC 集群环境下日志文件结构 在Oracle RAC环境中,对集群中的日志的定期检查是必不可少的.通过查看集群日志,可以早期定位集群环境中出现的问题,以便将问题消灭在萌芽状态.简单介绍 ...
- Python 中 logging 日志模块在多进程环境下的使用
因为我的个人网站 restran.net 已经启用,博客园的内容已经不再更新.请访问我的个人网站获取这篇文章的最新内容,Python 中 logging 日志模块在多进程环境下的使用 使用 Pytho ...
- Sublime Text 3 安装+注册+汉化
参考:https://www.cnblogs.com/h--d/p/7555119.html
- leetcode28
public class Solution { public int StrStr(string haystack, string needle) { return haystack.IndexOf( ...
- Variation Model的应用
Variation Model的主要原理是将待检测的图像与一张标准图像作比较,找出待检测图像与标准图像(ideal image)的明显差异(也就是不良). 标准图像可以采用几张OK品的图像训练(tra ...