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

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

  由于我们公司的各系统基本都是AD帐号登录的,所以我们需扩展ABP的AuthenticationSource。

  • 添加MyLdapAuthenticationSource.cs及MyLdapSettings.cs

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

  MyLdapAuthenticationSource.cs

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

 

  

  LdapAuthenticationSource的构造函数需要两个参数:ILdapSettings及IAbpZeroLdapModuleConfig,我们构造自己的MyLdapSettings:

  MyLdapSettings.cs

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使用默认的即可。

[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的方法来自定义实现。

  

 

转载于:https://www.cnblogs.com/ceci/p/9263700.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值