• Abp 实现通过手机号注册用户


    前言

    Abp 的 Identity 模块,实现了用户的管理,但是对于国内来讲,很多场景不能很好适配。比如:通过手机号进行注册的场景。

    Abp vnext Identity 以及 asp.net core identity  默认只有 Email 必填以及唯一的校验,缺少手机号必要的校验;对此我们需要进行适当的调整,以作适配。

     

    准备

     

    建议先参考 IdentityUserAppService 对用户注册的实现;

    由于手机号验证的场景基本上是需要的,所以本次采用重写的方式,当然也可以参考其代码,自定义自己的实现。

     

    Application 

      

    Domain

    由于 IIdentityUserRepository 缺少对手机号是否存在的默认实现,我们可以新增对应Repository 来实现相关功能。

    尽量遵守DDD 分层的原则。

    1  public interface IAccountRepository
    2     {
    3         Task<bool> IsPhoneNumberExistAsync(string phoneNumber);
    4     }
    View Code

     

    Repository

    实现Domain 层定义的接口

     1  public class AccountRepository: IAccountRepository, ITransientDependency
     2     {
     3         private readonly IRepository _identityUserRepository;
     4 
     5         public AccountRepository(IRepository identityUserRepository)
     6         {
     7             _identityUserRepository = identityUserRepository;
     8         }
     9 
    10         public async Task<bool> IsPhoneNumberExistAsync(string phoneNumber)
    11         {
    12             return await _identityUserRepository.AnyAsync(
    13                 c => c.PhoneNumber == phoneNumber);
    14         }
    15     }
    View Code

     

    替换默认实例

    我们已经完成了对 IdentityUserAppService 创建方法的重写,需要替换默认的接口实例对象,可以参考 Customizing Application Modules Overriding Services | Documentation Center | ABP.IO

        [Dependency(ReplaceServices = true)]
        [ExposeServices(typeof(IIdentityUserAppService), typeof(IdentityUserAppService), typeof(PublicAccountAppService))]
        public class PublicAccountAppService: IdentityUserAppService
        {...}
    View Code

     

    其他

    主要的修改已经调整完毕。但是由于AbpUser 表没有 PhoneNumber 的相关索引,可以自行通过 Migration 进行添加。

    Abp 框架比较优秀,很多方面也算是最佳实践,推荐使用。

    改动比较小,修改起来也比较方便;当然也可以完全重写 注册的方法。下次有时间可以再整理下通过手机号登陆的实现。

     

  • 相关阅读:
    Flutter GetX的使用
    想要好身材,你不得不看的高蛋白饮食计划
    全面解析优化企业Microsoft 365网络的加速方案
    Qt鼠标事件全面解析:从基础到实战
    FPGA 学习笔记:Vitis IDE launch failed 的解决方法
    山西电力市场日前价格预测【2023-10-28】
    【Django】RESTful API接口设计风格
    深度学习中的注意力机制模型及代码实现(SE Attention、CBAM Attention)
    8.ROS编程学习:自定义服务数据python调用
    面试题:你是如何计划和组织一个大型的软件测试项目的?
  • 原文地址:https://www.cnblogs.com/qiu-gu/p/16130602.html