我有一个应用程序可以用来设置或清除一些AD属性extensionAttribute2
。如何清除扩展属性
一切似乎工作正常,但更新我的颂歌以清除该属性时使用Property.Clear()
方法,我遇到了一些问题。
调用ActiveDirectory.ClearProperty("user.name", "extensionAttribute2")
后,该财产被清除,但是当我尝试ActiveDirectory.GetProperty("user.name", "extensionAttribute2", "123456789")
设置它,我得到一个错误:
Object reference not set to an instance of an object.
而且我可以看到extensionAttribute2
尚未在DirectoryEntry
对象加载的用户。
如果我更改了ClearProperty
代码调用user.Properties[property].Value = " ";
那么它似乎正常工作(即物业再次询问用户时仍然存在),但是我觉得利用Clear()
。
这是AD的正常行为吗?我得到的印象是,调用Clear
只是清除了价值而不是实际上破坏了财产,或者这是extensionAttribute
s的一个特征?我想使用Clear
,因为它看起来更清晰 - 这看起来合理还是坚持user.Properties[property].Value = " ";
真的是最好的选择?
在此先感谢您的帮助。
private static DirectoryEntry GetUser(string friendlyName)
{
var userEntry = new DirectoryEntry("LDAP://dc=DOMAIN,dc=co,dc=uk");
var mySearcher = new DirectorySearcher(userEntry)
{ Filter = $"(cn={friendlyName})" };
mySearcher.PropertiesToLoad.Add("extensionAttribute2");
mySearcher.PropertiesToLoad.Add("distinguishedName");
var userSearchResult = mySearcher.FindOne();
var distinguishedName = userSearchResult.Properties["distinguishedName"][0].ToString();
var userDirectoryEntry = new DirectoryEntry($"LDAP://{distinguishedName}");
return userDirectoryEntry;
}
public static string GetProperty(string friendlyname, string property)
{
var user = GetUser(friendlyname);
return user.Properties[property].Value.ToString();
}
public static void SetProperty(string friendlyName, string property, string value)
{
var user = GetUser(friendlyName);
user.Properties[property].Value = value;
user.CommitChanges();
}
public static void ClearProperty(string friendlyName, string property)
{
var user = GetUser(friendlyName);
user.Properties[property].Clear();
user.CommitChanges();
}