feat:完善注册功能
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|||||||
@@ -15,11 +15,15 @@ namespace Yi.Framework.Sms.Aliyun
|
|||||||
public Client AliyunClient { get; set; }
|
public Client AliyunClient { get; set; }
|
||||||
private ILogger<SmsAliyunManager> _logger;
|
private ILogger<SmsAliyunManager> _logger;
|
||||||
private SmsAliyunOptions Options { get; set; }
|
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;
|
Options = options.Value;
|
||||||
|
if (Options.EnableFeature)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
AliyunClient = CreateClient(Options.AccessKeyId, Options.AccessKeySecret);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Client CreateClient(string accessKeyId, string accessKeySecret)
|
private static Client CreateClient(string accessKeyId, string accessKeySecret)
|
||||||
|
|||||||
@@ -12,5 +12,7 @@ namespace Yi.Framework.Sms.Aliyun
|
|||||||
public string AccessKeySecret { get; set; }
|
public string AccessKeySecret { get; set; }
|
||||||
|
|
||||||
public string SignName { get; set; }
|
public string SignName { get; set; }
|
||||||
|
|
||||||
|
public bool EnableFeature { get; set; } = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,6 @@
|
|||||||
"DbConnOptions": {
|
"DbConnOptions": {
|
||||||
"Url": "DataSource=yi-sqlsugar-dev.db",
|
"Url": "DataSource=yi-sqlsugar-dev.db",
|
||||||
"DbType": "Sqlite",
|
"DbType": "Sqlite",
|
||||||
"EnabledDbSeed": false,
|
|
||||||
"EnabledReadWrite": false,
|
"EnabledReadWrite": false,
|
||||||
"EnabledCodeFirst": false,
|
"EnabledCodeFirst": false,
|
||||||
"EntityAssembly": null,
|
"EntityAssembly": null,
|
||||||
@@ -42,6 +41,7 @@
|
|||||||
"SmsAliyunOptions": {
|
"SmsAliyunOptions": {
|
||||||
"AccessKeyId": "",
|
"AccessKeyId": "",
|
||||||
"AccessKeySecret": "",
|
"AccessKeySecret": "",
|
||||||
"SignName": ""
|
"SignName": "",
|
||||||
|
"EnableFeature": false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,6 @@
|
|||||||
"DbConnOptions": {
|
"DbConnOptions": {
|
||||||
"Url": "DataSource=yi-sqlsugar-dev.db",
|
"Url": "DataSource=yi-sqlsugar-dev.db",
|
||||||
"DbType": "Sqlite",
|
"DbType": "Sqlite",
|
||||||
"EnabledDbSeed": false,
|
|
||||||
"EnabledReadWrite": false,
|
"EnabledReadWrite": false,
|
||||||
"EnabledCodeFirst": false,
|
"EnabledCodeFirst": false,
|
||||||
"EntityAssembly": null,
|
"EntityAssembly": null,
|
||||||
@@ -42,6 +41,7 @@
|
|||||||
"SmsAliyunOptions": {
|
"SmsAliyunOptions": {
|
||||||
"AccessKeyId": "",
|
"AccessKeyId": "",
|
||||||
"AccessKeySecret": "",
|
"AccessKeySecret": "",
|
||||||
"SignName": ""
|
"SignName": "",
|
||||||
|
"EnableFeature": false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ namespace Yi.RBAC.Application.Contracts.Identity.Dtos.Account
|
|||||||
public string UserName { get; set; } = string.Empty;
|
public string UserName { get; set; } = string.Empty;
|
||||||
public string Password { 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; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,6 +31,9 @@ using Yi.RBAC.Domain.Shared.Identity.Etos;
|
|||||||
using System.Net.WebSockets;
|
using System.Net.WebSockets;
|
||||||
using Yi.Framework.Uow;
|
using Yi.Framework.Uow;
|
||||||
using Yi.Framework.Caching;
|
using Yi.Framework.Caching;
|
||||||
|
using Yi.Framework.Sms.Aliyun;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
namespace Yi.RBAC.Application.Identity
|
namespace Yi.RBAC.Application.Identity
|
||||||
{
|
{
|
||||||
@@ -72,20 +75,38 @@ namespace Yi.RBAC.Application.Identity
|
|||||||
[Autowired]
|
[Autowired]
|
||||||
private CacheManager _cacheManager { get; set; }
|
private CacheManager _cacheManager { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
[Autowired]
|
||||||
/// 效验图片登录验证码
|
private SmsAliyunManager _smsAliyunManager { get; set; }
|
||||||
/// </summary>
|
|
||||||
private void ValidationCaptcha()
|
|
||||||
{
|
|
||||||
|
|
||||||
|
[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>
|
||||||
/// 效验电话验证码
|
/// 效验电话验证码,需要与电话号码绑定
|
||||||
/// </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>
|
/// <summary>
|
||||||
@@ -101,7 +122,7 @@ namespace Yi.RBAC.Application.Identity
|
|||||||
}
|
}
|
||||||
|
|
||||||
//效验验证码
|
//效验验证码
|
||||||
ValidationCaptcha();
|
ValidationImageCaptcha(input);
|
||||||
|
|
||||||
UserEntity user = new();
|
UserEntity user = new();
|
||||||
//登录成功
|
//登录成功
|
||||||
@@ -130,18 +151,70 @@ namespace Yi.RBAC.Application.Identity
|
|||||||
return new { Token = token };
|
return new { Token = token };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 生成验证码
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
|
||||||
|
[AllowAnonymous]
|
||||||
|
public CaptchaImageDto GetCaptchaImage()
|
||||||
|
{
|
||||||
|
var uuid = Guid.NewGuid();
|
||||||
|
var code = _securityCode.GetRandomEnDigitalText(4);
|
||||||
|
//将uuid与code,Redis缓存中心化保存起来,登录根据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>
|
||||||
/// 注册 手机验证码
|
/// 注册 手机验证码
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public object PostPhoneCaptchaImage(PhoneCaptchaImageDto input)
|
[AllowAnonymous]
|
||||||
|
public async Task<object> PostCaptchaPhone(PhoneCaptchaImageDto input)
|
||||||
{
|
{
|
||||||
var code = _securityCode.GetRandomEnDigitalText(4);
|
ValidationPhone(input.Phone);
|
||||||
var uuid = Guid.NewGuid();
|
var value = _cacheManager.Get<string>($"Yi:Phone:{input.Phone}");
|
||||||
_cacheManager.Set($"Yi:Phone:{input.Phone}", $"{code}:{uuid}", new TimeSpan(0, 10, 0));
|
|
||||||
|
//防止暴刷
|
||||||
|
if (value is not null)
|
||||||
|
{
|
||||||
|
throw new UserFriendlyException($"{input.Phone}已发送过验证码,10分钟后可重试");
|
||||||
|
}
|
||||||
//生成一个4位数的验证码
|
//生成一个4位数的验证码
|
||||||
//发送短信,同时生成uuid
|
//发送短信,同时生成uuid
|
||||||
//key: 电话号码 value:验证码+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 };
|
return new { Uuid = uuid };
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,6 +223,7 @@ namespace Yi.RBAC.Application.Identity
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="input"></param>
|
/// <param name="input"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
|
[AllowAnonymous]
|
||||||
public async Task<object> PostRegisterAsync(RegisterDto input)
|
public async Task<object> PostRegisterAsync(RegisterDto input)
|
||||||
{
|
{
|
||||||
if (input.UserName == UserConst.Admin)
|
if (input.UserName == UserConst.Admin)
|
||||||
@@ -166,7 +240,7 @@ namespace Yi.RBAC.Application.Identity
|
|||||||
throw new UserFriendlyException("密码需大于等于6位!");
|
throw new UserFriendlyException("密码需大于等于6位!");
|
||||||
}
|
}
|
||||||
//效验验证码,根据电话号码获取 value,比对验证码已经uuid
|
//效验验证码,根据电话号码获取 value,比对验证码已经uuid
|
||||||
ValidationPhone();
|
ValidationPhoneCaptcha(input);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -253,23 +327,6 @@ namespace Yi.RBAC.Application.Identity
|
|||||||
return Task.FromResult(true);
|
return Task.FromResult(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 生成验证码
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
|
|
||||||
[AllowAnonymous]
|
|
||||||
public CaptchaImageDto GetCaptchaImage()
|
|
||||||
{
|
|
||||||
var uuid = Guid.NewGuid();
|
|
||||||
var code = _securityCode.GetRandomEnDigitalText(4);
|
|
||||||
//将uuid与code,Redis缓存中心化保存起来,登录根据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>
|
||||||
/// 更新密码
|
/// 更新密码
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -4,14 +4,14 @@
|
|||||||
<name>Yi.RBAC.Application</name>
|
<name>Yi.RBAC.Application</name>
|
||||||
</assembly>
|
</assembly>
|
||||||
<members>
|
<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>
|
||||||
效验图片登录验证码
|
效验图片登录验证码,无需和账号绑定
|
||||||
</summary>
|
</summary>
|
||||||
</member>
|
</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>
|
||||||
效验电话验证码
|
效验电话验证码,需要与电话号码绑定
|
||||||
</summary>
|
</summary>
|
||||||
</member>
|
</member>
|
||||||
<member name="M:Yi.RBAC.Application.Identity.AccountService.PostLoginAsync(Yi.RBAC.Application.Contracts.Identity.Dtos.Account.LoginInputVo)">
|
<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>
|
<param name="input"></param>
|
||||||
<returns></returns>
|
<returns></returns>
|
||||||
</member>
|
</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>
|
||||||
注册 手机验证码
|
注册 手机验证码
|
||||||
</summary>
|
</summary>
|
||||||
@@ -53,12 +65,6 @@
|
|||||||
</summary>
|
</summary>
|
||||||
<returns></returns>
|
<returns></returns>
|
||||||
</member>
|
</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)">
|
<member name="M:Yi.RBAC.Application.Identity.AccountService.UpdatePasswordAsync(Yi.RBAC.Application.Contracts.Identity.Dtos.Account.UpdatePasswordDto)">
|
||||||
<summary>
|
<summary>
|
||||||
更新密码
|
更新密码
|
||||||
|
|||||||
Reference in New Issue
Block a user