So, we now have created a way to find a DirectoryEntry and use it to instantiate a custom object, exposing the attributes we find important for our application. As I mentioned at the end of Part 2, our Owner doesn't live in a bubble and is a member of a Group (or groups), and has people who report to him. In this post we will create 2 new objects:
-
User - Which is very similar to our Owner object in that it exposes the same information
-
Group - Which although .Net encapsulates an Active Directory group as a DirectoryEntry, a group is not a person.
A group in Active Directory is what it sounds like, a logical grouping of people (or things) that are considered related by the organization. For instance, you may be part of the IT group, or perhaps more specifically part of the New Product Development group which is a member of the IT group. You should be able to see now why finding out what group(s) our owner belongs to could be useful when representing their position in the organization.
So as to re-enforce what we learned in Part 2, lets start by creating the User object.
using System;
using System.DirectoryServices;
namespace ActiveDirectory_Demo1.GlobalObjects.AuthorityObjects
{
/// <summary>
/// Summary description for User.
/// </summary>
public class User : BaseObject
{
public User(DirectoryEntry de, bool IterateBranch)
{
originaldirectoryentry = de;
ID = de.NativeGuid;
Login = de.Properties["sAMAccountName"].Value.ToString();
UserName = de.Name.Replace("CN=","");
ObjectType = de.SchemaClassName.ToLower();
Path = de.Path;
//System.Collections.IDictionaryEnumerator enmr = de.Properties.GetEnumerator();
//while (enmr.MoveNext())
//{
// object o = enmr.Current;
// string x = "";
//}
}
private string username;
private string id;
private string login;
private DirectoryEntry originaldirectoryentry;
private string homepage;
private string objecttype;
private string path;
/// <summary>
/// Name of the user.
/// </summary>
public string UserName
{
get { return username; }
set { username = value; }
}
/// <summary>
/// NativeGuid of the DirectoryEntry
/// </summary>
public string ID
{
get { return id; }
set { id = value; }
}
/// <summary>
/// Login used to authenticate
/// </summary>
public string Login
{
get{ return login; }
set{ login = value; }
}
/// <summary>
/// HomePage
/// </summary>
public string HomePage
{
get
{
if(homepage == null)
{
if(this.originaldirectoryentry.Properties["wWWHomePage"].Value == null)
{homepage = "";}
else
{homepage = this.originaldirectoryentry.Properties["wWWHomePage"].Value.ToString();}
}
return homepage;
}
set { homepage = value;}
}
/// <summary>
/// user
/// </summary>
public string ObjectType
{
get{ return objecttype; }
set{ objecttype = value; }
}
/// <summary>
/// Path from the oringinal DirectoryEntry
/// </summary>
public string Path
{
get{ return path; }
set{ path = value; }
}
This should look very familiar since our Owner object is a person as is this User object. So enough of the review, lets look at the Group object:
using System;
using System.DirectoryServices;
namespace ActiveDirectory_Demo1.GlobalObjects.AuthorityObjects
{
/// <summary>
/// Summary description for Group.
/// </summary>
public class Group : BaseObject
{
public Group(DirectoryEntry de)
{
originaldirectoryentry = de;
ID = de.NativeGuid;
GroupName = de.Name.Replace("CN=","");
ObjectType = de.SchemaClassName.ToLower();
Path = de.Path;
}
private string groupname;
private string id;
private DirectoryEntry originaldirectoryentry;
private string objecttype;
private string path;
/// <summary>
/// Group Name
/// </summary>
public string GroupName
{
get { return groupname; }
set { groupname = value; }
}
/// <summary>
/// NativeGuid of the DirectoryEntry
/// </summary>
public string ID
{
get { return id; }
set { id = value; }
}
/// <summary>
/// group
/// </summary>
public string ObjectType
{
get{ return objecttype; }
set{ objecttype = value; }
}
/// <summary>
/// Path from the oringinal DirectoryEntry
/// </summary>
public string Path
{
get{ return path; }
set{ path = value; }
}
Well, that looks pretty much identical to our User object. It has the same attributes and acquires them in the same fashion. Again, that is because both people and groups are encapsulated in the DirectoryEntry object in .Net. At this point your should feel like this is easy stuff, because it is. While obfuscated behind an unfamiliar object type (DirectoryEntry) once you start examining it, you will find that it is not difficult to coax the data we need concerning our Users and Groups from Active Directory. And while it may not have been obvious at first that the the type of object your DirectoryEntry really is can be found in the SchemaClassName, or that you can get the friendly name each and every time by simply dropping the "CN=" string from the Name attribute, once you do we are not performing brain surgery here.
Next we will do some remedial object oriented programming surrounding the establishment of Collection objects so we can group the objects together and iterate over them like you would an Array.
Be the first to rate this post
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5