Before .NET, managing Active Directory objects was a bit lengthy and you needed a good knowledge on the principal store to have your head around on what you want to do. We usually use the System.DirectoryServices
namespace, but with .NET 3.5 they introduced System.DirectoryServices.AccountManagement
which manages directory objects independent of the System.DirectoryServices
namespace.
So what are the advantages of using this if I already have a library created for the whole AD methods that System.DirectoryServices
exposed? Because everything is really simple in terms of managing a user, computer or group principal and performing queries on the stores are much faster, thanks to the Fast Concurrent Bind (FSB) feature which caches the connection which decreases the number of ports used in the process.
I remember I had posted a while back Active Directory Objects and C# which is basically everything regarding AD Methods in terms of Users and Group management and if you see the codebase, it is a bit lengthy and you need a bit of understanding on setting and getting hex values which is why I enumerated it. Now I had rewritten it using the System.DirectoryServices.AccountManagement
namespace, functionalities remain the same but it's easier to understand and there are fewer lines.
The code is divided into several regions but here are the 5 key regions with their methods explained:
Validate Methods
ValidateCredentials
– This method will validate the users' credentials
IsUserExpired
– Checks if the User Account has expired
IsUserExisiting
– Checks if user exists on AD
IsAccountLocked
– Checks if user account is locked
Search Methods
GetUser
– This will return a UserPrincipal
Object if the User exists
User Account Methods
SetUserPassword
– This method will set the Users Password
EnableUserAccount
– This method will Enable a User Account
DisableUserAccount
– This method will Disable the User Account
ExpireUserPassword
– This method will Force Expire a Users Password
UnlockUserAccount
– This method will unlock a User Account
CreateNewUser
– This method will create a new User Directory Object
DeleteUser
– This method will delete an AD User based on Username
Group Methods
CreateNewGroup
– This method will create a New Active Directory Group
AddUserToGroup
– This method will add a User to a group
RemoveUserFromGroup
– This method will remove a User from a Group
IsUserGroupMember
– This method will validate whether the User is a Member of a Group
GetUserGroups
– This method will return an ArrayList
of a User Group Memberships
Helper Methods
GetPrincipalContext
– Gets the base principal context
using System;
using System.Collections;
using System.Text;
using System.DirectoryServices.AccountManagement;
using System.Data;
using System.Configuration;
public class ADMethodsAccountManagement
{
#region Variables
private string sDomain = "test.com";
private string sDefaultOU = "OU=Test Users,OU=Test,DC=test,DC=com";
private string sDefaultRootOU = "DC=test,DC=com";
private string sServiceUser = @"ServiceUser";
private string sServicePassword = "ServicePassword";
#endregion
#region Validate Methods
public bool ValidateCredentials(string sUserName, string sPassword)
{
PrincipalContext oPrincipalContext = GetPrincipalContext();
return oPrincipalContext.ValidateCredentials(sUserName, sPassword);
}
public bool IsUserExpired(string sUserName)
{
UserPrincipal oUserPrincipal = GetUser(sUserName);
if (oUserPrincipal.AccountExpirationDate != null)
{
return false;
}
else
{
return true;
}
}
public bool IsUserExisiting(string sUserName)
{
if (GetUser(sUserName) == null)
{
return false;
}
else
{
return true;
}
}
public bool IsAccountLocked(string sUserName)
{
UserPrincipal oUserPrincipal = GetUser(sUserName);
return oUserPrincipal.IsAccountLockedOut();
}
#endregion
#region Search Methods
public UserPrincipal GetUser(string sUserName)
{
PrincipalContext oPrincipalContext = GetPrincipalContext();
UserPrincipal oUserPrincipal =
UserPrincipal.FindByIdentity(oPrincipalContext, sUserName);
return oUserPrincipal;
}
public GroupPrincipal GetGroup(string sGroupName)
{
PrincipalContext oPrincipalContext = GetPrincipalContext();
GroupPrincipal oGroupPrincipal =
GroupPrincipal.FindByIdentity(oPrincipalContext, sGroupName);
return oGroupPrincipal;
}
#endregion
#region User Account Methods
public void SetUserPassword(string sUserName, string sNewPassword, out string sMessage)
{
try
{
UserPrincipal oUserPrincipal = GetUser(sUserName);
oUserPrincipal.SetPassword(sNewPassword);
sMessage = "";
}
catch (Exception ex)
{
sMessage = ex.Message;
}
}
public void EnableUserAccount(string sUserName)
{
UserPrincipal oUserPrincipal = GetUser(sUserName);
oUserPrincipal.Enabled = true;
oUserPrincipal.Save();
}
public void DisableUserAccount(string sUserName)
{
UserPrincipal oUserPrincipal = GetUser(sUserName);
oUserPrincipal.Enabled = false;
oUserPrincipal.Save();
}
public void ExpireUserPassword(string sUserName)
{
UserPrincipal oUserPrincipal = GetUser(sUserName);
oUserPrincipal.ExpirePasswordNow();
oUserPrincipal.Save();
}
public void UnlockUserAccount(string sUserName)
{
UserPrincipal oUserPrincipal = GetUser(sUserName);
oUserPrincipal.UnlockAccount();
oUserPrincipal.Save();
}
public UserPrincipal CreateNewUser(string sOU,
string sUserName, string sPassword, string sGivenName, string sSurname)
{
if (!IsUserExisiting(sUserName))
{
PrincipalContext oPrincipalContext = GetPrincipalContext(sOU);
UserPrincipal oUserPrincipal = new UserPrincipal
(oPrincipalContext, sUserName, sPassword, true );
oUserPrincipal.UserPrincipalName = sUserName;
oUserPrincipal.GivenName = sGivenName;
oUserPrincipal.Surname = sSurname;
oUserPrincipal.Save();
return oUserPrincipal;
}
else
{
return GetUser(sUserName);
}
}
public bool DeleteUser(string sUserName)
{
try
{
UserPrincipal oUserPrincipal = GetUser(sUserName);
oUserPrincipal.Delete();
return true;
}
catch
{
return false;
}
}
#endregion
#region Group Methods
public GroupPrincipal CreateNewGroup(string sOU, string sGroupName,
string sDescription, GroupScope oGroupScope, bool bSecurityGroup)
{
PrincipalContext oPrincipalContext = GetPrincipalContext(sOU);
GroupPrincipal oGroupPrincipal = new GroupPrincipal(oPrincipalContext, sGroupName);
oGroupPrincipal.Description = sDescription;
oGroupPrincipal.GroupScope = oGroupScope;
oGroupPrincipal.IsSecurityGroup = bSecurityGroup;
oGroupPrincipal.Save();
return oGroupPrincipal;
}
public bool AddUserToGroup(string sUserName, string sGroupName)
{
try
{
UserPrincipal oUserPrincipal = GetUser(sUserName);
GroupPrincipal oGroupPrincipal = GetGroup(sGroupName);
if (oUserPrincipal == null || oGroupPrincipal == null)
{
if (!IsUserGroupMember(sUserName, sGroupName))
{
oGroupPrincipal.Members.Add(oUserPrincipal);
oGroupPrincipal.Save();
}
}
return true;
}
catch
{
return false;
}
}
public bool RemoveUserFromGroup(string sUserName, string sGroupName)
{
try
{
UserPrincipal oUserPrincipal = GetUser(sUserName);
GroupPrincipal oGroupPrincipal = GetGroup(sGroupName);
if (oUserPrincipal == null || oGroupPrincipal == null)
{
if (IsUserGroupMember(sUserName, sGroupName))
{
oGroupPrincipal.Members.Remove(oUserPrincipal);
oGroupPrincipal.Save();
}
}
return true;
}
catch
{
return false;
}
}
public bool IsUserGroupMember(string sUserName, string sGroupName)
{
UserPrincipal oUserPrincipal = GetUser(sUserName);
GroupPrincipal oGroupPrincipal = GetGroup(sGroupName);
if (oUserPrincipal == null || oGroupPrincipal == null)
{
return oGroupPrincipal.Members.Contains(oUserPrincipal);
}
else
{
return false;
}
}
public ArrayList GetUserGroups(string sUserName)
{
ArrayList myItems = new ArrayList();
UserPrincipal oUserPrincipal = GetUser(sUserName);
PrincipalSearchResult<Principal> oPrincipalSearchResult = oUserPrincipal.GetGroups();
foreach (Principal oResult in oPrincipalSearchResult)
{
myItems.Add(oResult.Name);
}
return myItems;
}
public ArrayList GetUserAuthorizationGroups(string sUserName)
{
ArrayList myItems = new ArrayList();
UserPrincipal oUserPrincipal = GetUser(sUserName);
PrincipalSearchResult<Principal> oPrincipalSearchResult =
oUserPrincipal.GetAuthorizationGroups();
foreach (Principal oResult in oPrincipalSearchResult)
{
myItems.Add(oResult.Name);
}
return myItems;
}
#endregion
#region Helper Methods
public PrincipalContext GetPrincipalContext()
{
PrincipalContext oPrincipalContext = new PrincipalContext
(ContextType.Domain, sDomain, sDefaultOU, ContextOptions.SimpleBind,
sServiceUser, sServicePassword);
return oPrincipalContext;
}
public PrincipalContext GetPrincipalContext(string sOU)
{
PrincipalContext oPrincipalContext =
new PrincipalContext(ContextType.Domain, sDomain, sOU,
ContextOptions.SimpleBind, sServiceUser, sServicePassword);
return oPrincipalContext;
}
#endregion
}
Now this is how to use it.
ADMethodsAccountManagement ADMethods = new ADMethodsAccountManagement();
UserPrincipal myUser = ADMethods.GetUser(Test");
myUser.GivenName = "Given Name";
myUser.Surname = "Surname";
myUser.MiddleName = "Middle Name";
myUser.EmailAddress = "Email Address";
myUser.EmployeeId = "Employee ID";
myUser.Save();