ABP实战--集成Ladp/AD认证

  参照Hunter的ABP-Zero模块中用户管理部分。html

  因为咱们公司的各系统基本都是AD账号登陆的,因此咱们需扩展ABP的AuthenticationSource。async

  • 添加MyLdapAuthenticationSource.cs及MyLdapSettings.cs

  Core层的Authorization目录下新建Ldap目录,并新建两个MyLdapAuthenticationSource.csMyLdapSettings.cs,代码以下:ide

  MyLdapAuthenticationSource.cs函数

public  class MyLdapAuthenticationSource : LdapAuthenticationSource<Tenant, User>
    {
        public MyLdapAuthenticationSource(ILdapSettings settings, IAbpZeroLdapModuleConfig ldapModuleConfig)
       : base(settings, ldapModuleConfig)
        {
        }
    }

 

  

  LdapAuthenticationSource的构造函数须要两个参数:ILdapSettings及IAbpZeroLdapModuleConfig,咱们构造本身的MyLdapSettings:

  MyLdapSettings.csspa

public class MyLdapSettings : ILdapSettings
    {
        private const string DomainName = "XXXX.com";
        private const string Container = "OU=XXX,DC=XXXX,DC=com";
        private const string UserName = "XXXX";
        private const string Password = "XXXX";
        private const string ADPath = "LDAP://XXXXX";

        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 Container;
        }

        public async Task<string> GetDomain(int? tenantId)
        {
            return DomainName;
        }

        public async Task<string> GetUserName(int? tenantId)
        {
            return UserName;
        }

        public async Task<string> GetPassword(int? tenantId)
        {
            return Password;
        }
    }

  

  • 在CoreModule中启用

  这里ILdapSettings咱们使用MyLdapSettings来注册,可是IAbpZeroLdapModuleConfig使用默认的便可。code

[DependsOn(typeof(AbpZeroCoreModule))]
    public class CeciCoreModule : AbpModule
    {
        public override void PreInitialize()
        {
            Configuration.Auditing.IsEnabledForAnonymousUsers = true;

            IocManager.Register<IAbpZeroLdapModuleConfig, AbpZeroLdapModuleConfig>(); IocManager.Register<ILdapSettings, MyLdapSettings>(); //change default setting source
            Configuration.Modules.ZeroLdap().Enable(typeof(MyLdapAuthenticationSource));
  •  重载认证逻辑

  目前咱们只使用了Ldap最简逻辑,如须要复杂逻辑(如从AD中得到用户部门职位等),需重载LdapAuthenticationSource的方法来自定义实现。htm

  

相关文章
相关标签/搜索