如何从一种方法调用和计数变量到另一种方法?

How to call and count variable from one method to another?

您好,我想使用我的其他方法 GetSAM 从方法 valSAM 获取并计算所有无效帐户。

我已经设法使用计数 属性 从 GetSAM 方法中获取数据库中的帐户总数。 (第 7-23 行,GetSAM) 问题是,我不知道如何复制它并从 valSAM 方法中调用/计算无效帐户的总数。 (第 20-39 行,valSAM)

我有一种预感,我必须先以某种方式将无效帐户调用到 GetSAM 方法,然后才能调用它们,但我不知道如何实现它。有人可以就此给我一些建议吗?

GetSAM 方法:

 //Get SAMAccount
    private static string GetSAM(string ldapAddress, string serviceAccountUserName, string serviceAccountPassword)
    {

        string ldapPath = "LDAP://" + ldapAddress;

        string ldapFilter = "(&(objectclass=user)(objectcategory=person))";

        DirectoryEntry directoryEntry = new DirectoryEntry(ldapPath, serviceAccountUserName, serviceAccountPassword);

        string readOutput;

        List<string> list = new List<string>();

        StringBuilder builder = new StringBuilder();

        using (DirectorySearcher directorySearcher = new DirectorySearcher(directoryEntry))
        {
            string samAccountName;

            directorySearcher.Filter = ldapFilter;

            directorySearcher.SearchScope = SearchScope.Subtree;

            directorySearcher.PageSize = 1000;

            using (SearchResultCollection searchResultCollection = directorySearcher.FindAll())
            {

                foreach (SearchResult result in searchResultCollection)
                {
                    samAccountName = result.Properties["sAMAccountName"][0].ToString();

                    valSAM(samAccountName, ldapAddress, serviceAccountUserName, serviceAccountPassword);

                    list.Add(samAccountName);

                }  //end of foreach

                // Count all accounts 
                int totalAccounts = list.Count;

                Console.WriteLine("Found " + totalAccounts + " accounts. Query in " + ldapAddress + " has finished.\n");

                Console.WriteLine("Press [enter] to continue.\n");

                readOutput = Console.ReadLine();

            }//SearchResultCollection will be disposed here
        }
        return readOutput;

    }

valSAM 方法:

//Validate SAMAccount
    private static string valSAM(string samAccountName, string ldapAddress, string serviceAccountUserName, string serviceAccountPassword)
    {
        string ldapPath = "LDAP://" + ldapAddress;

        DirectoryEntry directoryEntry = new DirectoryEntry(ldapPath, serviceAccountUserName, serviceAccountPassword);

        StringBuilder builder = new StringBuilder();


        //create instance fo the directory searcher
        DirectorySearcher desearch = new DirectorySearcher(directoryEntry);

        //set the search filter
        desearch.Filter = "(&(sAMAccountName=" + samAccountName + ")(objectcategory=user))";

        //find the first instance
        SearchResult results = desearch.FindOne();

        using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, ldapAddress))
        {

            //if users are present in database
            if (results != null)
            {

                //Check if account is activated
                bool isAccountActived = IsActive(results.GetDirectoryEntry());

                //Check if account is expired or locked
                bool isAccountLocked = IsAccountLockOrExpired(results.GetDirectoryEntry());

                //account is invalid 
                if ((isAccountActived != true) || (isAccountLocked))
                {
                    builder.Append("User account " + samAccountName + " is invalid. ");

                    if ((isAccountActived != true) && (isAccountLocked))
                    {
                        builder.Append("Account is inactive and locked or expired.").Append('\n'); ;
                    } else if (isAccountActived != true)
                    {
                        builder.Append("Account is inactive.").Append('\n'); ;
                    }
                    else if (isAccountLocked)
                    {
                        builder.Append("Account is locked or has expired.").Append('\n'); ;
                    }
                    else
                    {
                        builder.Append("Unknown reason for status. Contact admin for help.").Append('\n'); ;
                    }

                }

                //account is valid
                if ((isAccountActived) && (isAccountLocked != true))
                {
                    builder.Append("User account " + samAccountName + " is valid.").Append('\n');
                }

            }
            else Console.WriteLine("Nothing found.");

            Console.WriteLine(builder);

        }
        return builder.ToString();
    }

