Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Active Directory中添加自定义属性_C#_Active Directory - Fatal编程技术网

C# 在Active Directory中添加自定义属性

C# 在Active Directory中添加自定义属性,c#,active-directory,C#,Active Directory,有人能帮我找到如何使用C#将自定义属性/类添加到Active Directory架构吗?我可以使用MMC/a手动添加。但现在我想用C代码来做。有人能给我这个的样品吗 提前谢谢 这是一个非常重要的方法: 特定于目标的域控制器或凭据 public static ArrayList GetUsedAttributes(string objectDn) { DirectoryEntry objRootDSE = new DirectoryEntry("LDAP://" + objectDn);

有人能帮我找到如何使用C#将自定义属性/类添加到Active Directory架构吗?我可以使用MMC/a手动添加。但现在我想用C代码来做。有人能给我这个的样品吗


提前谢谢

这是一个非常重要的方法:

特定于目标的域控制器或凭据

public static ArrayList GetUsedAttributes(string objectDn)
{
    DirectoryEntry objRootDSE = new DirectoryEntry("LDAP://" + objectDn);
    ArrayList props = new ArrayList();

    foreach (string strAttrName in objRootDSE.Properties.PropertyNames)
    {
        props.Add(strAttrName);
    }
    return props;
}
在代码中的任何地方:LDAP://可以替换为LDAP://MyDomainControllerNameOrIpAddress,也可以在构建DirectoryEntry类的任何地方发送特定凭据。如果您的计算机不是Active Directory的林或域的成员,或者您希望以DC为目标对其进行更改,则这一点尤其有用

//重命名对象并直接指定域控制器和凭据

public static void Rename(string server,
    string userName, string password, string objectDn, string newName)
{
    DirectoryEntry child = new DirectoryEntry("LDAP://" + server + "/" + 
        objectDn, userName, password);
    child.Rename("CN=" + newName);
}
使用DirectoryEntry管理本地帐户

需要注意的是,如果需要,只需将LDAP://字符串替换为WinNT://即可对本地计算机而不是Active Directory执行其中一些方法,如下所示

//创建新的本地帐户

DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + 
    Environment.MachineName);
DirectoryEntry newUser = localMachine.Children.Add("localuser", "user");
newUser.Invoke("SetPassword", new object[] { "3l!teP@$$w0RDz" });
newUser.CommitChanges();
Console.WriteLine(newUser.Guid.ToString());
localMachine.Close();
newUser.Close();
使用DirectoryEntry管理本地组

需要对代码进行一些配置更改,但这非常简单。下面是使用DirectoryEntry枚举本地“管理员”组成员的示例

使用DirectoryEntry管理IIS

除了管理本地和目录服务帐户外,通用DirectoryEntry对象还可以管理其他网络提供程序,如IIS。下面是如何使用DirectoryEntry在IIS中设置新虚拟目录的示例

//使用DirectoryEntry()在IIS中创建新的虚拟目录

现在是Active Directory对象:

枚举对象的多字符串属性值

public string AttributeValuesSingleString
    (string attributeName, string objectDn)
{
    string strValue;
    DirectoryEntry ent = new DirectoryEntry(objectDn);
    strValue = ent.Properties[attributeName].Value.ToString();
    ent.Close();
    ent.Dispose();
    return strValue;
}
如果要递归地挖掘属性的属性,例如枚举组的所有成员值,然后获取树上所有成员组的组,则此方法包含一个递归标志

public ArrayList AttributeValuesMultiString(string attributeName,
     string objectDn, ArrayList valuesCollection, bool recursive)
{
    DirectoryEntry ent = new DirectoryEntry(objectDn);
    PropertyValueCollection ValueCollection = ent.Properties[attributeName];
    IEnumerator en = ValueCollection.GetEnumerator();

    while (en.MoveNext())
    {
        if (en.Current != null)
        {
            if (!valuesCollection.Contains(en.Current.ToString()))
            {
                valuesCollection.Add(en.Current.ToString());
                if (recursive)
                {
                    AttributeValuesMultiString(attributeName, "LDAP://" +
                    en.Current.ToString(), valuesCollection, true);
                }
            }
        }
    }
    ent.Close();
    ent.Dispose();
    return valuesCollection;
}
枚举对象的单个字符串属性值

public string AttributeValuesSingleString
    (string attributeName, string objectDn)
{
    string strValue;
    DirectoryEntry ent = new DirectoryEntry(objectDn);
    strValue = ent.Properties[attributeName].Value.ToString();
    ent.Close();
    ent.Dispose();
    return strValue;
}
枚举对象的属性:具有值的属性

public static ArrayList GetUsedAttributes(string objectDn)
{
    DirectoryEntry objRootDSE = new DirectoryEntry("LDAP://" + objectDn);
    ArrayList props = new ArrayList();

    foreach (string strAttrName in objRootDSE.Properties.PropertyNames)
    {
        props.Add(strAttrName);
    }
    return props;
}

有趣的是,无论是提议的答案还是问题作者的示例都没有真正回答原始问题:

如何在C#中向Active Directory架构添加自定义属性/类?

通过这个问题,我明白了,添加了一个在AD模式中还没有出现的属性。当您看到添加属性实际上意味着更新广告模式时,我怀疑C代码是否支持:

我已经尝试了上述方法,但是这里没有提到如何添加自定义属性。我想在现有属性列表中添加新属性。我已经编写了以下代码来创建新用户,现在我想添加一些自定义属性字符串connectionPrefix=“LDAP://”+ldapPath;DirectoryEntry dirEntry=新的DirectoryEntry(connectionPrefix);DirectoryEntry newUser=dirEntry.Children.Add(“CN=”+“user”,用户名);newUser.Properties[“samAccountName”]。Value=用户名;newUser.CommitChanges();oGUID=newUser.Guid.ToString();调用(“SetPassword”,新对象[]{userPassword});newUser.CommitChanges();该代码已经出现在我发布的链接中,在“创建用户帐户”下,我要求您提供创建这些自定义属性的代码。至少要正确复制:DirectoryEntry newUser=dirEntry.Children.Add(“CN=“+userName,”user”);不是子项。添加(“CN=”+“user”,用户名);