亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

利用C#怎么編寫一個Windows組和用戶管理功能系統

發布時間:2021-01-08 16:18:40 來源:億速云 閱讀:154 作者:Leah 欄目:開發技術

本篇文章給大家分享的是有關利用C#怎么編寫一個Windows組和用戶管理功能系統,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

1、WindowsAccountHelper類實現

using System;
using System.Collections.Generic;
using System.DirectoryServices.AccountManagement;
using System.Linq;
 
public class WindowsAccountHelper
{
    public static string LastErrorMsg { get; private set; }
 
    public static List<string> GetGroups()
    {
        var groups = new List<string>();
        try
        {
            var context = new PrincipalContext(ContextType.Machine);
            var queryGroup = new GroupPrincipal(context);
            var searcher = new PrincipalSearcher(queryGroup);
            searcher.FindAll().ToList().ForEach(t => groups.Add(t.Name));
        }
        catch (Exception)
        {
            groups.Clear();
        }
 
        return groups;
    }
 
    public static List<string> GetGroupUsers(string groupName)
    {
        var group = GetGroup(groupName);
        return GetGroupUsers(group);
    }
 
    public static List<string> GetGroupUsers(GroupPrincipal group)
    {
        var users = new List<string>();
         
        if (group == null)
        {
            return users;
        }
 
        group.GetMembers().ToList().ForEach(t => users.Add(t.Name));
        return users;
    }
 
    public static GroupPrincipal GetGroup(string groupName)
    {
        GroupPrincipal group = null;
        try
        {
            var context = new PrincipalContext(ContextType.Machine);
            var queryGroup = new GroupPrincipal(context);
            var searcher = new PrincipalSearcher(queryGroup);
            foreach (var principal in searcher.FindAll())
            {
                var groupPrincipal = (GroupPrincipal)principal;
                if (groupPrincipal != null && groupPrincipal.Name.Equals(groupName))
                {
                    group = groupPrincipal;
                    break;
                }
            }
        }
        catch (Exception)
        {
            // ignored
        }
 
        return group;
    }
 
    public static GroupPrincipal CreateGroup(string groupName, string description, bool isSecurityGroup)
    {
        GroupPrincipal group;
        try
        {
            group = GetGroup(groupName);
            if (group == null)
            {
                var context = new PrincipalContext(ContextType.Machine);
                group = new GroupPrincipal(context)
                {
                    Name = groupName,
                    Description = description,
                    IsSecurityGroup = isSecurityGroup,
                    GroupScope = GroupScope.Local
                };
                group.Save();
            }
        }
        catch (Exception e)
        {
            LastErrorMsg = e.Message;
            group = null;
        }
 
        return group;
    }
 
    public static bool DeleteGroup(string groupName)
    {
        var group = GetGroup(groupName);
        if (group == null)
        {
            return true;
        }
 
        var ret = true;
        try
        {
            group.Delete();
        }
        catch (Exception)
        {
            ret = false;
        }
 
        return ret;
    }
 
    public static bool CreateWindowsAccount(string userName, string password,
        string displayName, string description, bool cannotChangePassword,
        bool passwordNeverExpires, string groupName)
    {
        bool ret;
        try
        {
            var context = new PrincipalContext(ContextType.Machine);
            var group = GroupPrincipal.FindByIdentity(context, groupName);
            if (group == null)
            {
                return false;
            }
 
            ret = CreateWindowsAccount(userName, password, displayName,
                description, cannotChangePassword, passwordNeverExpires, group);
        }
        catch (Exception)
        {
            ret = false;
        }
 
        return ret;
    }
 
    public static bool CreateWindowsAccount(string userName, string password,
        string displayName, string description, bool cannotChangePassword,
        bool passwordNeverExpires, GroupPrincipal group)
    {
        bool ret;
        try
        {
            if (group == null)
            {
                return false;
            }
 
            var context = new PrincipalContext(ContextType.Machine);
            var user = UserPrincipal.FindByIdentity(context, userName)
                       ?? new UserPrincipal(context);
            user.SetPassword(password);
            user.DisplayName = displayName;
            user.Name = userName;
            user.Description = description;
            user.UserCannotChangePassword = cannotChangePassword;
            user.PasswordNeverExpires = passwordNeverExpires;
            user.Save();
 
            group.Members.Add(user);
            group.Save();
            ret = true;
        }
        catch (Exception)
        {
            ret = false;
        }
 
        return ret;
    }
 
