我想检查一个选定的用户是否存在于一个 OU 中(通过他/她登录的用户名),完成这项工作的最正确方法是什么? 之后我想选择用户并更改他/她的密码。
我在这里找到了一些帮助:http://www.codeproject.com/KB/system/everythingInAD.aspx#46
但是我找到的代码是这样的:
public static bool Exists(string objectPath)
{
bool found = false;
if (DirectoryEntry.Exists("LDAP://" + objectPath))
{
found = true;
}
return found;
}
可以概括为:
return DirectoryEntry.Exists("LDAP://" + objectPath);
所以我真的不知道该信任谁,如果我只有用户名、OU 名称和域名,我应该将什么作为 objectPath 传递。
请帮忙。
谢谢。
请您参考如下方法:
由于用户名在一个域中需要是唯一的,我认为我不会过分关注 OU。构建它可能会使您的代码更加脆弱并使其更加复杂。我会尝试使用新的 UserPrincipal如果可以,请上课。
using (var context = new PrincipalContext( ContextType.Domain ))
{
using (var user = UserPrincipal.FindByIdentity( context, IdentityType.SamAccountName, userName ))
{
if (user != null)
{
user.ChangePassword( oldPassword, newPassword );
// or if you don't have the user's old password and
// do have enough privileges.
// user.SetPassword( newPassword );
}
}
}
