C# 在c中“如何设置活动的directy字段”;“办公室”;因此,我可以在Outlook中显示用户的位置

C# 在c中“如何设置活动的directy字段”;“办公室”;因此,我可以在Outlook中显示用户的位置,c#,.net,active-directory,C#,.net,Active Directory,在c#中,我试图设置office字段 当我这样做时: ADEntry.Properties[“office”]。添加(“阿拉斯加”) 它说办公室不存在 有人能告诉我在哪里可以到这个地方吗 谢谢 Cal-签出-他有大量关于AD UI中的哪个属性映射到目录条目中的哪个底层AD属性的Excel参考表 您具体的“office”示例映射到DirectoryEntry的集合中名为physicalDeliveryOfficeName的属性。经过长期研究,我得到了它 string Username = Sys

在c#中,我试图设置office字段

当我这样做时:

ADEntry.Properties[“office”]。添加(“阿拉斯加”)

它说办公室不存在

有人能告诉我在哪里可以到这个地方吗

谢谢

Cal-

签出-他有大量关于AD UI中的哪个属性映射到
目录条目中的哪个底层AD属性的Excel参考表


您具体的“office”示例映射到
DirectoryEntry
集合中名为
physicalDeliveryOfficeName
的属性。

经过长期研究,我得到了它

string Username = System.Security.Principal.WindowsIdentity.GetCurrent().Name.Substring(System.Security.Principal.WindowsIdentity.GetCurrent().Name.IndexOf("\\") + 1);

 string office = string.Empty;

    using (var context = new PrincipalContext(ContextType.Domain, ConfigurationManager.AppSettings["DOMAIN"].ToString()))
    {
        using (var userPrincipal = new UserPrincipal(context))
        {
            userPrincipal.SamAccountName = Username;

            using (PrincipalSearcher search = new PrincipalSearcher(userPrincipal))
            {
                UserPrincipal result = (UserPrincipal)search.FindOne();

                DirectoryEntry directoryEntry = result.GetUnderlyingObject() as DirectoryEntry;

                if (directoryEntry.Properties["physicalDeliveryOfficeName"].Count > 0
                        && directoryEntry.Properties["physicalDeliveryOfficeName"][0] != null
                        && !string.IsNullOrWhiteSpace(directoryEntry.Properties["physicalDeliveryOfficeName"][0].ToString()))
                {
                    office = directoryEntry.Properties["physicalDeliveryOfficeName"][0].ToString();
                }
            }
        }
    }

谢谢你的力量是强大的!