前言

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

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

准备

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

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

Application

 public class PublicAccountAppService: IdentityUserAppService
{
public PublicAccountAppService(
IdentityUserManager userManager,
IIdentityUserRepository userRepository,
IIdentityRoleRepository roleRepository,
IOptions<IdentityOptions> identityOptions)
: base(userManager, userRepository, roleRepository, identityOptions)
{ } public override async Task<IdentityUserDto> CreateAsync(
IdentityUserCreateDto input)
{
ValidateRegisterInput(input);
await CheckRegisterableByPhone(input.PhoneNumber);
return await base.CreateAsync(input);
} private static void ValidateRegisterInput(IdentityUserCreateDto input)
{
if (input.PhoneNumber.IsNullOrWhiteSpace())
{
throw new AbpValidationException(
"Phone number is required for new users!",
new List<ValidationResult>
{
new ValidationResult(
"Phone number can not be empty!",
new []{"PhoneNumber"}
)
}
);
}
} private async Task CheckRegisterableByPhone(string phoneNumber)
{
var isPhoneNumberExist = await _accountRepository.IsPhoneNumberExistAsync(phoneNumber);
if (isPhoneNumberExist)
{
throw new AbpValidationException(
"Phone number already exist!",
new List<ValidationResult>
{
new ValidationResult(
"Phone number already exist!",
new []{"PhoneNumber"}
)
}
);
}
}
}

  

Domain

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

尽量遵守DDD 分层的原则。

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

Repository

实现Domain 层定义的接口

 1  public class AccountRepository: IAccountRepository, ITransientDependency
2 {
3 private readonly IRepository<IdentityUser, Guid> _identityUserRepository;
4
5 public AccountRepository(IRepository<IdentityUser, Guid> 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 }

替换默认实例

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

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

其他

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

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

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

Abp 实现通过手机号注册用户的更多相关文章

  1. java在线聊天项目 客户端登陆窗口LoginDialog的注册用户功能 修改注册逻辑 增空用户名密码的反馈 增加showMessageDialog()提示框

    LoginDialog类的代码修改如下: package com.swift.frame; import java.awt.EventQueue; import java.awt.event.Acti ...

  2. 在Django中进行注册用户的邮件确认

    之前利用Flask写博客时(http://hbnnlove.sinaapp.com),我对注册模块的逻辑设计很简单,就是用户填写注册表单,然后提交,数据库会更新User表中的数据,字段主要有用户名,哈 ...

  3. Servlet页面注册用户的小程序(一)

    本实例实现用userreg.jsp页面中的表单提交注册请求,把注册信息提交给regservlet写入数据库并且查询新用户显示出来. 一.准备工作. 1.jdbc数据驱动开发包mysql-connect ...

  4. Textview下划线注册用户跳转实现

    在xml中: <TextView android:id="@+id/textView_regtext" android:layout_width="wrap_con ...

  5. WordPress 前端投稿/编辑插件 DJD Site Post(支持游客和已注册用户)

    转自:http://www.wpdaxue.com/front-end-publishing.html 说到前端用户投稿,倡萌之前推荐过3个不错的插件: WordPress匿名投稿插件:DX-Cont ...

  6. ASP.NET MVC+EF框架+EasyUI实现权限管理系列(17)-注册用户功能的细节处理(各种验证)

    原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列(17)-注册用户功能的细节处理(各种验证) ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇)   (1):框 ...

  7. SpringBoot对注册用户密码进行Bcrypt密码加密

    一.注册用户时,用户的密码一般都是加密存储在数据库中.今天我要用到的加密方式是Bcrypt加密. 1.首先在SpringBoot项目的pom文件中,引入SpringSecurity相关依赖,目的是为了 ...

  8. Django中,ajax检测注册用户信息是否可用?

    ajax检测注册用户信息主体思路 1.在settings.py中配置需要使用的信息 #对static文件进行配置 STATICFILES_DIRS=[ os.path.join(BASE_DIR,'s ...

  9. 如何在Web.config中注册用户控件和自定义控件

    问题: 在ASP.NET 的早先版本里,开发人员通过在页面的顶部添加 指令来引入和使用自定义服务器控件和用户控件时,象这样: <%@ Register TagPrefix="scott ...

随机推荐

  1. AQS 详解之共享锁模式

    概括 AQS框架数据结构是一个先进先出的双向队列,当多个线程进行竞争资源时,那些竞争失败的线程会加入到队列中.他向上层提供了很多接口,其中一个是acquireShared获取共享模式的接口.本文将会根 ...

  2. TetBrains产品快捷键大全

     快捷键大全

  3. Visual Studio 2022 预览版下载来了(x64位)

    Visual Studio 2022 预览版下载:https://visualstudio.microsoft.com/zh-hans/vs/preview/vs2022/

  4. HTTP 错误 500.21 - Internal Server Error 解决方案【转】

    HTTP 错误 500.21 - Internal Server Error 解决方案:  今天在测试网站的时候,在浏览器中输入http://localhost/时,发生如下错误: HTTP Erro ...

  5. pytest配置文件pytest.ini

    说明: pytest.ini是pytest的全局配置文件,一般放在项目的根目录下 是一个固定的文件-pytest.ini 可以改变pytest的运行方式,设置配置信息,读取后按照配置的内容去运行 py ...

  6. 【算法篇】Bitmap 算法

    首先,什么是Bitmap算法(位图算法)呢? 一:定义: Bit map就是用一个bit位来标记某个元素对应的Value, 而Key即是该元素.使用Bit为用来存储数据的单位, 可以大大节省存储空间. ...

  7. 如何在 Mac 上强制退出 App

    同时按住三个按键:Option.Command 和 Esc (Escape) 键.或者,从屏幕左上角的苹果菜单  中选取"强制退出".(这类似于在 PC 上按下 Control- ...

  8. Oracle 关于v$之类的视图使用说明

    官方文档:https://docs.oracle.com/en/database/oracle/oracle-database/19/cncpt/data-dictionary-and-dynamic ...

  9. Kafka 的设计架构你知道吗?

    Producer :消息生产者,就是向 kafka broker 发消息的客户端. Consumer :消息消费者,向 kafka broker 取消息的客户端. Topic :可以理解为一个队列,一 ...

  10. Constant Pool和String Constant Pool详解

    Constant Pool常量池的概念: 在讲到String的一些特殊情况时,总会提到String Pool或者Constant Pool,但是我想很多人都不太明白Constant Pool到底是个怎 ...