Showing posts with label Active Directory. Show all posts
Showing posts with label Active Directory. Show all posts

Get a user's path within Active Directory using LDAP

Get a user's path within Active Directory

Note: To add or read properties from an Active Directory group you need to have special username and password like an Active Directory admin.

Within Active Directory User could be in OU=Accounts or in OU=Accounts\HR or in OU=Accounts\HR\Recruiters. You can get the USer's path without having to traverse through the entire "Accounts".


string uID = userID.Remove(0, 6).ToUpper();
string folderPath = "";
folderPath = "LDAP://OU=UserAccounts,DC=aaaaa,DC=com";
DirectoryEntry folderEntry;
folderEntry = new DirectoryEntry(folderPath, "Spluser""password");
DirectorySearcher searcher = new DirectorySearcher(folderEntry);
searcher.Filter = string.Format("(&(objectClass=user)(sAMAccountName={0}))"uID);
searcher.SearchScope = SearchScope.Subtree;
SearchResult result = searcher.FindOne();
if (result != null)
{
   searchpath = result.Path;
}
searcher.Dispose(); folderEntry.Dispose();
return searchpath;

you can read here  about the SearcherScope property offered by the DirectorySearcher.

Find all the folders within OU=Accounts in Active Directory and add to an arraylist

Find all the folders within OU=Accounts in Active Directory and add to an ArrayList

Note: To add or read properties from an Active Directory group you need to have special username and password like an Active Directory admin.


string userAcctName = ""
string displayName = ""
string userDept = "";

ArrayList usrAccountsfolders = new ArrayList();

// get all folders of UserAccounts.

string folderPath = ""int membercount = 0; int foldercount = 0;
folderPath = "LDAP://OU=UserAccounts,DC=aaaaaa,DC=com";
DirectoryEntry addGroup;
addGroup = new DirectoryEntry(folderPath, "Spluser""password");
Response.Write("The name is :: " + addGroup.Name.ToString() + "<br/>");

foreach (DirectoryEntry member in addGroup.Children)
{
    if (member.Name.Substring(0, 2) == "CN")
    { membercount += 1; }
    else if (member.Name.Substring(0, 2) == "OU")
    {  foldercount += 1;
       usrAccountsfolders.Add(member);
     }
    //Response.Write(member.Properties["displayName"].Value.ToString() + "<br/>");
}


Get all members in OU=Accounts or OU=Archived Users in Active Directory

Get all members in OU=Accounts or OU=Archived Users in Active Directory


Note: To add or read properties from an Active Directory group you need to have special username and password like an Active Directory admin.


tring folderPath = "";
folderPath = "LDAP://OU=Archived Users,DC=aaaaa,DC=com";
DirectoryEntry addGroup;
addGroup = new DirectoryEntry(folderPath, "Spluser""password");
Response.Write("The name is " + addGroup.Name.ToString() +"<br/>");

foreach (DirectoryEntry member in addGroup.Children)
{
 Response.Write(member.Properties["displayName"].Value.ToString() + "<br/>");
}



Add a user to a given active directory group

Add a user to a given active directory group

Note: To add or read properties from an Active Directory group you need to have special username and password like an Active Directory admin.

DirectoryEntry addGroup;
string folderPath = "LDAP://CN="+grpName+"OU=”+FolderName+”,OU=Groups,DC=aaaaa,DC=com";
addGroup = new DirectoryEntry(folderPath, "Spluser""password");

string usrId = “myUserID”;
string usrPath = "LDAP://CN="+usrId+”,OU=UserAccounts,DC=aaaaa,DC=com";
DirectoryEntry addUser;
addUser = new DirectoryEntry(usrPath);

if((chkGroupExistence(addGroup) == true)&&(chkUserExistence(addGroup, usrId) == false))      
{
  try
    {
      addGroup.Invoke("Add"new object[] { addUser.Path.ToString() });
    }
  catch (Exception)
    {
        LblSuccessMsg.Text = "User could not be added to the Group. ";
    }
 }

Leave your comments below.

Check if a user already exists in Active Directory group

Check if a user already exists in Active Directory group:

Note: To add or read properties from an Active Directory group you need to have special username and password like an Active Directory admin.

