欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用C#實(shí)現(xiàn)Windows組和用戶管理的示例代碼

 更新時(shí)間:2021年01月07日 11:14:19   作者:xhubobo  
這篇文章主要介紹了使用C#實(shí)現(xiàn)Windows組和用戶管理的示例代碼,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下

1、WindowsAccountHelper類實(shí)現(xiàn)

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();
                }
            }
 
            //已有用戶數(shù)
            groupUserCount = addedUsers.Count;
 
            //剩余的即為需要添加的用戶集合
            foreach (var userName in addedUsers)
            {
                userNameList.Remove(userName);
            }
 
            //創(chuàng)建用戶
            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#實(shí)現(xiàn)Windows組和用戶管理的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于C#實(shí)現(xiàn)Windows組和用戶管理的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • DevExpress實(shí)現(xiàn)GridView當(dāng)無數(shù)據(jù)行時(shí)提示消息

    DevExpress實(shí)現(xiàn)GridView當(dāng)無數(shù)據(jù)行時(shí)提示消息

    這篇文章主要介紹了DevExpress實(shí)現(xiàn)GridView當(dāng)無數(shù)據(jù)行時(shí)提示消息,需要的朋友可以參考下
    2014-08-08
  • 實(shí)例講解C#中的職責(zé)鏈模式

    實(shí)例講解C#中的職責(zé)鏈模式

    這篇文章主要介紹了C#中的職責(zé)鏈模式的相關(guān)資料,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • 詳解c# 接口IDisposable的用法

    詳解c# 接口IDisposable的用法

    這篇文章主要介紹了詳解c# 接口IDisposable的用法,幫助大家更好的理解和學(xué)習(xí)c#,感興趣的朋友可以了解下
    2020-10-10
  • C#實(shí)現(xiàn)讀取寫入Json文件

    C#實(shí)現(xiàn)讀取寫入Json文件

    這篇文章主要介紹了C#實(shí)現(xiàn)讀取寫入Json文件方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • C#中的局部變量沖突問題

    C#中的局部變量沖突問題

    今天小編就為大家分享一篇C#中的局部變量沖突問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • C# 腳本引擎CS-Script的使用

    C# 腳本引擎CS-Script的使用

    這篇文章主要介紹了C#腳本引擎CS-Script的使用,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-12-12
  • Unity切割圖集轉(zhuǎn)換為多張圖片

    Unity切割圖集轉(zhuǎn)換為多張圖片

    這篇文章主要為大家詳細(xì)介紹了Unity切割圖集轉(zhuǎn)換為多張圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • 淺談二叉查找樹的集合總結(jié)分析

    淺談二叉查找樹的集合總結(jié)分析

    本篇文章是談二叉查找樹進(jìn)行了詳細(xì)的總結(jié)分析,需要的朋友參考下
    2013-05-05
  • c# 線程安全隊(duì)列的用法原理及使用示例

    c# 線程安全隊(duì)列的用法原理及使用示例

    這篇文章主要介紹了c# 線程安全隊(duì)列的用法原理及使用示例,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
    2020-11-11
  • c#網(wǎng)絡(luò)喚醒功能實(shí)現(xiàn)

    c#網(wǎng)絡(luò)喚醒功能實(shí)現(xiàn)

    網(wǎng)絡(luò)喚醒實(shí)現(xiàn)了對網(wǎng)絡(luò)的集中管理,即在任何時(shí)刻,網(wǎng)管中心的IT管理人員可以經(jīng)由網(wǎng)絡(luò)遠(yuǎn)程喚醒一臺處于休眠或關(guān)機(jī)狀態(tài)的計(jì)算機(jī),下面使用c#實(shí)現(xiàn)網(wǎng)絡(luò)喚醒功能
    2014-01-01

最新評論