Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Active directory Active Directory中的用户信息_Active Directory_C# 2.0_Webpage_Asp.net 3.5 - Fatal编程技术网

Active directory Active Directory中的用户信息

Active directory Active Directory中的用户信息,active-directory,c#-2.0,webpage,asp.net-3.5,Active Directory,C# 2.0,Webpage,Asp.net 3.5,我正在使用用户的ID(用户名)从广告中获取他/她的信息。我想知道是否可以使用其他标准,如姓氏、电子邮件地址等来执行相同的操作 这就是我现在要过滤掉的用户: string adPath = ConfigurationManager.AppSettings["ADPath"].ToString(); DirectoryEntry de = new DirectoryEntry(adPath); DirectorySearcher deSearch =

我正在使用用户的ID(用户名)从广告中获取他/她的信息。我想知道是否可以使用其他标准,如姓氏、电子邮件地址等来执行相同的操作

这就是我现在要过滤掉的用户:

        string adPath = ConfigurationManager.AppSettings["ADPath"].ToString();
        DirectoryEntry de = new DirectoryEntry(adPath);
        DirectorySearcher deSearch = new DirectorySearcher();
        deSearch.SearchRoot = de;
        string sFilter = String.Format("(&(objectClass=user)(SAMAccountName={0}))", UserID);
        deSearch.Filter = sFilter;
        deSearch.SearchScope = SearchScope.Subtree;
        SearchResult results = deSearch.FindOne();
谢谢

编辑(使用Mrc_的建议):

使用(adPrincipalContext)
{
UserPrincipal qbeUser=新用户主体(adPrincipalContext);
qbeUser.GivenName=“Bruce”;
qbeUser.name=“Miller”;
PrincipalSearcher srch=新PrincipalSearcher(qbeUser);
foreach(在srch.FindAll()中找到的变量)
{
已找到UserPrincipal up=(UserPrincipal);
PrincipalSearchResult psr=up.GetGroups();
List insListPrincipal=新列表();
foreach(psr中的负责人p)
{
增加(p);
} 
foreach(psr中的主要gp)
{
字符串s1=gp.Name;
字符串s2=总说明;
}

当我试图在两个(内部)foreach循环中查找用户所属的组时,在一次迭代后,我会得到错误。列表(“indListPrincipal”)将包含18个条目,第一个是“域用户”,其余是pricnipal上下文的每个属性的错误。第二个foreach在第一次迭代后刚刚死亡。我得到的唯一条目是“域用户”组。似乎整个搜索结果在一次迭代后就被处理掉了。我做错了什么?

既然您使用的是.NET 3.5及更高版本,您应该查看
System.DirectoryServices.AccountManagement
(S.DS.am)命名空间。请在此处阅读所有相关内容:

基本上,您可以定义域上下文并在AD中轻松找到用户和/或组:

// set up domain context
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
    // find a user
    UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");

    if(user != null)
    {
       // do something here....     
    }
}
新的S.DS.AM使得在广告中与用户和群组进行互动变得非常容易

您可以直接访问-如果您需要其他人,您甚至可以根据需要扩展您的
UserPrincipal

更新:如果
FindByIdentity
搜索依据的各种属性对您来说不够,请使用带有“示例查询”主体的
PrincipalSearcher
进行搜索:

// create your domain context
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
   // define a "query-by-example" principal - here, we search for a UserPrincipal 
   // and with the first name (GivenName) of "Bruce" and a last name (Surname) of "Miller"
   UserPrincipal qbeUser = new UserPrincipal(ctx);
   qbeUser.GivenName = "Bruce";
   qbeUser.Surname = "Miller";
   // of course, you can set **ANY** of the UserPrincipal properties here

   // create your principal searcher passing in the QBE principal    
   PrincipalSearcher srch = new PrincipalSearcher(qbeUser);

   // find all matches
   foreach(var found in srch.FindAll())
   {
       // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....          
   }
}

由于您使用的是.NET 3.5及更高版本,因此应该查看
System.DirectoryServices.AccountManagement
(S.DS.AM)命名空间。请在此处阅读所有相关内容:

基本上,您可以定义域上下文并在AD中轻松找到用户和/或组:

// set up domain context
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
    // find a user
    UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");

    if(user != null)
    {
       // do something here....     
    }
}
新的S.DS.AM使得在广告中与用户和群组进行互动变得非常容易

您可以直接访问-如果您需要其他人,您甚至可以根据需要扩展您的
UserPrincipal

更新:如果
FindByIdentity
搜索依据的各种属性对您来说不够,请使用带有“示例查询”主体的
PrincipalSearcher
进行搜索:

// create your domain context
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
   // define a "query-by-example" principal - here, we search for a UserPrincipal 
   // and with the first name (GivenName) of "Bruce" and a last name (Surname) of "Miller"
   UserPrincipal qbeUser = new UserPrincipal(ctx);
   qbeUser.GivenName = "Bruce";
   qbeUser.Surname = "Miller";
   // of course, you can set **ANY** of the UserPrincipal properties here

   // create your principal searcher passing in the QBE principal    
   PrincipalSearcher srch = new PrincipalSearcher(qbeUser);

   // find all matches
   foreach(var found in srch.FindAll())
   {
       // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....          
   }
}

谢谢。似乎主要的方法是FindByIdentity()。我可以通过其他方式查找吗,如姓、姓+名、电话、电子邮件等?@NoBullMan:
FindByIdentity
已经通过许多属性进行搜索-查看MSDN文档了解详细信息。如果需要更灵活的搜索,请查看
PrincipalSearcher
(在MSDN上也有很好的记录)谢谢marc_,我想我明白了!只有一个问题:在foreach循环中,我看不到能够获取用户的电话(“VoiceTelephoneNumber”)。我必须投“found”吗要成为我想要的任何原则类型,如UserPrincipal?marc_s,我可以获取用户信息,但当我尝试获取组信息时,我遇到了一些问题。我更新了原始帖子。@NoBullMan:
VoiceTelephoneNumber
是一个仅在
UserPrincipal
上可用的属性-因此,是的,您必须强制转换通用
Principal
是一个
UserPrincipal
来处理这个问题的。关于组的整个故事应该在一个新问题中问出来-这太多了,无法用越来越多的内容更新原始问题…谢谢。似乎主要的方法是FindByIdentity()。我可以通过其他方式(如姓氏、姓氏+名、电话、电子邮件等)查找吗?@NoBullMan:
FindByIdentity
已经通过许多属性进行搜索-查看MSDN文档了解详细信息。如果您需要更灵活的搜索,请查看
PrincipalSearcher
(在MSDN上也有很好的文档记录)谢谢marc_s,我想我明白了!只有一个问题:在foreach循环中,我看不到能够获取用户的电话(“VoiceTelephoneNumber”)。我必须投“found”吗要成为我想要的任何原则类型,如UserPrincipal?marc_s,我可以获取用户信息,但当我尝试获取组信息时,我遇到了一些问题。我更新了原始帖子。@NoBullMan:
VoiceTelephoneNumber
是一个仅在
UserPrincipal
上可用的属性-因此,是的,您必须强制转换通用
Principal
是你找到的一个
UserPrincipal
来处理这个问题。关于小组的整个故事应该在一个新的问题中提出——这太多了,无法用越来越多的内容来更新你原来的问题。。。