已更新 valSAM:

    //Validate SAMAccount
    private static bool valSAM(string samAccountName, string ldapAddress, string serviceAccountUserName, string serviceAccountPassword)
    {
        string ldapPath = "LDAP://" + ldapAddress;

        DirectoryEntry directoryEntry = new DirectoryEntry(ldapPath, serviceAccountUserName, serviceAccountPassword);

        StringBuilder builder = new StringBuilder();

        bool accountValidation = true;

        //create instance fo the directory searcher
        DirectorySearcher desearch = new DirectorySearcher(directoryEntry);

        //set the search filter
        desearch.Filter = "(&(sAMAccountName=" + samAccountName + ")(objectcategory=user))";

        //find the first instance
        SearchResult results = desearch.FindOne();

        using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, ldapAddress))
        {

            //if users are present in database
            if (results != null)
            {

                //Check if account is activated
                bool isAccountActived = IsActive(results.GetDirectoryEntry());

                //Check if account is expired or locked
                bool isAccountLocked = IsAccountLockOrExpired(results.GetDirectoryEntry());

                accountValidation = ((isAccountActived != true) || (isAccountLocked));

                //account is invalid 
                if (accountValidation)
                {
                    builder.Append("User account " + samAccountName + " is invalid. ");

                    if ((isAccountActived != true) && (isAccountLocked))
                    {
                        builder.Append("Account is inactive and locked or expired.").Append('\n'); ;
                    } else if (isAccountActived != true)
                    {
                        builder.Append("Account is inactive.").Append('\n'); ;
                    }
                    else if (isAccountLocked)
                    {
                        builder.Append("Account is locked or has expired.").Append('\n'); ;
                    }
                    else
                    {
                        builder.Append("Unknown reason for status. Contact admin for help.").Append('\n'); ;
                    }

                    return false;

                }

                //account is valid
                if ((isAccountActived) && (isAccountLocked != true))
                {
                    builder.Append("User account " + samAccountName + " is valid.").Append('\n');

                    return true;
                }

            }
            else Console.WriteLine("Nothing found.");

            Console.WriteLine(builder);

            Console.ReadLine();

        }//end of using
        return accountValidation;
    }

万分感谢 :)

更新:现在更新我的 valSAM 后我遇到了一个新问题 - 当我 return 布尔 accountValidation 而不是 builder.ToString() 时,我无法打印出帐户。

在 Console.WriteLine 之前,您正在 return 拨打电话,请执行以下操作:

private static bool valSAM(string samAccountName, string ldapAddress, string serviceAccountUserName, string serviceAccountPassword)
    {
        string ldapPath = "LDAP://" + ldapAddress;

        DirectoryEntry directoryEntry = new DirectoryEntry(ldapPath, serviceAccountUserName, serviceAccountPassword);

        StringBuilder builder = new StringBuilder();

        bool accountValidation = true;

        //create instance fo the directory searcher
        DirectorySearcher desearch = new DirectorySearcher(directoryEntry);

        //set the search filter
        desearch.Filter = "(&(sAMAccountName=" + samAccountName + ")(objectcategory=user))";

        //find the first instance
        SearchResult results = desearch.FindOne();

        using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, ldapAddress))
        {

            //if users are present in database
            if (results != null)
            {

                //Check if account is activated
                bool isAccountActived = IsActive(results.GetDirectoryEntry());

                //Check if account is expired or locked
                bool isAccountLocked = IsAccountLockOrExpired(results.GetDirectoryEntry());

                accountValidation = ((isAccountActived != true) || (isAccountLocked));

                //account is invalid 
                if (accountValidation)
                {
                    builder.Append("User account " + samAccountName + " is invalid. ");

                    if ((isAccountActived != true) && (isAccountLocked))
                    {
                        builder.Append("Account is inactive and locked or expired.").Append('\n'); ;
                    } else if (isAccountActived != true)
                    {
                        builder.Append("Account is inactive.").Append('\n'); ;
                    }
                    else if (isAccountLocked)
                    {
                        builder.Append("Account is locked or has expired.").Append('\n'); ;
                    }
                    else
                    {
                        builder.Append("Unknown reason for status. Contact admin for help.").Append('\n'); ;
                    }

                    accountValidation = false;

                }

                //account is valid
                if ((isAccountActived) && (isAccountLocked != true))
                {
                    builder.Append("User account " + samAccountName + " is valid.").Append('\n');

                    accountValidation = true;
                }

            }
            else Console.WriteLine("Nothing found.");

            Console.WriteLine(builder);

            Console.ReadLine();

        }//end of using
        return accountValidation;
    }

所以现在,您可以分配值并获得一个 return 点,还可以打印名称。至于在主函数中跟踪计数,您可以在

中放置 valSAM 调用
if(valSAM(samAccountName, ldapAddress, serviceAccountUserName, serviceAccountPassword))
{
  invalidAccountCount++;
}

不用说,您必须在循环外初始化 invalidAccountCount。