zoukankan      html  css  js  c++  java
  • [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#使用Xamarin开发可移植移动应用(5.进阶篇显示弹出窗口与通讯中心)附源码
    C#使用Xamarin开发可移植移动应用(4.进阶篇MVVM双向绑定和命令绑定)附源码
    C#使用Xamarin开发可移植移动应用(3.Xamarin.Views控件)附源码
    C#使用Xamarin开发可移植移动应用(2.Xamarin.Forms布局,本篇很长,注意)附源码
    C#使用Xamarin开发可移植移动应用(1.入门与Xamarin.Forms页面),附源码
    ASP.NET Core之跨平台的实时性能监控(2.健康检查)
    Android Studio 快捷键:重载与重写、try catch代码块、导包 快捷键
    新版本jQuery对动态添加元素绑定点击事件实例
    ssm框架中,mybatis的sql语句日志输出
    maven环境下的ssm框架上传excel 案例
  • 原文地址:https://www.cnblogs.com/boneking/p/3572859.html
Copyright © 2011-2022 走看看