Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.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# .Net代码将Active Directory属性设置为;“未设置”;_C#_.net_Vb.net_Active Directory_Ldap - Fatal编程技术网

C# .Net代码将Active Directory属性设置为;“未设置”;

C# .Net代码将Active Directory属性设置为;“未设置”;,c#,.net,vb.net,active-directory,ldap,C#,.net,Vb.net,Active Directory,Ldap,在Active Directory mmc管理单元中,您看不到“未设置”的属性。当您使用ADSIEDIT.MSC工具时,如果属性值为null,则会将其视为“未设置” 如何在.Net代码中将属性设置为“未设置” 下面是Powershell中的答案,但我需要使用一些.Net代码(VB.Net/C#)来完成。 ps MSExchHideAddressList是罪魁祸首属性,当其在此域中为True或False时,会阻止用户信息从AD复制到Sharepoint 在中,您可以找到: 在支持LDAP的常用目

在Active Directory mmc管理单元中,您看不到“未设置”的属性。当您使用ADSIEDIT.MSC工具时,如果属性值为null,则会将其视为“未设置”

如何在.Net代码中将属性设置为“未设置”

下面是Powershell中的答案,但我需要使用一些.Net代码(VB.Net/C#)来完成。

ps MSExchHideAddressList是罪魁祸首属性,当其在此域中为True或False时,会阻止用户信息从AD复制到Sharepoint

在中,您可以找到:

在支持LDAP的常用目录中,不存在没有值的属性。当通过更改、替换或追加操作将属性值设置为非空值时,如果该属性不存在,则会创建该属性。类似地,如果将属性修改为没有值,则将删除整个属性。有时,您可能希望将属性设置为null。虽然在支持LDAP的目录中不存在此概念,但可以通过完全删除属性并指定要清除的属性来完成此操作

下面是使用
System.DirectoryServices
的示例:

/* Connection to Active Directory
 */
DirectoryEntry deBase = new DirectoryEntry("LDAP://192.168.183.220:389/dc=societe,dc=local", "administrateur", "adm");

/* Directory Search
 */
DirectorySearcher dsLookForOUs = new DirectorySearcher(deBase);
dsLookForOUs.Filter = "(objectCategory=organizationalUnit)";
dsLookForOUs.SearchScope = SearchScope.Subtree;
dsLookForOUs.PropertiesToLoad.Add("cn");
dsLookForOUs.PropertiesToLoad.Add("ou");
dsLookForOUs.PropertiesToLoad.Add("telephoneNumber");

SearchResultCollection srcOUs = dsLookForOUs.FindAll();

foreach (SearchResult srOU in srcOUs)
{
  Console.WriteLine("{0}", srOU.Path);
  DirectoryEntry de = srOU.GetDirectoryEntry();
  if (de.Properties["TelephoneNumber"].Value!= null)
  {
    // Both solutions are working. Don't forget to commit

    //de.Properties["TelephoneNumber"].Clear();
    de.Properties["TelephoneNumber"].Value=null;
    de.CommitChanges();
  }
}

轻微更正:属性是否可以没有值(或空值)取决于属性的语法。例如,IA5String允许为零长度。那么这不被视为“无值”,而是“空值”。没有真正为空的值。