Tha*_*amy 7 ldap active-directory c#-4.0
我有场景使用LDAP和C#在Active Directory中创建新组.
请提供建议
这篇关于CodeProject的文章是一个非常好的起点:
Howto :(几乎)通过C#在Active Directory中的一切
要创建组,您需要:
码:
public void Create(string ouPath, string name)
{
if (!DirectoryEntry.Exists("LDAP://CN=" + name + "," + ouPath))
{
try
{
// bind to the container, e.g. LDAP://cn=Users,dc=...
DirectoryEntry entry = new DirectoryEntry("LDAP://" + ouPath);
// create group entry
DirectoryEntry group = entry.Children.Add("CN=" + name, "group");
// set properties
group.Properties["sAmAccountName"].Value = name;
// save group
group.CommitChanges();
}
catch (Exception e)
{
Console.WriteLine(e.Message.ToString());
}
}
else { Console.WriteLine(path + " already exists"); }
}
Run Code Online (Sandbox Code Playgroud)
有关设置组范围和组类型的一些附加信息,枚举是:
public enum GroupType : uint
{
GLOBAL = 0x2,
DOMAIN_LOCAL = 0x4,
UNIVERSAL = 0x8,
SECURITY = 0x80000000
}
Run Code Online (Sandbox Code Playgroud)
安全性(从ADS_GROUP_TYPE_SECURITY_ENABLED缩短)与前3个枚举相结合,为您提供6种可能的选项,如果没有它,则组将成为通讯组.
这些值设置为int,其中安全标志为负数,因此需要使用unchecked().或者,您可以为组合值创建枚举.
GLOBAL | SECURITY = 0x80000002 = -2147483646
DOMAIN_LOCAL | SECURITY = 0x80000004 = -2147483644
UNIVERSAL | SECURITY = 0x80000008 = -2147483640
Run Code Online (Sandbox Code Playgroud)
该值存储在'groupType'属性中:
var groupType = unchecked((int)(GroupType.UNIVERSAL | GroupType.SECURITY));
group.Properties["groupType"].Value = groupType;
group.CommitChanges();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15002 次 |
| 最近记录: |