Ejemplo n.º 1
0
        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.");
        }