使用 C# 进行 AD 请求
我试图使用以下代码从 AD 获取所有用户及其密码到期日期,但出现错误。
System.InvalidOperationException:“调用此方法之前必须保留主体对象。”
代码:
PrincipalContext domain = new PrincipalContext(ContextType.Domain,"AAA","OU=USERS,OU=TEST,DC=AAA,DC=AA, DC=A");
UserPrincipal user = new UserPrincipal(domain);
PrincipalSearcher pS = new PrincipalSearcher(user);
foreach (UserPrincipal result in pS.FindAll())
{
if (result != null && result.DisplayName != null)
{
DirectoryEntry entry = (DirectoryEntry)user.GetUnderlyingObject();
IADsUser native = (IADsUser)entry.NativeObject;
Console.WriteLine(result.DisplayName, native.PasswordExpirationDate);
}
}
I'm trying to get all users from AD and their passwords expiration dates with the following code, but got the error.
System.InvalidOperationException: 'The Principal object must be persisted before this method can be called.'
The code:
PrincipalContext domain = new PrincipalContext(ContextType.Domain,"AAA","OU=USERS,OU=TEST,DC=AAA,DC=AA, DC=A");
UserPrincipal user = new UserPrincipal(domain);
PrincipalSearcher pS = new PrincipalSearcher(user);
foreach (UserPrincipal result in pS.FindAll())
{
if (result != null && result.DisplayName != null)
{
DirectoryEntry entry = (DirectoryEntry)user.GetUnderlyingObject();
IADsUser native = (IADsUser)entry.NativeObject;
Console.WriteLine(result.DisplayName, native.PasswordExpirationDate);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该异常意味着您正在对尚未保存的
UserPrincipal
对象调用GetUnderlyingObject()
。您正在为
PrincipalSearcher
创建作为过滤器的user
对象调用GetUnderlyingObject()
。我认为您打算在结果
上调用它:The exception means that you're calling
GetUnderlyingObject()
on aUserPrincipal
object that hasn't been saved.You're calling
GetUnderlyingObject()
on theuser
object that you created as the filter for yourPrincipalSearcher
. I think you intended to call it onresult
: