المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Get list of groups for a given user using ActiveDirectory



C# Programming
03-29-2010, 07:50 PM
Hi

I am getting the list of groups for a particular user using ActiveDirectory.
I am using the following code.

void GetUserDetailsFromAD()
{
string _userName = Environment.UserName;
string _userDomain = Environment.UserDomainName;

//Use given URL
using (DirectoryEntry de = new DirectoryEntry(string.Format("LDAP://URL /OU=XYZ,DC={0},DC=XYZ,DC=XYZ,DC=com",_userDomain)))
{
//Create a text file to write details
TextWriter tw = new StreamWriter("C:\\LDAP.txt");

de.AuthenticationType = AuthenticationTypes.Secure |AuthenticationTypes.ReadonlyServer;
using (DirectorySearcher search = new DirectorySearcher(de))
{
search.Filter = "(sAMAccountName=" + _userName + ")";
search.PropertiesToLoad.Add("displayName");
search.PropertiesToLoad.Add("memberof");
SearchResult result = search.FindOne();


if (result != null)
{
_displayName = result.Properties["displayname"][0].ToString();

//Loop through all the groups that user is part of
foreach (object item in result.Properties["memberof"])
{

//Write the groups to a text file
tw.WriteLine(item.ToString());

//Check whether the group in which user is present starts with ABC
if (item.ToString().StartsWith("CN=ABC,DC=DEF"))
{
//user is member of ABC group
}
else {
//user is not member
}
}
}
}

tw.Close();


}
}
When i run these program from my local machine(let's say machine A) ,the list of groups which _userName belongs to is populated in C:\\LDAP.txt file.However if i run this from another machine (machine B) in same network,only some groups are displayed ,not all.Eg:If machine A lists group 1,2,3,4 ,machine B lists only 1,2.

1)How can i know whether there are any restrictions in place on other system?(due to which all groups are not displayed)
2)How to check whether there any changes in network settings in the second machine with respect to Active Directory?

Please suggest.
"Every morning I go through Forbes list of 40 richest people in the world. If my name is not in there, I go to work..!!!"