Deleting User From Active Directory c#.net

Posted: November 7, 2014 in Uncategorized
Introduction 
I have written an application for deleting accounts from the Active Directory wrapper.
  1. public static string DelUserInAD(string sGroupName, string strDomain, string strName)
  2.         {
  3.             bool done = false;
  4.             string result = string.Empty;
  5.             try
  6.             {
  7.                 PrincipalContext oPrincipalContext = GetPrincipalContext();
  8.                 string sUserName = strDomain + “\\” + strName;
  9.                 UserPrincipal oUserPrincipal = UserPrincipal.FindByIdentity(oPrincipalContext, sUserName);
  10.                 GroupPrincipal oGroupPrincipal = GetGroup(sGroupName);
  11.                 if (oUserPrincipal != null && oGroupPrincipal != null)
  12.                 {
  13.                     if (IsUserGroupMember(sUserName, sGroupName))
  14.                    {
  15.                         //oGroupPrincipal.Members.Add(oUserPrincipal);
  16.                         //oGroupPrincipal.Save();
  17.                         oGroupPrincipal.Members.Remove(oUserPrincipal);
  18.                         oGroupPrincipal.Save();
  19.                         done = !(oUserPrincipal.IsMemberOf(oGroupPrincipal));
  20.                     }
  21.                     else
  22.                     {
  23.                        result = sUserName + ” already exists. Exiting!!”;
  24.                     }
  25.                 }
  26.             }
  27.             catch
  28.             {
  29.             }
  30.             return result;
  31.         }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s