简体   繁体   English

.NET如何在Active Directory中搜索和获取用户列表

[英].NET How to search and get list of users in Active Directory

I've never programmed against Active directory, and MVC before and need some advice. 我以前从未针对Active Directory和MVC进行过编程,因此需要一些建议。

I'm using following code to search, and get list of users in a view. 我正在使用以下代码进行搜索,并获取视图中的用户列表。 I don't know if I'm on the right track, and how do I get it in a list view on cshtml? 我不知道自己走的路是否正确,如何在cshtml的列表视图中获取它?

   public ActionResult Find()
    {
        DirectoryEntry entry = new DirectoryEntry(
                      "LDAP://example..");

        DirectorySearcher searcher;
        SearchResultCollection results;

        searcher = new DirectorySearcher(entry);

        searcher.Filter = "(&(objectClass=user)(displayname=*))";
        searcher.SearchScope = SearchScope.Subtree;

        using (searcher)
        {
            results = searcher.FindAll();

            foreach (SearchResult result in results)
            {
               string searchOK = result.Properties["displayname"][0].ToString();
               objects.Add(searchOK);
            }
        }
        return View();
    }

Just pass the list of users to the View: 只需将用户列表传递给View:

return View(objects);

In your View, declare the type of your model on the top like this: 在您的视图中,在顶部声明模型的类型,如下所示:

@model List<string>

And then you can access the list using the @Model Variable anywhere in your View. 然后,您可以在视图的任何位置使用@Model变量访问列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM