ABP涵盖的内容很多,对于国人来说,有很多地方不太实用,需要按国内环境修改使用,由于笔者空余时间不是很多,只能不定时更新这个系列的文章,以便交流学习,本系列不会说框架的原理(官方又比较详细的文章)。
本文主要操作是取消注册用户时强制填写邮箱的功能,改为可不填(填写邮箱才验证),效果如上。直接上代码:
1.首先定义属性验证
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public sealed class EmailAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
var text = value as string;
if (string.IsNullOrEmpty(text))
return true;
else
{
var EmailRegex = @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$";
var regex = new Regex(EmailRegex);
return regex.IsMatch(text);
}
}
}
2.使用这个验证替换原有的验证,并取消Required,项目用到验证的几处都要改。
3.context加入,然后更新数据库
modelBuilder.Entity<User>().Property(a => a.EmailAddress).HasDefaultValue("");
4.重写验证方法:
public override async Task<IdentityResult> CheckDuplicateUsernameOrEmailAddressAsync(long? expectedUserId, string userName, string emailAddress)
{
var user = (await FindByNameAsync(userName));
if (user != null && user.Id != expectedUserId)
{
throw new UserFriendlyException(string.Format(L("Identity.DuplicateUserName"), userName));
}
if (!string.IsNullOrEmpty(emailAddress))
{
user = (await FindByEmailAsync(emailAddress));
if (user != null && user.Id != expectedUserId)
{
throw new UserFriendlyException(string.Format(L("Identity.DuplicateEmail"), emailAddress));
}
}
return IdentityResult.Success;
}
5.修改前端js,这个很简单就不贴了。
需要的可以去下载abp取消邮箱验证前端代码