LDAP/活动目录
LdapAuthenticationSource 是一种外部身份验证实现,使用户使用其 LDAP(活动目录)用户名和密码登录。
如果我们要使用LDAP认证,我们首先将Abp.Zero.Ldap nuget包添加到我们的项目中(一般添加到Core(域)项目中)。然后我们应该为我们的应用程序扩展 LdapAuthenticationSource,如下所示:
public class MyLdapAuthenticationSource : LdapAuthenticationSource<Tenant, User>
{
public MyLdapAuthenticationSource(ILdapSettings settings, IAbpZeroLdapModuleConfig ldapModuleConfig)
: base(settings, ldapModuleConfig)
{
}
}
最后,我们应该为 AbpZeroLdapModule 设置一个模块依赖,并使用上面创建的身份验证源启用 LDAP:
[DependsOn(typeof(AbpZeroLdapModule))]
public class MyApplicationCoreModule : AbpModule
{
public override void PreInitialize()
{
Configuration.Modules.ZeroLdap().Enable(typeof (MyLdapAuthenticationSource));
}
...
}
完成这些步骤后,将为您的应用程序启用 LDAP 模块。但默认情况下不启用 LDAP 身份验证。我们可以使用设置启用它。
设置
LdapSettingNames 类定义用于设置名称的常量。您可以在更改设置(或获取设置)时使用这些常量名称。 LDAP 设置是按租户的(对于多租户应用程序)。因此,不同的租户有不同的设置(参见 github 上的设置定义)。
正如您在 MyLdapAuthenticationSource 构造函数中看到的那样,LdapAuthenticationSource 期望 ILdapSettings 作为构造函数参数。此接口用于获取域、用户名和密码等 LDAP 设置以连接到 Active Directory。默认实现(LdapSettings 类)从设置管理器获取这些设置。
如果您使用设置管理器,那么没问题。您可以使用设置管理器 API 更改 LDAP 设置。如果需要,您可以将初始/种子数据添加到数据库以默认启用 LDAP 身份验证。
注意:如果您没有定义域、用户名和密码,如果您的应用程序在具有适当权限的域中运行,则 LDAP 身份验证适用于当前域。
自定义设置
如果你想定义另一个设置源,你可以实现一个自定义的ILdapSettings类,如下所示:
public class MyLdapSettings : ILdapSettings
{
public async Task<bool> GetIsEnabled(int? tenantId)
{
return true;
}
public async Task<ContextType> GetContextType(int? tenantId)
{
return ContextType.Domain;
}
public async Task<string> GetContainer(int? tenantId)
{
return null;
}
public async Task<string> GetDomain(int? tenantId)
{
return null;
}
public async Task<string> GetUserName(int? tenantId)
{
return null;
}
public async Task<string> GetPassword(int? tenantId)
{
return null;
}
}
并在模块的 PreInitialize 中将其注册到 IOC:
[DependsOn(typeof(AbpZeroLdapModule))]
public class MyApplicationCoreModule : AbpModule
{
public override void PreInitialize()
{
IocManager.Register<ILdapSettings, MyLdapSettings>(); //change default setting source
Configuration.Modules.ZeroLdap().Enable(typeof (MyLdapAuthenticationSource));
}
...
}
然后您可以从任何其他来源获取 LDAP 设置。
https://aspnetboilerplate.com/Pages/Documents/Zero/User-Management#ldapactive-directory