@@ -68,7 +68,7 @@ public static async Task RetrieveClaimsAsync(LdapSettings settings, ClaimsIdenti
68
68
69
69
if ( ! settings . IgnoreNestedGroups )
70
70
{
71
- GetNestedGroups ( settings . LdapConnection , identity , distinguishedName , groupCN , logger , retrievedClaims ) ;
71
+ GetNestedGroups ( settings . LdapConnection , identity , distinguishedName , groupCN , logger , retrievedClaims , new HashSet < string > ( ) ) ;
72
72
}
73
73
else
74
74
{
@@ -96,8 +96,10 @@ public static async Task RetrieveClaimsAsync(LdapSettings settings, ClaimsIdenti
96
96
}
97
97
}
98
98
99
- private static void GetNestedGroups ( LdapConnection connection , ClaimsIdentity principal , string distinguishedName , string groupCN , ILogger logger , IList < string > retrievedClaims )
99
+ private static void GetNestedGroups ( LdapConnection connection , ClaimsIdentity principal , string distinguishedName , string groupCN , ILogger logger , IList < string > retrievedClaims , HashSet < string > processedGroups )
100
100
{
101
+ retrievedClaims . Add ( groupCN ) ;
102
+
101
103
var filter = $ "(&(objectClass=group)(sAMAccountName={ groupCN } ))"; // This is using ldap search query language, it is looking on the server for someUser
102
104
var searchRequest = new SearchRequest ( distinguishedName , filter , SearchScope . Subtree , null ) ;
103
105
var searchResponse = ( SearchResponse ) connection . SendRequest ( searchRequest ) ;
@@ -109,8 +111,10 @@ private static void GetNestedGroups(LdapConnection connection, ClaimsIdentity pr
109
111
logger . LogWarning ( $ "More than one response received for query: { filter } with distinguished name: { distinguishedName } ") ;
110
112
}
111
113
112
- var group = searchResponse . Entries [ 0 ] ; //Get the object that was found on ldap
113
- retrievedClaims . Add ( groupCN ) ;
114
+ var group = searchResponse . Entries [ 0 ] ; // Get the object that was found on ldap
115
+ var groupDN = group . DistinguishedName ;
116
+
117
+ processedGroups . Add ( groupDN ) ;
114
118
115
119
var memberof = group . Attributes [ "memberof" ] ; // You can access ldap Attributes with Attributes property
116
120
if ( memberof != null )
@@ -119,7 +123,14 @@ private static void GetNestedGroups(LdapConnection connection, ClaimsIdentity pr
119
123
{
120
124
var nestedGroupDN = $ "{ Encoding . UTF8 . GetString ( ( byte [ ] ) member ) } ";
121
125
var nestedGroupCN = nestedGroupDN . Split ( ',' ) [ 0 ] . Substring ( "CN=" . Length ) ;
122
- GetNestedGroups ( connection , principal , distinguishedName , nestedGroupCN , logger , retrievedClaims ) ;
126
+
127
+ if ( processedGroups . Contains ( nestedGroupDN ) )
128
+ {
129
+ // We need to keep track of already processed groups because circular references are possible with AD groups
130
+ return ;
131
+ }
132
+
133
+ GetNestedGroups ( connection , principal , distinguishedName , nestedGroupCN , logger , retrievedClaims , processedGroups ) ;
123
134
}
124
135
}
125
136
}
0 commit comments