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

  

相關文章
相關標籤/搜索