6
开始使用System.DirectoryServices.AccountManagement
命名空间,对活动目录(AD)中的用户执行查找。 我也需要用户的经理,但我似乎在使用这个命名空间的道路上碰到了一个碰撞。当前的代码来获得一个人:C# - 在活动目录中查找用户管理器
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;
}
什么是另一种方式来直接访问特定用户的管理者的专有名称?
你试过吗?这对我来说不起作用。 UserPrincipalEx.FindByIdentity不返回UserPrincipalEx对象,并且转换为UserPrincipalEx会导致InvalidCastException。 – Naikrovek 2013-03-27 19:47:45
@Naikrovek:对不起 - 我的错误 - 我从我的(更长的)样本中删除了太多的代码。我错过了两个重载的'FindByIdentity'和'FindByIdentityWithType'方法 - 我将这些添加到了我的代码片段 - 是的,通过这段代码,我只是对照Win Server 2008 R2 Active Directory进行了检查,并且对我来说工作正常。 – 2013-03-27 22:10:48
很好,谢谢。 – Naikrovek 2013-03-28 13:08:33