DirectoryEntry addGroup;
string folderPath = "LDAP://CN=" + folderName + ",";
folderPath += "OU=SharedFolder,OU=Groups,DC=aaaaa,DC=com";

string usrId = “myUserID”;

addGroup = new DirectoryEntry(folderPath, "Spluser""password");

protected bool chkUserExistence(DirectoryEntry addGroup, string usrId)
    {
        bool usrExists = false;
        object allMembers = addGroup.Invoke("members"null);
        foreach (object member in (IEnumerable)allMembers)
        {
            DirectoryEntry memberEntry = new DirectoryEntry(member);
            if (usrId == memberEntry.Name.ToString())
            {
                usrExists = true;
                break;
            }
        }
        return usrExists;
    }

Leave your comments below.

Check if a group exists in Active directory

Check if a group exists in Active directory

Note: To add or read properties from an Active Directory group you need to have special username and password like an Active Directory admin.

DirectoryEntry addGroup;
string folderPath = "LDAP://CN=" + folderName + ",";
folderPath += "OU=SharedFolder,OU=Groups,DC=aaaaaa,DC=com";

protected Boolean chkGroupExistence(DirectoryEntry addGroup)
    {
        // chk if the requested AD group is available and a proper AD Group object is formed.    

        bool groupExists = true;
        try
        {
            if (addGroup.SchemaClassName == "group")
            { groupExists = true; }
        }
        catch (Exception)
        {
            groupExists = false;
        }
        return groupExists;
    }

Leave your comments below.

Get all members Email,Name of an Active Directory Group using ArrayList: C#.Net


Get members of an Active Directory Group using C#.Net

Note: To add or read properties from an Active Directory group you need to have special username and password like an Active Directory admin.

protected ArrayList GetMembersFromADGroup(string strRole)
    {      
        ArrayList membersEmail = new ArrayList();
        ArrayList membersName = new ArrayList();
        ArrayList strloginName = new ArrayList();
        string AdGroupForSysAdmin = "";        string AdGroupFolder = "";
        char[] charsToTrim = { ' ' };       

        string folderPath = "";
        folderPath = "LDAP://CN=" + AdGroupName;
        folderPath = folderPath.TrimEnd(charsToTrim) + ","; ;
        folderPath += "OU=" + AdFolderName + ",OU=Groups,DC=aaaaaa,DC=com";

// create object for the shared folder in AD using admin username and password. 
        DirectoryEntry addGroup;
        addGroup = new DirectoryEntry(folderPath, "Spluser""password");
        if (chkGroupExistence(addGroup) == true)
        {
           object allMembers = addGroup.Invoke("members"null);
           foreach (object member in (IEnumerable)allMembers)
           {
              DirectoryEntry memberEntry = new DirectoryEntry(member);
              string Email = (memberEntry.Properties["mail"].Value.ToString());
              membersEmail.Add(Email);
              string Name = (memberEntry.Properties["DisplayName"].Value.ToString());
              membersName.Add(SysAdminName);
              string adminID = memberEntry.Properties["Name"].Value.ToString();
              string adminLogin = "PAR-1\\" + adminID.ToLower();
              strloginName.Add(adminLogin);
            } 
       }
        return membersEmail;// Return Name or Email.
}

Leave your comments below.

Get the Sharepoint user's additional information from Active Directory

 Get the Sharepoint user's additional information from Active Directory
Note: To add or read properties from an Active Directory group you need to have special username and password like an Active Directory admin.

 SPUser currentUser = web.CurrentUser;
string userID = currentUser.LoginName;
string newUserID = userID.Remove(0, 6).ToUpper();
// GET THE USER'S info FROM THE ACTIVE DIRECTORY
string usrPath = "LDAP://CN=" + userID.Remove(0, 6).ToUpper();
UsrPath += ",OU=UserAccounts,DC=aaaaa,DC=com";

DirectoryEntry UserInAD;
UserInAD = new DirectoryEntry(usrPath);
string UsrPhone = UserInAD.Properties["telephoneNumber"].Value.ToString();
string UserEmail = UserInAD.Properties["mail"].Value.ToString();
string MgrFromAD = UserInAD.Properties["Manager"].Value.ToString();
string managerLogin = "Company\\" + MgrFromAD.Substring(3, 7).ToLower();

Here we get Manager information from Active Directory of the currently logged in user in sharepoint.

Leave your comments below.