Abp 实现通过手机号注册用户
前言
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 实现通过手机号注册用户的更多相关文章
- java在线聊天项目 客户端登陆窗口LoginDialog的注册用户功能 修改注册逻辑 增空用户名密码的反馈 增加showMessageDialog()提示框
LoginDialog类的代码修改如下: package com.swift.frame; import java.awt.EventQueue; import java.awt.event.Acti ...
- 在Django中进行注册用户的邮件确认
之前利用Flask写博客时(http://hbnnlove.sinaapp.com),我对注册模块的逻辑设计很简单,就是用户填写注册表单,然后提交,数据库会更新User表中的数据,字段主要有用户名,哈 ...
- Servlet页面注册用户的小程序(一)
本实例实现用userreg.jsp页面中的表单提交注册请求,把注册信息提交给regservlet写入数据库并且查询新用户显示出来. 一.准备工作. 1.jdbc数据驱动开发包mysql-connect ...
- Textview下划线注册用户跳转实现
在xml中: <TextView android:id="@+id/textView_regtext" android:layout_width="wrap_con ...
- WordPress 前端投稿/编辑插件 DJD Site Post(支持游客和已注册用户)
转自:http://www.wpdaxue.com/front-end-publishing.html 说到前端用户投稿,倡萌之前推荐过3个不错的插件: WordPress匿名投稿插件:DX-Cont ...
- ASP.NET MVC+EF框架+EasyUI实现权限管理系列(17)-注册用户功能的细节处理(各种验证)
原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列(17)-注册用户功能的细节处理(各种验证) ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇) (1):框 ...
- SpringBoot对注册用户密码进行Bcrypt密码加密
一.注册用户时,用户的密码一般都是加密存储在数据库中.今天我要用到的加密方式是Bcrypt加密. 1.首先在SpringBoot项目的pom文件中,引入SpringSecurity相关依赖,目的是为了 ...
- Django中,ajax检测注册用户信息是否可用?
ajax检测注册用户信息主体思路 1.在settings.py中配置需要使用的信息 #对static文件进行配置 STATICFILES_DIRS=[ os.path.join(BASE_DIR,'s ...
- 如何在Web.config中注册用户控件和自定义控件
问题: 在ASP.NET 的早先版本里,开发人员通过在页面的顶部添加 指令来引入和使用自定义服务器控件和用户控件时,象这样: <%@ Register TagPrefix="scott ...
随机推荐
- ## [湖南省赛2019]Findme ###
[湖南省赛2019]Findme 1.题目概述 2.解题过程 010打开这几张图片 先简单分析一下这几张图片 简单分析 1.png 从外观上,1.png明显高度太低,需要更改 2.png 2.png末 ...
- Docker-compose 搭建 Harbor私有仓库
一. 安装docker-compose 1. 下载docker-compose的最新版本 curl -L "https://github.com/docker/compose/release ...
- NoSQL:Redis缓存、雪崩、击穿、穿透
Redis介绍 Redis(Remote Dictionary Server ),即远程字典服务,是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库 ...
- python psutila模块(示例)
# qianxiao996精心制作 #博客地址:https://blog.csdn.net/qq_36374896 import psutil import time import datetime ...
- 比Tensorflow还强?
大家好,我是章北海 Python是机器学习和深度学习的首选编程语言,但绝不是唯一.训练机器学习/深度学习模型并部署对外提供服务(尤其是通过浏览器)JavaScript 是一个不错的选择,市面上也出现了 ...
- mysql join 底层原理
你知道 Sql 中 left join 的底层原理吗? 2019-09-10阅读 7130 https://cloud.tencent.com/developer/column/2367 01.前 ...
- 请说说你对Struts2的拦截器的理解?
Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截器是可插拔的,拦截器是AOP的一种实现. 拦截器栈(Interceptor Stac ...
- 五、关于mycat踩过的坑
1.ER分表的从表无法批量插入,例如:insert into tab_a(c1,c2) values(v1,v2),(v11,v21)或者使用jdbctemplate进行batchUpdate操作会报 ...
- RabbitMQ踩坑记
之前我们给我们的系统加了一个使用SpringAOP+RabbitMQ+WebSocket进行实时消息通知功能(https://www.cnblogs.com/little-sheep/p/993488 ...
- spi协议
1. 概述 SPI = Serial Peripheral Interface,是串行外围设备接口,是一种高速,全双工,同步的通信总线.常规只占用四根线,节约了芯片管脚,PCB的布局省空间.现在越来越 ...