feat:完善注册功能

This commit is contained in:
陈淳
2023-03-30 11:49:39 +08:00
parent bcf7802f94
commit 6b2ef71296
8 changed files with 121 additions and 51 deletions

View File

@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;

View File

@@ -15,11 +15,15 @@ namespace Yi.Framework.Sms.Aliyun
public Client AliyunClient { get; set; }
private ILogger<SmsAliyunManager> _logger;
private SmsAliyunOptions Options { get; set; }
public SmsAliyunManager(ILogger<SmsAliyunManager> logger,IOptions<SmsAliyunOptions> options)
public SmsAliyunManager(ILogger<SmsAliyunManager> logger, IOptions<SmsAliyunOptions> options)
{
_logger = logger;
AliyunClient = CreateClient(Options.AccessKeyId, Options.AccessKeySecret);
Options = options.Value;
if (Options.EnableFeature)
{
_logger = logger;
AliyunClient = CreateClient(Options.AccessKeyId, Options.AccessKeySecret);
}
}
private static Client CreateClient(string accessKeyId, string accessKeySecret)

View File

@@ -12,5 +12,7 @@ namespace Yi.Framework.Sms.Aliyun
public string AccessKeySecret { get; set; }
public string SignName { get; set; }
public bool EnableFeature { get; set; } = true;
}
}

View File

@@ -16,7 +16,6 @@
"DbConnOptions": {
"Url": "DataSource=yi-sqlsugar-dev.db",
"DbType": "Sqlite",
"EnabledDbSeed": false,
"EnabledReadWrite": false,
"EnabledCodeFirst": false,
"EntityAssembly": null,
@@ -42,6 +41,7 @@
"SmsAliyunOptions": {
"AccessKeyId": "",
"AccessKeySecret": "",
"SignName": ""
"SignName": "",
"EnableFeature": false
}
}

View File

@@ -16,7 +16,6 @@
"DbConnOptions": {
"Url": "DataSource=yi-sqlsugar-dev.db",
"DbType": "Sqlite",
"EnabledDbSeed": false,
"EnabledReadWrite": false,
"EnabledCodeFirst": false,
"EntityAssembly": null,
@@ -42,6 +41,7 @@
"SmsAliyunOptions": {
"AccessKeyId": "",
"AccessKeySecret": "",
"SignName": ""
"SignName": "",
"EnableFeature": false
}
}

View File

@@ -11,8 +11,8 @@ namespace Yi.RBAC.Application.Contracts.Identity.Dtos.Account
public string UserName { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
public string? Uuid { get; set; }
public string Uuid { get; set; }
public string? Code { get; set; }
public string Code { get; set; }
}
}

View File

