C# Active Directory用户在MVC中导入

C# Active Directory用户在MVC中导入,c#,model-view-controller,active-directory,ldap,C#,Model View Controller,Active Directory,Ldap,我正在尝试从LDAP服务器的Active Directory中获取特定组的所有用户。身份验证成功,但结果为空。 下面是我的代码 域-172.11.12.123 电子邮件-sample@email.com 密码-123456 using (var context = new DirectoryEntry(user.Domain, user.Email, user.Password, AuthenticationTypes.Secure)) {

我正在尝试从LDAP服务器的Active Directory中获取特定组的所有用户。身份验证成功,但结果为空。 下面是我的代码

域-172.11.12.123
电子邮件-sample@email.com
密码-123456

 using (var context = new DirectoryEntry(user.Domain,  user.Email, user.Password, AuthenticationTypes.Secure))
            {

                try
                {                    
                    string FirstName;
                    string LastName;
                    string ADUserName;
                    string Email;

                    using (var searcher = new DirectorySearcher(context))
                    {
                        searcher.Filter = "(&((&(objectCategory=Person)(objectClass=User)))(samaccountname='user3'))";
                        List<string> Adusers = new List<string>();
                        System.DirectoryServices.SearchResult result = searcher.FindOne();




                    }
                }
                catch (Exception ex)
                {
                    TempData["message"] = "error";
                    return RedirectToAction("Index", "ADuserList");
                }

            }
使用(var context=newdirectoryEntry(user.Domain、user.Email、user.Password、AuthenticationTypes.Secure))
{
尝试
{                    
字符串名;
字符串LastName;
字符串名称;
字符串电子邮件;
使用(var searcher=newdirectorysearcher(上下文))
{
searcher.Filter=“(&(&(&(objectCategory=Person)(objectClass=User))(samaccountname='user3'))”;
列表管理员=新列表();
System.DirectoryServices.SearchResult=searcher.FindOne();
}
}
捕获(例外情况除外)
{
TempData[“消息”]=“错误”;
返回重定向到操作(“索引”、“ADuserList”);
}
}
出什么事了。
提前感谢

如果您使用的是.NET 3.5及更高版本,您应该查看
System.DirectoryServices.AccountManagement
(S.DS.AM)命名空间

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

// set up domain context for the currently connected AD domain
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
    // find the group in question
    GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "YourGroupNameHere");

    // if found....
    if (group != null)
    {
       // iterate over members
       foreach (Principal p in group.GetMembers())
       {
           Console.WriteLine("{0}: {1}", p.StructuralObjectClass, p.DisplayName);
           // do whatever you need to do to those members
       }
    }
}
新的S.DS.AM使得在广告中与用户和群组进行互动变得非常容易

请在此处阅读更多信息:

更新:为了获得给定OU的所有用户,方法是完全不同的

您需要创建一个单独的
PrincipalContext
,定义您感兴趣的内容-然后您需要使用
PrincipalSearcher
从该OU获取所有用户:

// create your domain context and define what OU to use:
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, null, "OU=YourOU,OU=SubOU,dc=YourCompany,dc=com"))
{
   // define a "query-by-example" principal - here, we search for any UserPrincipal 
   UserPrincipal qbeUser = new UserPrincipal(ctx);

   // 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.....          
   }
}

如果您将rdp发送到dc并启动
dsa.msc
,然后转到search-advanced并粘贴您的查询,它是否有效?语法看起来有点奇怪(您有两个and,没有OR,只有一个,就足够了)使用过滤器samaccountname='user3'验证DirectoryEntry ctor中的第一个参数是否为LDAP://172.11.12.123您在Active Directory中搜索用户“user3”。验证此用户是否存在。也不需要单引号。只需键入samaccountname=user3thanks,我将检查,您能建议我正确的格式吗?您还需要获取嵌套组成员吗?例如,您希望检索属于组A的所有用户,而组B也属于组A。组B有自己的用户。这些用户也属于A组。如果这是您的情况,请查看我在感谢@oldovets上的评论,谢谢您的回复,解决方案也很有用,解决了我的问题。哇,非常感谢。这很有魅力。但当我在小组中通过OU(组织单位)时,它不起作用。在OU情况下该怎么办。@C.jacking:嗯,“OU”不是一个团体-所以这对OU不起作用,当然。。。。。你想干什么?列出OU中的所有用户?或者在你的广告树中找到OU?或者还有什么?我想要一个特定OU和组的所有用户的列表。您的代码已经解决了组的问题,但我也希望根据OU列出列表。先生,如何根据OU进行筛选