Zum Inhalt springen

C# Rekursion zu tief? (StackOverflowException)


Empfohlene Beiträge

Geschrieben

Hi zusammen,

um ein kleines AD Problem zu Lösen habe ich mir ein kleines Programm in C# gebaut.

Das Problem ist das ich beim Attribut MemberOF eines Users nur die Gruppen angezeigt bekomme denen der USer direkt zu geordnet ist. Da ich aber gerne auf seine Zuordnung durch Gruppen in Gruppen wissen möchte habe ich mir wie bereits erwähnt eine kleine Rekursion gebaut.

Diese nimmt eine Gruppe und sucht alle der Gruppe zugeordneten Gruppen und User. Jetzt zum eigentlichen Problem dieser Code:

	public class Group

	{

		public ArrayList Member;

		private string GroupName;

		private System.DirectoryServices.DirectoryEntry entryPC;

		private static NameTranslate nto;

		public Group (string name, System.DirectoryServices.DirectoryEntry entrypc)

		{

			this.GroupName=name;

			this.entryPC=entrypc;

			this.Member= new ArrayList();

			this.Member.AddRange(this.searchGroup(this.entryPC.Path, this.GroupName));

		}

		private ArrayList searchGroup(string LDAPPath, string GroupName)

		{

			ArrayList grouplst=new ArrayList();


			System.DirectoryServices.DirectorySearcher Searcher = new System.DirectoryServices.DirectorySearcher( LDAPPath  ); 

			if ( nto == null ) 

			{ 

				nto = new NameTranslate(); 

				nto.Init( System.Convert.ToInt32( ActiveDs.ADS_NAME_INITTYPE_ENUM.ADS_NAME_INITTYPE_GC ), "" ); 

			}

			Searcher.Filter = ( "(&(anr=" + GroupName+ ")(objectclass=group))" ); 


			Searcher.PropertiesToLoad.Add( "member" );

			Searcher.PageSize=int.MaxValue;

			Searcher.SizeLimit=int.MaxValue;




			SearchResultCollection SRColl = null; 

			try 

			{

				try

				{

					SRColl = Searcher.FindAll(); 

				}

				catch(Exception x)

				{

					Console.WriteLine(x.Message);

				}

				foreach(SearchResult resEnt in SRColl)

				{

					ResultPropertyCollection propcoll=resEnt.Properties;

					foreach(string key in propcoll.PropertyNames)

					{

						foreach(object values in propcoll[key])

						{

							grouplst.Add(values.ToString());


							string CN = values.ToString().Substring(values.ToString().IndexOf("CN=")+3,(values.ToString().Length-((values.ToString().IndexOf("CN=")+3)+( values.ToString().Length-values.ToString().IndexOf(",OU")))));

							if (!GroupName.ToUpper().Equals(CN.ToUpper()))

							{

								if (!(values.ToString().ToUpper().IndexOf("OU=BENUTZER")>=0))

								{

									grouplst.AddRange(searchGroup(LDAPPath,CN));

								}

							}

						}


					}

				}

			}

			catch(Exception e)

			{

				Console.WriteLine(e.Message);

			}

			return  grouplst;

		}

	}

erzeugt mir immer eine StackOverflowException.

Leider gibt es auch keine möglichkeit diese mit try catch abzufangen.

Hat jemand eine idee wie ich entweder mehr rekursionen hin bekomme oder eine alternative zur rekursion?

Schon mal Danke im Vorraus.

Geschrieben

Hi nochmal sorry kann geschlossen werden.

hab die lösung selbst gefunden

hier der Code falls jemand mal sowas Braucht:

public class Group

	{

		public ArrayList Member;

		private string GroupName;

		private System.DirectoryServices.DirectoryEntry entryPC;

		private static NameTranslate nto;

		public Group (string name, System.DirectoryServices.DirectoryEntry entrypc)

		{

			this.GroupName=name;

			this.entryPC=entrypc;

			this.Member= new ArrayList();

			//this.Member.AddRange(this.searchGroup(this.entryPC.Path, this.GroupName));

			this.searchGroup(this.entryPC.Path, this.GroupName);

		}

		private void searchGroup(string LDAPPath, string GroupName)

		{


			ArrayList grouplst=new ArrayList();

			grouplst.Add(GroupName);

			for (int i = 0;i<grouplst.Count;i++)

			{


				System.DirectoryServices.DirectorySearcher Searcher = new System.DirectoryServices.DirectorySearcher( LDAPPath  ); 

				if ( nto == null ) 

				{ 

					nto = new NameTranslate(); 

					nto.Init( System.Convert.ToInt32( ActiveDs.ADS_NAME_INITTYPE_ENUM.ADS_NAME_INITTYPE_GC ), "" ); 

				}

				Searcher.Filter = "(anr=" + grouplst[i].ToString()+ ")"; 


				Searcher.PropertiesToLoad.Add( "member" );

				Searcher.PageSize=int.MaxValue;

				Searcher.SizeLimit=int.MaxValue;




				SearchResultCollection SRColl = null; 

				try 

				{

					try

					{

						SRColl = Searcher.FindAll(); 

					}

					catch(Exception x)

					{

						Console.WriteLine(x.Message);

					}

					foreach(SearchResult resEnt in SRColl)

					{

						ResultPropertyCollection propcoll=resEnt.Properties;

						foreach(string key in propcoll.PropertyNames)

						{

							foreach(object values in propcoll[key])

							{

								string CN = values.ToString().Substring(values.ToString().IndexOf("CN=")+3,(values.ToString().Length-((values.ToString().IndexOf("CN=")+3)+( values.ToString().Length-values.ToString().IndexOf(",OU")))));

								if (!GroupName.ToUpper().Equals(CN.ToUpper()))

								{

									if (!(values.ToString().ToUpper().IndexOf("OU=BENUTZER")>=0))

									{

										bool exist=false;

										for (int x=0;x<grouplst.Count;x++)

										{

											if (grouplst[x].ToString().ToUpper().Equals(CN.ToUpper()))

											{

												exist=true;

												x= grouplst.Count;

											}

										}

										if (!exist)

										{

											grouplst.Add(CN);

										}

									}

									else

									{

										this.Member.Add(values.ToString());

									}

								}

							}


						}

					}

				}

				catch(Exception e)

				{

					Console.WriteLine(e.Message);

				}

			}

		}

}

Erstelle ein Benutzerkonto oder melde Dich an, um zu kommentieren

Du musst ein Benutzerkonto haben, um einen Kommentar verfassen zu können

Benutzerkonto erstellen

Neues Benutzerkonto für unsere Community erstellen. Es ist einfach!

Neues Benutzerkonto erstellen

Anmelden

Du hast bereits ein Benutzerkonto? Melde Dich hier an.

Jetzt anmelden

Fachinformatiker.de, 2024 by SE Internet Services

fidelogo_small.png

Schicke uns eine Nachricht!

Fachinformatiker.de ist die größte IT-Community
rund um Ausbildung, Job, Weiterbildung für IT-Fachkräfte.

Fachinformatiker.de App

Download on the App Store
Get it on Google Play

Kontakt

Hier werben?
Oder sende eine E-Mail an

Social media u. feeds

Jobboard für Fachinformatiker und IT-Fachkräfte

×
×
  • Neu erstellen...