C# 如何在C中使用UserPrincipal获取Active Directory中地址的详细信息#
我正忙于在我的GUI应用程序中创建搜索功能,该应用程序正在我的Windows服务器上运行,用于添加、删除、更新和搜索用户。 我几乎完成了应用程序的构建,但我无法解决从UserPrincipal中未提供的其他属性(如“Address”属性)获取详细信息的问题。 我怎样才能进入那所房子 我尝试了许多编码方式来进入给定的属性“地址”,但它仍然不起作用 代码如下:C# 如何在C中使用UserPrincipal获取Active Directory中地址的详细信息#,c#,active-directory,userprincipal,C#,Active Directory,Userprincipal,我正忙于在我的GUI应用程序中创建搜索功能,该应用程序正在我的Windows服务器上运行,用于添加、删除、更新和搜索用户。 我几乎完成了应用程序的构建,但我无法解决从UserPrincipal中未提供的其他属性(如“Address”属性)获取详细信息的问题。 我怎样才能进入那所房子 我尝试了许多编码方式来进入给定的属性“地址”,但它仍然不起作用 代码如下: private void ListOfUsers(String ou) { List<string> users = n
private void ListOfUsers(String ou)
{
List<string> users = new List<string>();
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "EMRE", "OU=" + ou + ",dc=emre,dc=han");
UserPrincipal qbeUser = new UserPrincipal(ctx);
PrincipalSearcher search = new PrincipalSearcher(qbeUser);
foreach (UserPrincipal user in search.FindAll())
{
users.Add(user.UserPrincipalName);
users.Add("********");
users.Add(user.GivenName);
users.Add(user.Surname);
if (user.GetUnderlyingObjectType() == typeof(DirectoryEntry))
{
using (var entry = (DirectoryEntry)user.GetUnderlyingObject())
{
if (entry.Properties["Address"] != null)
users.Add(entry.Properties["Street"].Value.ToString());
}
}
users.Add(user.VoiceTelephoneNumber);
users.Add(user.EmailAddress);
users.Add(ou);
}
string[] row = users.ToArray();
var listViewItem = new ListViewItem(row);
lstStudents.Items.Add(listViewItem);
}
private void列表用户(字符串ou)
{
列表用户=新列表();
PrincipalContext ctx=新PrincipalContext(ContextType.Domain,“EMRE”,“OU=“+OU+”,dc=EMRE,dc=han”);
UserPrincipal qbeUser=新的UserPrincipal(ctx);
PrincipalSearcher=新PrincipalSearcher(qbeUser);
foreach(search.FindAll()中的UserPrincipal用户)
{
users.Add(user.UserPrincipalName);
用户。添加(“*******”);
users.Add(user.GivenName);
users.Add(用户.姓氏);
if(user.GetUnderlyingObjectType()==typeof(DirectoryEntry))
{
使用(var entry=(DirectoryEntry)user.getUnderlineingObject())
{
if(entry.Properties[“Address”!=null)
添加(entry.Properties[“Street”].Value.ToString());
}
}
users.Add(user.VoiceTelephoneNumber);
users.Add(user.EmailAddress);
用户。添加(ou);
}
string[]行=users.ToArray();
var listViewItem=新listViewItem(行);
lststudens.Items.Add(listViewItem);
}
即使属性不为null,我也会返回null值您想要的属性实际上已被调用。您还可以使用检查该值是否存在(尽管效果实际上与检查
null
,只是更易于阅读)
if(entry.Properties.Contains(“streetAddress”))
添加(entry.Properties[“streetAddress”].Value.ToString());
就我个人而言,我一般喜欢直接使用
DirectoryEntry
/DirectorySearcher
,而不是UserPrincipal
/PrincipalSearcher
,因为它能让我更好地控制自己的工作,从而提高性能。我在这里写了一些相关内容:我以前是这样做的,但在使用DirectoryEntry遇到一些问题后,我将方法更改为UserPrincipalUserPrincipal
只是在后台使用DirectoryEntry
,因此我相信您遇到的任何问题都可以得到解决。但是如果你现在让它工作(并且表现得可以接受),那就是最重要的。我应该在创建用户时也这样做吗?因此,首先我必须创建用户,保存它,然后确定一个名为“Address”的属性,然后将详细信息放入其中?是的,您应该在任何地方使用streetAddress
。这是Outlook查看的属性,这是由广告用户和计算机中“地址”选项卡上的“街道”字段更改的属性。