Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add search with paging extensions. #171

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions LdapForNet/LdapSearchExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,62 @@ public static async Task<IList<LdapEntry>> SearchBySidAsync(this ILdapConnection
{
return await connection.SearchBySidAsync(LdapUtils.GetDnFromHostname(), sid);
}

public static IList<LdapEntry> SearchWithPaging(
this ILdapConnection connection,
SearchRequest request,
int pageSize = 1000)
{
var results = new List<DirectoryEntry>();
var resultRequestControl = new PageResultRequestControl(pageSize);
request.Controls.Add(resultRequestControl);

var response = (SearchResponse)connection.SendRequest(request);
results.AddRange(response.Entries);

PageResultResponseControl pageResultResponseControl;
while (true)
{
pageResultResponseControl = (PageResultResponseControl)response.Controls.FirstOrDefault(_ => _ is PageResultResponseControl);
if (pageResultResponseControl == null || pageResultResponseControl.Cookie.Length == 0)
{
break;
}

resultRequestControl.Cookie = pageResultResponseControl.Cookie;
response = (SearchResponse)connection.SendRequest(request);
results.AddRange(response.Entries);
}
return results.ConvertAll(x => x.ToLdapEntry());
}

public static async Task<IList<LdapEntry>> SearchWithPagingAsync(
this ILdapConnection connection,
SearchRequest request,
int pageSize = 1000,
CancellationToken token = default)
{
var results = new List<DirectoryEntry>();
var resultRequestControl = new PageResultRequestControl(pageSize);
request.Controls.Add(resultRequestControl);

var response = (SearchResponse)await connection.SendRequestAsync(request, token);
results.AddRange(response.Entries);

PageResultResponseControl pageResultResponseControl;
while (true)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check cancellationToken instead of true

while(!token.IsCancellationRequested)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would result in returning an incomplete result if the token was cancelled before all requests were processed, instead of throwing an exception, which is not a desired behaviour.
With the cancellation token fix PR that was already accepted the SendRequestAsync call will throw an exception if the token was cancelled which is what we would want to happen.

{
pageResultResponseControl = (PageResultResponseControl)response.Controls.FirstOrDefault(_ => _ is PageResultResponseControl);
if (pageResultResponseControl == null || pageResultResponseControl.Cookie.Length == 0)
{
break;
}

resultRequestControl.Cookie = pageResultResponseControl.Cookie;
response = (SearchResponse)await connection.SendRequestAsync(request, token);
results.AddRange(response.Entries);
}
return results.ConvertAll(x => x.ToLdapEntry());
}
}
}