我有一個應用程序可以用來設置或清除一些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();
}