I'm trying to get all users that belong to a group in the Active Directory. I want to display the result as a drop down for the site users to select. This is what I have so far:
public IEnumerable<KeyValuePair<string, string>> GetAllMembers(string group)
{
var domainContext = new PrincipalContext(ContextType.Domain);
var group = GroupPrincipal.FindByIdentity(domainContext, group);
return from principal in @group.Members select new KeyValuePair<string, string>(/* need to fill in key and value */);
}
The problem I have is that I am developing this outside of an active directory so cannot really test it yet (long story don't ask). I want to maximise my chances of success when deploying this into a testing environment.
My question is: if I want the key value pair to contain login username (key, ex: "DOMAIN\darkoz"
) and the users real name (value, ex: "Darko Z"
), how do I get those? the Principal
object has at least 5 properties with the word Name
in it so I'm not sure which is which.
Bonus question: is this a generally accepted way of achieving my goal? I realize it is a very simple problem but with my lack of knowledge of Active Directory, I wouldn't be surprised if there was a better way of doing this. Will this work running on an account that is not admin?