如何获取UserPrincipal类未表示的Active Directory属性

Mon*_*yak 22 c# directoryservices active-directory

我的意思是,现在我正在使用System.DirectoryServices.AccountManagement,如果我使用UserPrincipal类,我只看到名称,中间名等

所以在我的代码中它就像

UserPrincipal myUser = new UserPrincipal(pc);
myUser.Name = "aaaaaa";
myUser.SamAccountName = "aaaaaaa";
.
.
.
.
myUser.Save();
Run Code Online (Sandbox Code Playgroud)

我怎样才能看到像手机或信息这样的属性?

mar*_*c_s 25

在这种情况下,您需要DirectoryEntry从用户主体中获取更深层次 - 返回到更大的内容:

using (DirectoryEntry de = myUser.GetUnderlyingObject() as DirectoryEntry)
{
    if (de != null)
    {
        // Go for those attributes and do what you need to do...
        var mobile = de.Properties["mobile"].Value as string;
        var info = de.Properties["info"].Value as string;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 下面是一个获取Common-Name`de.Properties ["cn"]的例子.Value.ToString();` (2认同)

Ray*_*und 13

这样做的正确的方法是用PrincipalExtensions在这里你延长Principal你之后使用的方法ExtensionSet,并ExtensionGet为解释在这里.