【问题标题】:How does LDAP work in ASP.NET Boilerplate? [closed]LDAP 在 ASP.NET 样板中如何工作? [关闭]
【发布时间】:2018-02-19 18:03:19
【问题描述】:

我在文档中没有看到任何关于如何:

  • 连接到 LDAP 并
  • 根据 AD 组设置用户访问控制。

【问题讨论】:

  • 为什么有人投了反对票?这是一个合法的问题。
  • 这是一个关于什么的合理问题?这是 (a) 不清楚你在问什么 (b) 太宽泛了 (c) 不是关于计算机编程和 (d) 题外话。

标签: authentication configuration active-directory ldap aspnetboilerplate


【解决方案1】:

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

【讨论】:

  • 谢谢,这对我有很大帮助,它让我朝着正确的方向前进!
  • 请参阅this
  • @Alper,如果用户存在于特定的 AD 组中,我们如何才能只允许用户登录?
  • 我已按照您的步骤操作并将 UseUserPrincipalNameAsUserName 设置为 true,但我仍然收到不正确的用户名或密码。我错过了什么吗?
猜你喜欢
  • 2018-05-17
  • 2010-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多