John John
John John

Reputation: 1

How to return user details from the Active Directory using PrincipalContext

I want to create a new action method that when called it will return all the Active Directory usernames. The asp.net mvc application and Active Directory are within the same domain (and currently inside the same development machine).

So I have defined the following action method:-

public ViewResult Details()
        {
 var c = repository.GetUserDetails3();
return View("Details2",c); }

and the following repository method:-

public DirectoryEntry GetUserDetails3()
{   
     DirectoryEntry de = new DirectoryEntry();

     using (var context = new PrincipalContext(ContextType.Domain, "WIN-SPDEV.com"))
     {
         using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
         {
             foreach (var result in searcher.FindAll())
             {
                 de = result.GetUnderlyingObject() as DirectoryEntry; 
             }
         }
     }
     return de; 
}

and the following model class:-

public class DirectoryUser
    {public Nullable<Guid> Guid { get; set; }
        public string Name { get; set; }
        public string Username { get; set; }
}}

and on the view I have:-

@model IEnumerable<TMS.Models.DirectoryUser>

@{
    ViewBag.Title = "Details2";
}

<h2>Details2</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Guid)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Username)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Guid)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Username)
        </td>

but when I call the action method I got the following error:-

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type

The model item passed into the dictionary is of type 'System.DirectoryServices.DirectoryEntry', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[TMS.Models.DirectoryUser]'.

Upvotes: 0

Views: 3089

Answers (1)

marc_s
marc_s

Reputation: 755491

I don't understand why you are mixing the new PrincipalContext with the old DirectoryEntry stuff. Doesn't make any sense.....

Also - you're searching for all users, but in the end, you're returning only a single DirectoryEntry - why?!?

If you're using the new PrincipalContext - then use the UserPrincipal - it contains nice and easy to use properties about the user - much easier to use and work with than the old DirectoryEntry stuf....

public List<UserPrincipal> GetAllUsersDetails()
{   
    using (var context = new PrincipalContext(ContextType.Domain, "WIN-SPDEV.com"))
    using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
    {
       var searchResults = searcher.FindAll();

       List<UserPrincipal> results = new List<UserPrincipal>();

       foreach(Principal p in searchResults)
       {
           results.Add(p as UserPrincipal);
       }
    }
}

The UserPrincipal class has really nice properties like GivenName (first name), Surname and so forth - easy to use, strongly-typed properties. Use them!

Read all about those new classes and how to use them here:

Upvotes: 2

Related Questions