title: Developing a user search page in asp.net MVC 4 using Activedirectory
body: <p>I am developing a ASp.net MVC 4 Intranet application.
  So when I login I can see my account getting displayed.
  Now to authorise the account I have developed a custom authorization provider by following the below link :<a href="http://www.codeproject.com/Articles/607392/Custom-Role-Providers" rel="nofollow">http://www.codeproject.com/Articles/607392/Custom-Role-Providers</a></p>
  
  <p>now my problem is after the initial authorization I add myself to superadmin role who has acess to Index.cstml where there is a text box and a search button.</p>
  
  <p>basically here on this page I want to <strong>search user from active directory</strong> and display it so that I can assign some roles to the user.
  How do I do that?
  please provide the code how to connect to Active directory, where to provide the connection string for LDAP and the excat method which will help me in searching the user from active directory and dispaly it on the index view.</p>
  
  <p>Below is what I have tried:
  Entities: ADuser->property UserLoginName ,UserDispalyName
  Model: has a method that returns the serch user from AD and add it to entity
  public class ADUserModel
      {</p>
  
  <pre><code>    public List&lt;ADUser&gt; GetUserFromAD(string name) //Get Network Users (AD)
      {
          //ArrayList searchUser = new ArrayList();
          var domainContext = new PrincipalContext(ContextType.Domain);
          var groupPrincipal = GroupPrincipal.FindByIdentity(domainContext, IdentityType.SamAccountName, "Domain Users");
          UserPrincipal user = new UserPrincipal(domainContext);
          user.Enabled = true;
          user.Name = name + "*";
  
          PrincipalSearcher pS = new PrincipalSearcher();
          pS.QueryFilter = user;
  
          PrincipalSearchResult&lt;Principal&gt; results = pS.FindAll();
          var list = new List&lt;ADUser&gt;();
  
  
          //searchUser.Add(results);
          foreach (var item in results)
          {
              var users = new ADUser();
              users.UserLoginName = item.SamAccountName;
              users.UserDisplayName = item.DisplayName;
              list.Add(users);
              //searchUser.Add(users);
          }
  
          return list;
  
      }
  }
  </code></pre>
  
  <p>Controller: has an action method that will return the view</p>
  
  <p>public class ManageUsersController : Controller
      {
          //
          // GET: /ManageUsers/</p>
  
  <pre><code>    public ActionResult Search(string searchString)
      {
          ADUserModel model = new ADUserModel();
          var list = new List&lt;ADUser&gt;();
          if (searchString != null)
          {
              list = model.GetUserFromAD(searchString);
              return View("Search");
          }
          else
          {
              return View("Search");
          }
      }
  }
  </code></pre>
  
  <p>View: Search.cshtml</p>
  
  <pre><code> @model IEnumerable&lt;FMSKendoUI.Entities.ADUser&gt;
  @{
     ViewBag.Title = "Search";
    }
  @Html.BeginForm("Search","ManageUsers",FormMethod.Get)
  {
  @Html.TextBox("Search") &lt;input type="submit" value="Search" /&gt;
  }
  
  @if (Model.Count() != 0)
  {
  
      @foreach (FMSKendoUI.Entities.ADUser model in Model)
      {
  
              @Html.DisplayFor(m =&gt; model.UserLoginName)
              @Html.DisplayFor(m =&gt; model.UserDisplayName)
  
      }
  
  
  }
  </code></pre>
  
  <p>But my problem is:
  During my first page load I would need only the textbox and search button.
  and on the search button click I need to call the controller search method.</p>
  
  <p>Since during the first time my entity is null it is giving me null exception.
  my entity get loaded only after I provide the search string and call the model  method.</p>
  
  <p>I am unable to achieve this. Please help me on this.</p>