@@ -31,6 +31,9 @@ using Yi.RBAC.Domain.Shared.Identity.Etos;
using System.Net.WebSockets;
using Yi.Framework.Uow;
using Yi.Framework.Caching;
using Yi.Framework.Sms.Aliyun;
using Microsoft.Extensions.Options;
using System.Text.RegularExpressions;
namespace Yi.RBAC.Application.Identity
{
@@ -72,20 +75,38 @@ namespace Yi.RBAC.Application.Identity
[Autowired]
private CacheManager _cacheManager { get; set; }
/// <summary>
/// 效验图片登录验证码
/// </summary>
private void ValidationCaptcha()
{
[Autowired]
private SmsAliyunManager _smsAliyunManager { get; set; }
[Autowired]
private IOptions<SmsAliyunOptions> _smsAliyunManagerOptions { get; set; }
/// <summary>
/// 效验图片登录验证码,无需和账号绑定
/// </summary>
private void ValidationImageCaptcha(LoginInputVo input)
{
//登录不想要验证码 ,不效验
return;
var value = _cacheManager.Get<string>($"Yi:Captcha:{input.Code}");
if (value is not null && value.Equals(input.Uuid))
{
return;
}
throw new UserFriendlyException("验证码错误");
}
/// <summary>
/// 效验电话验证码
/// 效验电话验证码,需要与电话号码绑定
/// </summary>
private void ValidationPhone()
private void ValidationPhoneCaptcha(RegisterDto input)
{
var value = _cacheManager.Get<string>($"Yi:Phone:{input.Phone}");
if (value is not null && value.Equals($"{input.Code}:{input.Uuid}"))
{
return;
}
throw new UserFriendlyException("验证码错误");
}
/// <summary>
@@ -101,7 +122,7 @@ namespace Yi.RBAC.Application.Identity
}
//效验验证码
ValidationCaptcha();
ValidationImageCaptcha(input);
UserEntity user = new();
//登录成功
@@ -130,18 +151,70 @@ namespace Yi.RBAC.Application.Identity
return new { Token = token };
}
/// <summary>
/// 生成验证码
/// </summary>
/// <returns></returns>
[AllowAnonymous]
public CaptchaImageDto GetCaptchaImage()
{
var uuid = Guid.NewGuid();
var code = _securityCode.GetRandomEnDigitalText(4);
//将uuid与codeRedis缓存中心化保存起来登录根据uuid比对即可
//10分钟过期
_cacheManager.Set($"Yi:Captcha:{code}", uuid, new TimeSpan(0, 10, 0));
var imgbyte = _securityCode.GetEnDigitalCodeByte(code);
return new CaptchaImageDto { Img = imgbyte, Code = code, Uuid = uuid };
}
/// <summary>
/// 验证电话号码
/// </summary>
/// <param name="str_handset"></param>
private void ValidationPhone(string str_handset)
{
var res= Regex.IsMatch(str_handset, "^(0\\d{2,3}-?\\d{7,8}(-\\d{3,5}){0,1})|(((13[0-9])|(15([0-3]|[5-9]))|(18[0-9])|(17[0-9])|(14[0-9]))\\d{8})$");
if (res == false)
{
throw new UserFriendlyException("手机号码格式错误!请检查");
}
}
/// <summary>
/// 注册 手机验证码
/// </summary>
/// <returns></returns>
public object PostPhoneCaptchaImage(PhoneCaptchaImageDto input)
[AllowAnonymous]
public async Task<object> PostCaptchaPhone(PhoneCaptchaImageDto input)
{
var code = _securityCode.GetRandomEnDigitalText(4);
var uuid = Guid.NewGuid();
_cacheManager.Set($"Yi:Phone:{input.Phone}", $"{code}:{uuid}", new TimeSpan(0, 10, 0));
ValidationPhone(input.Phone);
var value = _cacheManager.Get<string>($"Yi:Phone:{input.Phone}");
//防止暴刷
if (value is not null)
{
throw new UserFriendlyException($"{input.Phone}已发送过验证码10分钟后可重试");
}
//生成一个4位数的验证码
//发送短信同时生成uuid
//key 电话号码 value:验证码+uuid
var code = _securityCode.GetRandomEnDigitalText(4);
var uuid = Guid.NewGuid();
//未开启短信验证默认8888
if (_smsAliyunManagerOptions.Value.EnableFeature)
{
await _smsAliyunManager.Send(input.Phone, code);
}
else
{
code = "8888";
}
_cacheManager.Set($"Yi:Phone:{input.Phone}", $"{code}:{uuid}", new TimeSpan(0, 10, 0));
return new { Uuid = uuid };
}
@@ -150,6 +223,7 @@ namespace Yi.RBAC.Application.Identity
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[AllowAnonymous]
public async Task<object> PostRegisterAsync(RegisterDto input)
{
if (input.UserName == UserConst.Admin)
@@ -166,7 +240,7 @@ namespace Yi.RBAC.Application.Identity
throw new UserFriendlyException("密码需大于等于6位");
}
//效验验证码,根据电话号码获取 value比对验证码已经uuid
ValidationPhone();
ValidationPhoneCaptcha(input);
@@ -253,23 +327,6 @@ namespace Yi.RBAC.Application.Identity
return Task.FromResult(true);
}
/// <summary>
/// 生成验证码
/// </summary>
/// <returns></returns>
[AllowAnonymous]
public CaptchaImageDto GetCaptchaImage()
{
var uuid = Guid.NewGuid();
var code = _securityCode.GetRandomEnDigitalText(4);
//将uuid与codeRedis缓存中心化保存起来登录根据uuid比对即可
//10分钟过期
_cacheManager.Set($"Yi:Captcha:{code}", uuid, new TimeSpan(0, 10, 0));
var imgbyte = _securityCode.GetEnDigitalCodeByte(code);
return new CaptchaImageDto { Img = imgbyte, Code = code, Uuid = uuid };
}
/// <summary>
/// 更新密码
/// </summary>

View File

@@ -4,14 +4,14 @@
<name>Yi.RBAC.Application</name>
</assembly>
<members>
<member name="M:Yi.RBAC.Application.Identity.AccountService.ValidationCaptcha">
<member name="M:Yi.RBAC.Application.Identity.AccountService.ValidationImageCaptcha(Yi.RBAC.Application.Contracts.Identity.Dtos.Account.LoginInputVo)">
<summary>
效验图片登录验证码
效验图片登录验证码,无需和账号绑定
</summary>
</member>
<member name="M:Yi.RBAC.Application.Identity.AccountService.ValidationPhone">
<member name="M:Yi.RBAC.Application.Identity.AccountService.ValidationPhoneCaptcha(Yi.RBAC.Application.Contracts.Identity.Dtos.Account.RegisterDto)">
<summary>
效验电话验证码
效验电话验证码,需要与电话号码绑定
</summary>
</member>
<member name="M:Yi.RBAC.Application.Identity.AccountService.PostLoginAsync(Yi.RBAC.Application.Contracts.Identity.Dtos.Account.LoginInputVo)">
@@ -21,7 +21,19 @@
<param name="input"></param>
<returns></returns>
</member>
<member name="M:Yi.RBAC.Application.Identity.AccountService.PostPhoneCaptchaImage(Yi.RBAC.Application.Contracts.Identity.Dtos.Account.PhoneCaptchaImageDto)">
<member name="M:Yi.RBAC.Application.Identity.AccountService.GetCaptchaImage">
<summary>
生成验证码
</summary>
<returns></returns>
</member>
<member name="M:Yi.RBAC.Application.Identity.AccountService.ValidationPhone(System.String)">
<summary>
验证电话号码
</summary>
<param name="str_handset"></param>
</member>
<member name="M:Yi.RBAC.Application.Identity.AccountService.PostCaptchatPhone(Yi.RBAC.Application.Contracts.Identity.Dtos.Account.PhoneCaptchaImageDto)">
<summary>
注册 手机验证码
</summary>
@@ -53,12 +65,6 @@
</summary>
<returns></returns>
</member>
<member name="M:Yi.RBAC.Application.Identity.AccountService.GetCaptchaImage">
<summary>
生成验证码
</summary>
<returns></returns>
</member>
<member name="M:Yi.RBAC.Application.Identity.AccountService.UpdatePasswordAsync(Yi.RBAC.Application.Contracts.Identity.Dtos.Account.UpdatePasswordDto)">
<summary>
更新密码