    public static bool DeleteWindowsAccount(List<string> userNameList)
    {
        var ret = true;
        try
        {
            foreach (var userName in userNameList)
            {
                var context = new PrincipalContext(ContextType.Machine);
                var user = UserPrincipal.FindByIdentity(context, userName);
                user?.Delete();
            }
        }
        catch (Exception)
        {
            ret = false;
        }
 
        return ret;
    }
 
    public static bool ChangeUserGroup(string userName, string groupName)
    {
        bool ret;
        try
        {
            var context = new PrincipalContext(ContextType.Machine);
            var group = GroupPrincipal.FindByIdentity(context, groupName);
            if (group == null)
            {
                return false;
            }
 
            ret = ChangeUserGroup(userName, group);
        }
        catch (Exception)
        {
            ret = false;
        }
 
        return ret;
    }
 
    public static bool ChangeUserGroup(string userName, GroupPrincipal group)
    {
        bool ret;
        try
        {
            if (group == null)
            {
                return false;
            }
 
            var context = new PrincipalContext(ContextType.Machine);
            var user = UserPrincipal.FindByIdentity(context, userName);
            if (user == null)
            {
                return false;
            }
 
            if (!group.Members.Contains(user))
            {
                group.Members.Add(user);
                group.Save();
            }
 
            ret = true;
        }
        catch (Exception)
        {
            ret = false;
        }
 
        return ret;
    }
 
    public static int UpdateGroupUsers(string groupName, List<string> userNames, string password = "")
    {
        var group = CreateGroup(groupName, string.Empty, false);
        if (group == null)
        {
            return 0;
        }
 
        var userNameList = new List<string>();
        userNameList.AddRange(userNames);
 
        var addedUsers = new List<string>();
        int groupUserCount;
 
        try
        {
            foreach (var principal in group.GetMembers())
            {
                var user = (UserPrincipal)principal;
                if (user == null)
                {
                    continue;
                }
 
                if (userNameList.Contains(user.Name))
                {
                    //已有用戶
                    addedUsers.Add(user.Name);
                }
                else
                {
                    user.Delete();
                }
            }
 
            //已有用戶數
            groupUserCount = addedUsers.Count;
 
            //剩余的即為需要添加的用戶集合
            foreach (var userName in addedUsers)
            {
                userNameList.Remove(userName);
            }
 
            //創建用戶
            foreach (var userName in userNameList)
            {
                if (CreateWindowsAccount(userName, password,
                    userName, string.Empty,
                    false, false, group))
                {
                    groupUserCount++;
                }
            }
        }
        catch (UnauthorizedAccessException)
        {
            groupUserCount = 0;
        }
 
        return groupUserCount;
    }
}

2、使用示例

private bool CreateGroupUsers(string groupName, List<string> windowsUserList,
    string password, int userCount)
{
    var group = WindowsAccountHelper.CreateGroup(groupName, string.Empty, true);
    if (group == null)
    {
        return false;
    }
 
    var userNames = WindowsAccountHelper.GetGroupUsers(group);
    foreach (var userName in WindowsUserList)
    {
        if (!userNames.Contains(userName))
        {
            if (!WindowsAccountHelper.CreateWindowsAccount(userName, password,
                userName, string.Empty,
                false, false, group))
            {
                return false;
            }
        }
    }
 
    return true;
}

以上就是利用C#怎么編寫一個Windows組和用戶管理功能系統,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

喀喇沁旗| 宿迁市| 宣武区| 桑日县| 当阳市| 荔浦县| 平舆县| 虞城县| 虎林市| 皮山县| 元阳县| 东安县| 盘锦市| 永兴县| 任丘市| 长葛市| 斗六市| 太和县| 措美县| 无极县| 洪湖市| 永城市| 新乡市| 甘泉县| 贵德县| 崇礼县| 呼伦贝尔市| 波密县| 福贡县| 赤水市| 息烽县| 石台县| 卢湾区| 北辰区| 定日县| 辉县市| 蛟河市| 庆安县| 当雄县| 浦城县| 大埔区|