使用C#将域中的用户添加到本地用户组 [英] Adding user from a domain, to local user group with c#

查看:225
本文介绍了使用C#将域中的用户添加到本地用户组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我可以使用以下代码将域用户添加到本地组

Right now i am able to add a domain user to local group with  below code

        //工作代码
       字符串userPath = string.Format("WinNT://{0}/{1},user","mydomain.com",userName);
       字符串groupPath = string.Format("WinNT://{0}/{1},group",Environment.MachineName,groupName);
       使用(DirectoryEntry组= new DirectoryEntry(groupPath))
        {
          group.Invoke("add",userPath);
          group.CommitChanges();
        }

        // working code
        string userPath = string.Format("WinNT://{0}/{1},user", "mydomain.com", userName);
        string groupPath = string.Format("WinNT://{0}/{1},group", Environment.MachineName, groupName);
        using (DirectoryEntry group = new DirectoryEntry(groupPath))
        {
          group.Invoke("Add", userPath);
          group.CommitChanges();
        }

仅在搜索时,我遇到了一些博客,说使用Principal上下文比DirectoryEntry更好,但是却找不到使用PrincipalContext将域用户添加到本地组的引用.

Just while searching i came across some blogs saying its better to use Principal context than DirectoryEntry but am not able to find references to add domain user to local groups using PrincipalContext.

我想知道

1)使用PrincipalContext比使用DirectoryEntry来添加/删除本地组中的域用户真的更好吗?

1) Is it really a better idea to use PrincipalContext than DirectoryEntry for adding/ removing domain users to local groups

2)使用PrincipalContext将域用户添加到本地组的方法

2) ways to add domain user to localgroups using PrincipalContext 

-

推荐答案

我对问题一没有意见,因此只提供问题2的解决方案:

I have no opinion over question one, so will just supply solution to question 2:


这篇关于使用C#将域中的用户添加到本地用户组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