feat(DirectorySearchService): Validierungs-Methode für Anmeldeinformationen hinzugefügt
- Neue Methode zur Überprüfung von Anmeldeinformationen in DirectorySearchService hinzugefügt. - Anpassung der Eigenschaften von Suchergebnissen ermöglicht.
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
using DigitalData.Core.Contracts.Application;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Configuration;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.DirectoryServices;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System.DirectoryServices.AccountManagement;
|
||||
|
||||
namespace DigitalData.Core.Application
|
||||
{
|
||||
@@ -12,15 +12,39 @@ namespace DigitalData.Core.Application
|
||||
public class DirectorySearchService : ServiceBase, IDirectorySearchService
|
||||
{
|
||||
private readonly IMemoryCache _memoryCache;
|
||||
public readonly string SearchRootPath;
|
||||
public string ServerName { get; }
|
||||
public string Root { get; }
|
||||
public string SearchRootPath { get; }
|
||||
private readonly DateTimeOffset _userCacheExpiration;
|
||||
public Dictionary<string, string> CustomSearchFilters { get; }
|
||||
|
||||
public DirectorySearchService(IConfiguration configuration, ILogger<DirectoryService> logger, IMemoryCache memoryCache)
|
||||
public DirectorySearchService(IConfiguration configuration, ILogger<DirectorySearchService> logger, IMemoryCache memoryCache)
|
||||
{
|
||||
SearchRootPath = configuration["DirectorySearch:SearchRootPath"] ?? throw new ConfigurationErrorsException("SearchRootPath configuration is missing.");
|
||||
_memoryCache = memoryCache;
|
||||
|
||||
ServerName = configuration["DirectorySearch:ServerName"] ?? throw new InvalidOperationException("The server name for directory search is not configured. Please specify the 'DirectorySearch:ServerName' in the configuration.");
|
||||
|
||||
Root = configuration["DirectorySearch:Root"] ?? throw new InvalidOperationException("The root for directory search is not configured. Please specify the 'DirectorySearch:Root' in the configuration.");
|
||||
|
||||
SearchRootPath = $"LDAP://{ServerName}/{Root}";
|
||||
|
||||
var customSearchFiltersSection = configuration.GetSection("DirectorySearch:CustomSearchFilters");
|
||||
CustomSearchFilters = customSearchFiltersSection.Get<Dictionary<string, string>>() ?? new();
|
||||
|
||||
var dayCounts = configuration.GetValue<int>("DirectorySearch:UserCacheExpirationDays");
|
||||
if (dayCounts == default)
|
||||
_userCacheExpiration = default;
|
||||
else
|
||||
_userCacheExpiration = DateTimeOffset.Now.Date.AddDays(dayCounts);
|
||||
}
|
||||
|
||||
public IServiceResult<IEnumerable<ResultPropertyCollection>> FindAll(DirectoryEntry searchRoot, string filter, SearchScope searchScope = SearchScope.Subtree, int sizeLimit = 5000)
|
||||
public bool ValidateCredentials(string dirEntryUsername, string dirEntryPassword)
|
||||
{
|
||||
using var context = new PrincipalContext(ContextType.Domain, ServerName, Root);
|
||||
return context.ValidateCredentials(dirEntryUsername, dirEntryPassword);
|
||||
}
|
||||
|
||||
public IServiceResult<IEnumerable<ResultPropertyCollection>> FindAll(DirectoryEntry searchRoot, string filter, SearchScope searchScope = SearchScope.Subtree, int sizeLimit = 5000, params string[] properties)
|
||||
{
|
||||
List<ResultPropertyCollection> list = new();
|
||||
|
||||
@@ -32,6 +56,14 @@ namespace DigitalData.Core.Application
|
||||
SearchRoot = searchRoot
|
||||
};
|
||||
|
||||
if (properties.Length > 0)
|
||||
{
|
||||
searcher.PropertiesToLoad.Clear();
|
||||
foreach (var property in properties)
|
||||
if(property is not null)
|
||||
searcher.PropertiesToLoad.Add(property);
|
||||
}
|
||||
|
||||
foreach (SearchResult result in searcher.FindAll())
|
||||
{
|
||||
ResultPropertyCollection rpc = result.Properties;
|
||||
@@ -41,7 +73,7 @@ namespace DigitalData.Core.Application
|
||||
return Successful<IEnumerable<ResultPropertyCollection>>(list);
|
||||
}
|
||||
|
||||
public IServiceResult<IEnumerable<ResultPropertyCollection>> FindAllByUserCache(string username, string filter, SearchScope searchScope = SearchScope.Subtree, int sizeLimit = 5000)
|
||||
public IServiceResult<IEnumerable<ResultPropertyCollection>> FindAllByUserCache(string username, string filter, SearchScope searchScope = SearchScope.Subtree, int sizeLimit = 5000, params string[] properties)
|
||||
{
|
||||
List<ResultPropertyCollection> list = new();
|
||||
|
||||
@@ -50,7 +82,21 @@ namespace DigitalData.Core.Application
|
||||
if (searchRoot is null)
|
||||
return Failed<IEnumerable<ResultPropertyCollection>>(MessageKey.DirSearcherDisconnected.ToString());
|
||||
|
||||
return FindAll(searchRoot, filter, searchScope, sizeLimit);
|
||||
return FindAll(searchRoot, filter, searchScope, sizeLimit, properties);
|
||||
}
|
||||
|
||||
public void SetSearchRootCache(string dirEntryUsername, string dirEntryPassword)
|
||||
{
|
||||
if (_userCacheExpiration == default)
|
||||
_memoryCache.Set(key: dirEntryUsername, new DirectoryEntry(path: SearchRootPath, username: dirEntryUsername, password: dirEntryPassword));
|
||||
else
|
||||
_memoryCache.Set(key: dirEntryUsername, new DirectoryEntry(path: SearchRootPath, username: dirEntryUsername, password: dirEntryPassword), absoluteExpiration: _userCacheExpiration);
|
||||
}
|
||||
|
||||
public DirectoryEntry? GetSearchRootCache(string dirEntryUsername)
|
||||
{
|
||||
_memoryCache.TryGetValue(dirEntryUsername, out DirectoryEntry? root);
|
||||
return root;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user