public static void PrintGroup(GroupPrincipal group, bool showMembers = false, bool showGroups = false) { Console.WriteLine("Name: " + group.Name); Console.WriteLine("DistinguishedName: " + group.DistinguishedName); Console.WriteLine("DisplayName: " + group.DisplayName); Console.WriteLine("SamAccountName: " + group.SamAccountName); Console.WriteLine("UserPrincipalName: " + group.UserPrincipalName); Console.WriteLine("Description: " + group.Description); Console.WriteLine("IsSecurityGroup: " + group.IsSecurityGroup); //Console.WriteLine(": " + group.GroupScope.); Console.WriteLine("Guid: " + group.Guid); Console.WriteLine("Sid: " + group.Sid); if (showMembers) { Console.WriteLine(); Console.WriteLine("Members:"); if (group.Members.Count > 0) { foreach (var user in group.Members.OrderBy(x => x.Name)) { if (user is UserPrincipal) { Print.PrintUser(user as UserPrincipal, ""); } if (user is GroupPrincipal) { Print.PrintGroup(group as GroupPrincipal); } } } else { Console.WriteLine("\tNo Members"); } } if (showGroups) { Console.WriteLine(); Console.WriteLine("Member Of:"); var groups = group.GetGroups().OrderBy(x => x.Name); if (groups.Count() > 0) { foreach (GroupPrincipal g in groups) { Print.PrintGroup(g, false, false); } } else { Console.WriteLine("\tNo Membership"); } } Console.WriteLine("==================================="); }
private static bool GetUser(PrincipalContext pc, string value, IdentityType?idType = null) { try { UserPrincipal user = null; if (idType.HasValue) { if (idType == IdentityType.UserPrincipalName) { //Add the domain url value += "@users.something.com"; } user = UserPrincipal.FindByIdentity(pc, idType.Value, value); } else { user = UserPrincipal.FindByIdentity(pc, value); } if (user == null) { Console.WriteLine($"Unable to find user with [{idType.ToString()}] [{value}]"); return(false); } else { Print.PrintUser(user, $"{idType.ToString()}: {value}", true); return(true); } } catch (Exception ex) { Console.WriteLine($"### Error TestUser [{idType.ToString()}]:[{value}]"); while (ex != null) { Console.WriteLine(ex.Message); ex = ex.InnerException; } return(false); } }
public void Run(string[] args) { Console.Clear(); Console.WriteLine("*** Test AD User ***"); try { if (args == null || args.Length == 0) { Print.PrintUser(UserPrincipal.Current, "CURRENT USER INFO"); Console.WriteLine("TestADUser completed. Press enter to close application."); Console.ReadLine(); } else { // Replace this with the domain that you want to test string DomainName = "DomainName"; using (var pc = new PrincipalContext(ContextType.Domain, DomainName, null, ContextOptions.Negotiate)) { Console.WriteLine($"ConnectedServer : [{pc.ConnectedServer}]"); Console.WriteLine($"Container : [{pc.Container}]"); Console.WriteLine($"pc.Name : [{pc.Name}]"); Console.WriteLine($"pc.UserName : [{pc.UserName}]"); // Get the UserId and insert it here to test Guid userId = Guid.Empty; GetUser(pc, userId.ToString(), IdentityType.Guid); //Username GetUser(pc, args[0], IdentityType.Name); //UserPrincipalName if (!GetUser(pc, args[0], IdentityType.UserPrincipalName)) { GetUser(pc, args[0]); } //A list of roles that you want to test var roles = new List <string> { "Administrator", "PowerUser", "User" }; //List of usernames that needs to be tested var servaccounts = new List <string> { "User1", "User2", "User3" }; var roleAndusers = new Dictionary <string, List <string> >(); //Group + Users. Test is the users are in the groups roleAndusers.Add("Admins", new List <string> { "Administrator" }); roleAndusers.Add("Super Users", new List <string> { "User4", "User5", "MarketingPerson" }); roleAndusers.Add("Blocked", new List <string> { }); roleAndusers.Add("IT", new List <string> { "TriedRebooting", "Format" }); if (roleAndusers.Count != roles.Count) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"The number of roles and the number of items in the dictionary do not match. Testing may miss problems"); } } } } catch (Exception ex) { Console.WriteLine("### An error occurred ###"); while (ex != null) { Console.WriteLine(ex.Message); ex = ex.InnerException; } Console.ReadLine(); } Console.WriteLine("TestADUser completed."); }