【问题标题】:How to call and count variable from one method to another?如何从一种方法调用和计算变量到另一种方法?
【发布时间】:2015-04-01 03:40:56
【问题描述】:

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

我已经设法使用 count 属性从 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 后遇到了一个新问题 - 当我返回布尔值 accountValidation 而不是 builder.ToString() 时,我无法打印出帐户。

【问题讨论】:

  • 当您调用valSAM 时,您应该返回一个布尔值而不是字符串,并在此基础上声明一个变量,如果返回的值为假(即帐户无效),该变量应该递增。
  • 没关系,解决了。感谢您的帮助:)

标签: c# visual-studio-2010 count call


【解决方案1】:

您在执行 Console.WriteLine 之前返回调用,请执行以下操作:

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;
    }

所以现在,您可以分配值并拥有一个返回点,还可以打印名称。至于在 main 函数中跟踪计数,您可以将 valSAM 调用放在

if(valSAM(samAccountName, ldapAddress, serviceAccountUserName, serviceAccountPassword))
{
  invalidAccountCount++;
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-18
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-29
    • 2014-03-02
    相关资源
    最近更新 更多