我使用 DirectoryEntry 和 DirectorySearcher 已經有一段時間了,它總是有效。最近我了解了 AccountManagement,并認為我會在一個新項目中嘗試它。但我無法讓它找到我。這個舊代碼工作正常:Using oDirectoryEntry As DirectoryEntry = New DirectoryEntry("LDAP://us.psy.com", "xxx2yyy", "MyStrongPwd") Using oDirectorySearcher As DirectorySearcher = New DirectorySearcher(oDirectoryEntry) oDirectorySearcher.Filter = "(&(sAMAccountType=805306368)(sAMAccountName=xxx2yyy))" Try Return oDirectorySearcher.FindOne IsNot Nothing Catch Return False End Try End UsingEnd Using但我無法完成這項工作:using (PrincipalContext context = new PrincipalContext(ContextType.Domain, "US", "DC=psy,DC=com")){ MessageBox.Show(context.ConnectedServer); // This shows me the server name using (UserPrincipal user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, "xxx2yyy")) { MessageBox.Show(user.SamAccountName); // results in Object reference not set to an instance of an object user.ChangePassword("OldPwd", "NewPwd"); user.Save(); }}希望有人能看到我做錯了什么。
1 回答

白衣染霜花
TA貢獻1796條經驗 獲得超10個贊
我認為 marc_s 走在正確的軌道上。但是您可以像使用DirectoryEntry
.?您可以使用僅包含域名的構造函數,如下所示:
using?(PrincipalContext?context?=?new?PrincipalContext(ContextType.Domain,?"us.psy.com"))
這將搜索您的整個域。
也就是說,如果您已經知道如何使用DirectoryEntry
and?DirectorySearcher
,那么最好堅持使用它。無論如何,命名AccountManagement
空間只是在后臺使用它們。它可以使一些事情變得更容易,但它對你隱藏了很多東西,這會損害性能。直接使用DirectoryEntry
andDirectorySearcher
幾乎總是會執行得更快。
- 1 回答
- 0 關注
- 166 瀏覽
添加回答
舉報
0/150
提交
取消