Get users from local user groups and active directory groups
Introduction
In this post we will see how to get users from local user groups and from active directory group.
What are the points that are covered
- How to get list of users from local user groups
- How to navigate users in active directory
Get list of users from local user groups
The following method returns arraylist object that contains users in local user group
using System.DirectoryServices; private ArrayList GetUserId(string SystemUserGroup) { string userId = ""; ArrayList userIds = new ArrayList(); string directoryString = "WinNT://./" + SystemUserGroup + ",group"; using (DirectoryEntry groupEntry = new DirectoryEntry(directoryString)) { foreach (object member in (IEnumerable)groupEntry.Invoke("Members")) { using (DirectoryEntry memberEntry = new DirectoryEntry(member)) { string userIdValue = memberEntry.Path; //output will be WinNT://system1/user1 //split with '//' to the user code string[] user = userIdValue.Split(new char[] { '\\' }); userId = user[user.Length - 1]; userIds.Add(userId); } } } return userIds; }
Get list of users from active directory
The following code shows how we can loop active directory
using System.DirectoryServices.AccountManagement; string groupName = "Administrators"; string domainName = ""; PrincipalContext prCtx = new PrincipalContext(ContextType.Domain, domainName); GroupPrincipal grPrincipal = GroupPrincipal.FindByIdentity(prCtx, IdentityType.SamAccountName, groupName); if (prCtx != null) { foreach (Principal prnc in prCtx.GetMembers(false)) { string AccountName = prnc.SamAccountName; string DisplayName = prnc.DisplayName; } prCtx.Dispose(); prCtx.Dispose(); }
Other References
If you are using .NET Framework 2.0 some useful links how to navigate users in the active directory
http://msdn.microsoft.com/en-us/library/ms180879(v=vs.80)
http://www.codeproject.com/Articles/9570/Querying-Microsoft-Active-Directory-Using-Microsof
Conclusion
Any task to access active directory or local user groups, hope this post gives you quick start.
August 2, 2012
В·
Adi В·
No Comments
Tags: Active Directory, UserGroups В· Posted in: C#, Sharepoint 2007, Sharepoint 2010
Leave a Reply