From 3c88f0a64a5162ca29bb0dfd1f55c9538270418b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0smail=20=C3=87A=C4=9EDA=C5=9E?= Date: Tue, 21 Feb 2023 10:06:36 +0300 Subject: [PATCH] Add UseSsl LDAP setting to support LDAPS --- .../LdapAuthenticationSource.cs | 22 +++++++++++++++++-- .../Ldap/Configuration/ILdapSettings.cs | 2 ++ .../Ldap/Configuration/LdapSettingNames.cs | 5 +++++ .../Ldap/Configuration/LdapSettings.cs | 7 ++++++ 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/Abp.Zero.Ldap/Ldap/Authentication/LdapAuthenticationSource.cs b/src/Abp.Zero.Ldap/Ldap/Authentication/LdapAuthenticationSource.cs index 8b58cfeb23..48ce9a4dcc 100644 --- a/src/Abp.Zero.Ldap/Ldap/Authentication/LdapAuthenticationSource.cs +++ b/src/Abp.Zero.Ldap/Ldap/Authentication/LdapAuthenticationSource.cs @@ -147,15 +147,33 @@ protected virtual Task CreatePrincipalContext(TTenant tenant, protected virtual async Task CreatePrincipalContext(TTenant tenant) { + var useSsl = await _settings.GetUseSsl(tenant?.Id); + var contextType = await _settings.GetContextType(tenant?.Id); + + var options = useSsl + ? ContextOptions.SecureSocketLayer | ContextOptions.Negotiate + : GetDefaultOptionForStore(contextType); + return new PrincipalContext( - await _settings.GetContextType(tenant?.Id), + contextType, ConvertToNullIfEmpty(await _settings.GetDomain(tenant?.Id)), ConvertToNullIfEmpty(await _settings.GetContainer(tenant?.Id)), + options, ConvertToNullIfEmpty(await _settings.GetUserName(tenant?.Id)), ConvertToNullIfEmpty(await _settings.GetPassword(tenant?.Id)) ); } + private ContextOptions GetDefaultOptionForStore(ContextType contextType) + { + if (contextType == ContextType.Machine) + { + return ContextOptions.Negotiate; + } + + return ContextOptions.Negotiate | ContextOptions.Signing | ContextOptions.Sealing; + } + protected virtual async Task CheckIsEnabled(TTenant tenant) { if (!_ldapModuleConfig.IsEnabled) @@ -178,4 +196,4 @@ protected static string ConvertToNullIfEmpty(string str) : str; } } -} +} \ No newline at end of file diff --git a/src/Abp.Zero.Ldap/Ldap/Configuration/ILdapSettings.cs b/src/Abp.Zero.Ldap/Ldap/Configuration/ILdapSettings.cs index c24300786c..5ef098b683 100644 --- a/src/Abp.Zero.Ldap/Ldap/Configuration/ILdapSettings.cs +++ b/src/Abp.Zero.Ldap/Ldap/Configuration/ILdapSettings.cs @@ -20,5 +20,7 @@ public interface ILdapSettings Task GetUserName(int? tenantId); Task GetPassword(int? tenantId); + + Task GetUseSsl(int? tenantId); } } \ No newline at end of file diff --git a/src/Abp.Zero.Ldap/Ldap/Configuration/LdapSettingNames.cs b/src/Abp.Zero.Ldap/Ldap/Configuration/LdapSettingNames.cs index f2f1b00c96..52f3779388 100644 --- a/src/Abp.Zero.Ldap/Ldap/Configuration/LdapSettingNames.cs +++ b/src/Abp.Zero.Ldap/Ldap/Configuration/LdapSettingNames.cs @@ -34,5 +34,10 @@ public static class LdapSettingNames /// Abp.Zero.Ldap.Password /// public const string Password = "Abp.Zero.Ldap.Password"; + + /// + /// Abp.Zero.Ldap.UseSsl + /// + public const string UseSsl = "Abp.Zero.Ldap.UseSsl"; } } \ No newline at end of file diff --git a/src/Abp.Zero.Ldap/Ldap/Configuration/LdapSettings.cs b/src/Abp.Zero.Ldap/Ldap/Configuration/LdapSettings.cs index e71111ac96..7bf710e175 100644 --- a/src/Abp.Zero.Ldap/Ldap/Configuration/LdapSettings.cs +++ b/src/Abp.Zero.Ldap/Ldap/Configuration/LdapSettings.cs @@ -60,5 +60,12 @@ public virtual Task GetPassword(int? tenantId) ? SettingManager.GetSettingValueForTenantAsync(LdapSettingNames.Password, tenantId.Value) : SettingManager.GetSettingValueForApplicationAsync(LdapSettingNames.Password); } + + public Task GetUseSsl(int? tenantId) + { + return tenantId.HasValue + ? SettingManager.GetSettingValueForTenantAsync(LdapSettingNames.UseSsl, tenantId.Value) + : SettingManager.GetSettingValueForApplicationAsync(LdapSettingNames.UseSsl); + } } } \ No newline at end of file