From 90c85814b0624f1b6ca8292361c70fc6a8c8854f Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Fri, 17 Jan 2025 11:24:30 +0100 Subject: [PATCH] refactor(DirectorySearchService): Methoden asynchron gemacht --- .../Application/IDirectorySearchService.cs | 70 ++++++++++++++++++- .../DirectorySearchService.cs | 67 ++++++++++++++---- 2 files changed, 122 insertions(+), 15 deletions(-) diff --git a/DigitalData.Core.Abstractions/Application/IDirectorySearchService.cs b/DigitalData.Core.Abstractions/Application/IDirectorySearchService.cs index 97d2fbf..74f410c 100644 --- a/DigitalData.Core.Abstractions/Application/IDirectorySearchService.cs +++ b/DigitalData.Core.Abstractions/Application/IDirectorySearchService.cs @@ -13,14 +13,82 @@ namespace DigitalData.Core.Abstractions.Application Dictionary CustomSearchFilters { get; } - bool ValidateCredentials(string dirEntryUsername, string dirEntryPassword); + /// + /// Creates the connections to the server and returns a Boolean value that specifies + /// whether the specified username and password are valid. + /// + /// The username that is validated on the server. See the Remarks section + /// for more information on the format of userName. + /// The password that is validated on the server. + /// True if the credentials are valid; otherwise, false. + bool ValidateCredentials(string userName, string password); + /// + /// Creates the connections to the server asynchronously and returns a Boolean value that specifies + /// whether the specified username and password are valid. + /// + /// The username that is validated on the server. See the Remarks section + /// for more information on the format of userName. + /// The password that is validated on the server. + /// True if the credentials are valid; otherwise, false. + Task ValidateCredentialsAsync(string userName, string password); + + /// + /// Finds all directory entries matching the specified filter. + /// + /// The search root. + /// The search filter. + /// The search scope. + /// The size limit. + /// The properties to load. + /// A containing the results. DataResult> FindAll(DirectoryEntry searchRoot, string filter, SearchScope searchScope = SearchScope.Subtree, int sizeLimit = 5000, params string[] properties); + /// + /// Finds all directory entries matching the specified filter asynchronously. + /// + /// The search root. + /// The search filter. + /// The search scope. + /// The size limit. + /// The properties to load. + /// A containing the results. + Task>> FindAllAsync(DirectoryEntry searchRoot, string filter, SearchScope searchScope = SearchScope.Subtree, int sizeLimit = 5000, params string[] properties); + + /// + /// Finds all directory entries matching the specified filter, using the user cache. + /// + /// The username. + /// The search filter. + /// The search scope. + /// The size limit. + /// The properties to load. + /// A containing the results. DataResult> FindAllByUserCache(string username, string filter, SearchScope searchScope = SearchScope.Subtree, int sizeLimit = 5000, params string[] properties); + /// + /// Finds all directory entries matching the specified filter asynchronously, using the user cache. + /// + /// The username. + /// The search filter. + /// The search scope. + /// The size limit. + /// The properties to load. + /// A containing the results. + Task>> FindAllByUserCacheAsync(string username, string filter, SearchScope searchScope = SearchScope.Subtree, int sizeLimit = 5000, params string[] properties); + + /// + /// Sets the search root in the cache. + /// + /// The directory entry username. + /// The directory entry password. void SetSearchRootCache(string username, string password); + /// + /// Gets the search root from the cache. + /// + /// The directory entry username. + /// The cached if found; otherwise, null. DirectoryEntry? GetSearchRootCache(string username); } } \ No newline at end of file diff --git a/DigitalData.Core.Application/DirectorySearchService.cs b/DigitalData.Core.Application/DirectorySearchService.cs index 16b714c..19ad47f 100644 --- a/DigitalData.Core.Application/DirectorySearchService.cs +++ b/DigitalData.Core.Application/DirectorySearchService.cs @@ -8,6 +8,7 @@ using Microsoft.Extensions.Options; namespace DigitalData.Core.Application { + //TODO: rename as DirectorySearcher [SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "")] public class DirectorySearchService : IDirectorySearchService { @@ -45,17 +46,31 @@ namespace DigitalData.Core.Application } /// - /// Validates the credentials of a directory entry. + /// Creates the connections to the server and returns a Boolean value that specifies + /// whether the specified username and password are valid. /// - /// The directory entry username. - /// The directory entry password. + /// The username that is validated on the server. See the Remarks section + /// for more information on the format of userName. + /// The password that is validated on the server. /// True if the credentials are valid; otherwise, false. - public bool ValidateCredentials(string dirEntryUsername, string dirEntryPassword) + public bool ValidateCredentials(string userName, string password) { using var context = new PrincipalContext(ContextType.Domain, ServerName, Root); - return context.ValidateCredentials(dirEntryUsername, dirEntryPassword); + return context.ValidateCredentials(userName, password); } + /// + /// Creates the connections to the server asynchronously and returns a Boolean value that specifies + /// whether the specified username and password are valid. + /// + /// The username that is validated on the server. See the Remarks section + /// for more information on the format of userName. + /// The password that is validated on the server. + /// True if the credentials are valid; otherwise, false. + public Task ValidateCredentialsAsync(string userName, string password) => Task.Run(() + => ValidateCredentials(userName, password)); + + //TODO: remove unnecessary DataResult /// /// Finds all directory entries matching the specified filter. /// @@ -69,7 +84,7 @@ namespace DigitalData.Core.Application { List list = new(); - var searcher = new DirectorySearcher() + using var searcher = new DirectorySearcher() { Filter = filter, SearchScope = searchScope, @@ -94,6 +109,18 @@ namespace DigitalData.Core.Application return Result.Success>(list); } + /// + /// Finds all directory entries matching the specified filter asynchronously. + /// + /// The search root. + /// The search filter. + /// The search scope. + /// The size limit. + /// The properties to load. + /// A containing the results. + public Task>> FindAllAsync(DirectoryEntry searchRoot, string filter, SearchScope searchScope = SearchScope.Subtree, int sizeLimit = 5000, params string[] properties) => Task.Run(() + => FindAll(searchRoot, filter, searchScope, sizeLimit, properties)); + /// /// Finds all directory entries matching the specified filter, using the user cache. /// @@ -113,27 +140,39 @@ namespace DigitalData.Core.Application return FindAll(searchRoot, filter, searchScope, sizeLimit, properties); } + /// + /// Finds all directory entries matching the specified filter asynchronously, using the user cache. + /// + /// The username. + /// The search filter. + /// The search scope. + /// The size limit. + /// The properties to load. + /// A containing the results. + public Task>> FindAllByUserCacheAsync(string username, string filter, SearchScope searchScope = SearchScope.Subtree, int sizeLimit = 5000, params string[] properties) => Task.Run(() + => FindAllByUserCache(username, filter, searchScope, sizeLimit, properties)); + /// /// Sets the search root in the cache. /// - /// The directory entry username. - /// The directory entry password. - public void SetSearchRootCache(string dirEntryUsername, string dirEntryPassword) + /// The directory entry username. + /// The directory entry password. + public void SetSearchRootCache(string username, string password) { if (_userCacheExpiration is DateTimeOffset cacheExpiration) - _memoryCache.Set(key: dirEntryUsername, new DirectoryEntry(path: SearchRootPath, username: dirEntryUsername, password: dirEntryPassword), absoluteExpiration: cacheExpiration); + _memoryCache.Set(key: username, new DirectoryEntry(path: SearchRootPath, username: username, password: password), absoluteExpiration: cacheExpiration); else - _memoryCache.Set(key: dirEntryUsername, new DirectoryEntry(path: SearchRootPath, username: dirEntryUsername, password: dirEntryPassword)); + _memoryCache.Set(key: username, new DirectoryEntry(path: SearchRootPath, username: username, password: password)); } /// /// Gets the search root from the cache. /// - /// The directory entry username. + /// The directory entry username. /// The cached if found; otherwise, null. - public DirectoryEntry? GetSearchRootCache(string dirEntryUsername) + public DirectoryEntry? GetSearchRootCache(string username) { - _memoryCache.TryGetValue(dirEntryUsername, out DirectoryEntry? root); + _memoryCache.TryGetValue(username, out DirectoryEntry? root); return root; } }