About Me

My photo
a Dynamic and Energetic guy.....

Friday, March 6, 2009

Get User belongs Active Groups

public string[] getUserBelongsGroup(string userName)
{
DirectoryEntry objDirEntry = GetUserInforamtionFromAD(userName);

PropertyValueCollection objPVC = objDirEntry.Properties["memberof"];
string[] strData = new string[objPVC.Count];
for (int count = 0; count < objPVC.Count; count++)
{
strData[count] = objPVC[count].ToString().Split(new char[] { ',' })[0].Substring(3);
}
return strData;
}


public DirectoryEntry GetUserInforamtionFromAD(string _UserName)
{
// Create a DirectorySearcher object using the user name as the LDAP search filter. If using a directory other than Exchange, use sAMAccountName instead of mailNickname.
string strActualName = _UserName.Split(new char[] {'\\'})[1].ToString();
DirectorySearcher searcher = new DirectorySearcher("(cn=" + strActualName + ")");

// Search for the specified user.
SearchResult result = searcher.FindOne();


if (result == null)
{
return null;
}
else
{
// Create a DirectoryEntry object to retrieve the collection of attributes (properties) for the user.
DirectoryEntry user = result.GetDirectoryEntry();
return user;
}
}

No comments:

My Masters