开始使用System.DirectoryServices.AccountManagement
命名空间,对活动目录 (AD) 中的用户执行查找。我还需要用户的 manager,但我似乎在使用这个命名空间时遇到了麻烦。当前获取人的代码:
class Person {
// Fields
public string GivenName = null;
public string Surname = null;
public string DistinguishedName = null;
public string Email = null;
public string MangerDistinguishedName = null; // Unable to set this
// Constructor
public Person(string userName) {
UserPrincipal user = null;
try {
user = GetUser(userName);
if (user != null) {
this.GivenName = user.GivenName;
this.Surname = user.Surname;
this.DistinguishedName = user.DistinguishedName;
this.Email = user.EmailAddress;
this.MangerDistinguishedName = user.<NO SUCH PROPERTY TO FIND A MANAGER'S DISTINGUISHED NAME>
}
else {
throw new MissingPersonException("Person not found");
}
}
catch (MissingPersonException ex) {
MessageBox.Show(
ex.Message
, ex.reason
, MessageBoxButtons.OK
, MessageBoxIcon.Error
);
}
catch (Exception ex) {
MessageBox.Show(
ex.Message
, "Error: Possible connection failure, or permissions failure to search for the username provided."
, MessageBoxButtons.OK
, MessageBoxIcon.Error
);
}
finally {
user.Dispose();
}
}
执行人员搜索
private UserPrincipal GetUser(string userName) {
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, userName);
return user;
}
直接访问特定用户的经理的专有名称的另一种方法是什么?