前言

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. CentOS7.5安装配置Jenkins

    一. 硬件配置: 1 GB的RAM 50 GB的驱动器空间 二. 系统环境: [root@Jenkins ~]# cat /etc/redhat-release CentOS Linux releas ...

  2. SpringBoot---Eclipse编辑yml文件不能自动提示的问题(Eclipse安装插件STS)

    在学习了几天SpringBoot之后,刚开始跟着别人的博客使用的是IDEA,后来跟着视频学,讲师用的eclipse,便跟着用了,但是发现在编辑yml配置文件的时候,没有自动提示的功能,百度之后发现是没 ...

  3. ctf之POST

    题目信息如下 可知该题考察post请求知识 直接将what=flag以post传参格式进行传参即可获得flag

  4. JavaScript 里的 'this' 的一般解释

    本文旨在帮助自己和大家理解 JS 里的 this, 翻译.整理并改写自本人关注的一个博主 Dmitri Pavlutin,原文链接如下: https://dmitripavlutin.com/gent ...

  5. centos配置ssh服务并简单测试

    最近在做计算机集群方面的东西,简单弄了一下ssh服务. 首先把前提情况介绍一下: 1.我是用的虚拟机先模拟的,也不是没有真机,就是跑来跑去麻烦. 2.装了三个相同配置的centos虚拟机,详细参数就不 ...

  6. chrome删除保存的密码

    chrome删除保存的密码 关于谷歌密码管理器 该管理器是将我们的密码管理在google的账号中,当然,谷歌是说用了加密技术保存的,不会存储明文. https://passwords.google.c ...

  7. IDEA 常用快捷键操作

    自定义设置及查询: 操作路径:file-setting-Keymap-Editor actions 右击需要修改的action操作,或者右击Editor actions,选择添加Add Keyboar ...

  8. pip 安装更新卸载 pip/yum换源

    pip安装:sudo apt-get install python3-pip pip更新:sudo pip3 install --upgrade pip pip卸载:sudo apt-get remo ...

  9. spring-boot--lernning之自定义starters

    思路: 1这个场景需要使用到的依赖是什么??? 2如何编写自动配置 @Configuration 指定这个类是一个配置类 @ConditionalOnXXXX 指定条件下成立的情况下自动配置类生效 @ ...

  10. 转载:23种常用设计模式的UML类图

    转载至:https://www.cnblogs.com/zytrue/p/8484806.html 23种常用设计模式的UML类图 本文UML类图参考<Head First 设计模式>(源 ...