博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[C#]创建Windows用户及组
阅读量:6630 次
发布时间:2019-06-25

本文共 4209 字,大约阅读时间需要 14 分钟。

原文:

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
usersName) {
List
addedUsers = new List
(); 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

转载地址:http://eazvo.baihongyu.com/

你可能感兴趣的文章
软件开发活动
查看>>
数制与编码
查看>>
Python学习搬家啦
查看>>
FileInputStream
查看>>
mybatis springmvc批量删除 2最新
查看>>
java接口
查看>>
为什么程序员不擅长估算时间?
查看>>
设置dom节点属性的代码优化
查看>>
wait与sleep的区别
查看>>
Rotator的单位
查看>>
[Hyper-V]给Hyper-V创建两块网卡备用
查看>>
WebApi系列~安全校验中的防篡改和防复用
查看>>
pymysql 数据库编程
查看>>
<2048>调查报告心得与体会
查看>>
BM串匹配算法
查看>>
10款常见MySQL高可用方案选型解读
查看>>
最小生成树——Prim算法
查看>>
数据结构8——割点
查看>>
数据结构2——动态树
查看>>
如何关闭ReSharper中的[ Use 'var' ]提示
查看>>