c# - How to call and count variable from one method to another? -


hi , count invalid accounts method valsam using other method getsam.

i've managed use count property total number of accounts in database getsam method. (lines 7- 23, getsam) problem is, not know how replicate , call/ count total number of invalid accounts valsam method. (lines 20- 39, valsam)

i have hunch have somehow call invalid accounts getsam method before able call them not know how implement it. can please advise me on this?

getsam method:

 //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 accounts                  int totalaccounts = list.count;                  console.writeline("found " + totalaccounts + " accounts. query in " + ldapaddress + " has finished.\n");                  console.writeline("press [enter] continue.\n");                  readoutput = console.readline();              }//searchresultcollection disposed here         }         return readoutput;      } 

valsam method:

//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 directory searcher         directorysearcher desearch = new directorysearcher(directoryentry);          //set search filter         desearch.filter = "(&(samaccountname=" + samaccountname + ")(objectcategory=user))";          //find first instance         searchresult results = desearch.findone();          using (principalcontext pc = new principalcontext(contexttype.domain, ldapaddress))         {              //if users present in database             if (results != null)             {                  //check if account activated                 bool isaccountactived = isactive(results.getdirectoryentry());                  //check if account expired or locked                 bool isaccountlocked = isaccountlockorexpired(results.getdirectoryentry());                  //account invalid                  if ((isaccountactived != true) || (isaccountlocked))                 {                     builder.append("user account " + samaccountname + " invalid. ");                      if ((isaccountactived != true) && (isaccountlocked))                     {                         builder.append("account inactive , locked or expired.").append('\n'); ;                     } else if (isaccountactived != true)                     {                         builder.append("account inactive.").append('\n'); ;                     }                     else if (isaccountlocked)                     {                         builder.append("account locked or has expired.").append('\n'); ;                     }                     else                     {                         builder.append("unknown reason status. contact admin help.").append('\n'); ;                     }                  }                  //account valid                 if ((isaccountactived) && (isaccountlocked != true))                 {                     builder.append("user account " + samaccountname + " valid.").append('\n');                 }              }             else console.writeline("nothing found.");              console.writeline(builder);          }         return builder.tostring();     } 

updated 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 directory searcher         directorysearcher desearch = new directorysearcher(directoryentry);          //set search filter         desearch.filter = "(&(samaccountname=" + samaccountname + ")(objectcategory=user))";          //find first instance         searchresult results = desearch.findone();          using (principalcontext pc = new principalcontext(contexttype.domain, ldapaddress))         {              //if users present in database             if (results != null)             {                  //check if account activated                 bool isaccountactived = isactive(results.getdirectoryentry());                  //check if account expired or locked                 bool isaccountlocked = isaccountlockorexpired(results.getdirectoryentry());                  accountvalidation = ((isaccountactived != true) || (isaccountlocked));                  //account invalid                  if (accountvalidation)                 {                     builder.append("user account " + samaccountname + " invalid. ");                      if ((isaccountactived != true) && (isaccountlocked))                     {                         builder.append("account inactive , locked or expired.").append('\n'); ;                     } else if (isaccountactived != true)                     {                         builder.append("account inactive.").append('\n'); ;                     }                     else if (isaccountlocked)                     {                         builder.append("account locked or has expired.").append('\n'); ;                     }                     else                     {                         builder.append("unknown reason status. contact admin help.").append('\n'); ;                     }                      return false;                  }                  //account valid                 if ((isaccountactived) && (isaccountlocked != true))                 {                     builder.append("user account " + samaccountname + " valid.").append('\n');                      return true;                 }              }             else console.writeline("nothing found.");              console.writeline(builder);              console.readline();          }//end of using         return accountvalidation;     } 

thanks million :)

update: have new problem after updating valsam- unable print out accounts when return boolean accountvalidation instead of builder.tostring().

you returning call before console.writeline, this:

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 directory searcher         directorysearcher desearch = new directorysearcher(directoryentry);          //set search filter         desearch.filter = "(&(samaccountname=" + samaccountname + ")(objectcategory=user))";          //find first instance         searchresult results = desearch.findone();          using (principalcontext pc = new principalcontext(contexttype.domain, ldapaddress))         {              //if users present in database             if (results != null)             {                  //check if account activated                 bool isaccountactived = isactive(results.getdirectoryentry());                  //check if account expired or locked                 bool isaccountlocked = isaccountlockorexpired(results.getdirectoryentry());                  accountvalidation = ((isaccountactived != true) || (isaccountlocked));                  //account invalid                  if (accountvalidation)                 {                     builder.append("user account " + samaccountname + " invalid. ");                      if ((isaccountactived != true) && (isaccountlocked))                     {                         builder.append("account inactive , locked or expired.").append('\n'); ;                     } else if (isaccountactived != true)                     {                         builder.append("account inactive.").append('\n'); ;                     }                     else if (isaccountlocked)                     {                         builder.append("account locked or has expired.").append('\n'); ;                     }                     else                     {                         builder.append("unknown reason status. contact admin help.").append('\n'); ;                     }                      accountvalidation = false;                  }                  //account valid                 if ((isaccountactived) && (isaccountlocked != true))                 {                     builder.append("user account " + samaccountname + " valid.").append('\n');                      accountvalidation = true;                 }              }             else console.writeline("nothing found.");              console.writeline(builder);              console.readline();          }//end of using         return accountvalidation;     } 

so now, can assign value , have 1 return point , can print names. keeping track of counts in main function can place valsam call in

if(valsam(samaccountname, ldapaddress, serviceaccountusername, serviceaccountpassword)) {   invalidaccountcount++; } 

and needless say, have initialize invalidaccountcount outside loop.


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -