gpt4 book ai didi

active-directory - LDAP:如何将新用户添加到 OU 内的组

转载 作者:行者123 更新时间:2023-12-04 06:42:15 25 4
gpt4 key购买 nike

我有一些代码使用 DirectoryEntry通过 LDAP 操作本地 Active Directory。目前我找到一个特定的OU ,向其中添加用户,更新用户的属性,然后提交所有更改:

DirectoryEntry ldapRoot = new DirectoryEntry(ldapString, user, password);
DirectoryEntry userGroup = ldapRoot.Children.Find("OU=OUGroup");
DirectoryEntry newUser = userGroup.Children.Add("CN=" + userName, "user");
newUser.Properties["displayName"].Value = displayName;

...

newUser.CommitChanges();
userGroup.Close();
ldapRoot.Close();

ldapString 类似于 LDAP:\\DC=company,DC=local ,基本上它只是获取根条目。

我更改了几个属性,但一切正常。但是,我还有一个 OU称为 SharePoint_Groups,其中有一个名为 Internal 的组.我想将新用户添加为该组的成员,但我不知道该怎么做。我尝试了以下方法:
DirectoryEntry spGroup = ldapRoot.Children.Find("OU=Sharepoint_Groups");
DirectoryEntry internal = spGroup.Children.Find("CN=Internal");

它不起作用,我不确定我应该如何处理 Internal - CN= 正确还是我应该使用其他规范?

而且,一旦我有了正确的组,如何将现有用户添加到其中?

提前致谢

最佳答案

基本上,要将用户添加到现有组,您需要绑定(bind)到该组并将其更新为 member属性,使用用户的完全限定专有名称:

DirectoryEntry deGroup = new DirectoryEntry("LDAP://CN=Internal,OU=Sharepoint_Groups,DC=Company,DC=local");

string userDN = newUser.Properties["distinguishedName"][0].ToString();

deGroup.Properties["member"].Add(userDN);
deGroup.CommitChanges();

CodeProject 文章 How to do just about everything in Active Directory using C# 是此类内容的绝佳资源。 - 大量有用的代码示例!

关于active-directory - LDAP:如何将新用户添加到 OU 内的组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4096161/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com