Merge branch 'dev_add_auth_1029' of http://192.168.5.200:8081/ql/apismnagaer_backend into dev_add_auth_1029
This commit is contained in:
commit
ba4157ef1a
@ -19,6 +19,10 @@ namespace Apimanager_backend.Controllers
|
||||
{
|
||||
this.userService = userService;
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取用户个人信息
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
[Authorize(Roles = "User")]
|
||||
public async Task<ActionResult<ResponseBase<UserInfoDto>>> UserInfo()
|
||||
@ -32,5 +36,43 @@ namespace Apimanager_backend.Controllers
|
||||
);
|
||||
return Ok(res);
|
||||
}
|
||||
/// <summary>
|
||||
/// 重置用户密码
|
||||
/// </summary>
|
||||
/// <param name="dto"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<ResponseBase<object?>>> Resetpassword([FromBody]ResetPasswordDto dto)
|
||||
{
|
||||
try
|
||||
{
|
||||
await userService.ResetPasswordAsync(dto.Email, dto.Code, dto.NewPassword);
|
||||
var res = new ResponseBase<object?>(
|
||||
code:1000,
|
||||
message:"Success",
|
||||
data: null
|
||||
);
|
||||
return Ok(res);
|
||||
}catch(BaseException e)
|
||||
{
|
||||
var res = new ResponseBase<object?>(
|
||||
code:e.code,
|
||||
message:e.message,
|
||||
data:null
|
||||
);
|
||||
return StatusCode(400,res);
|
||||
}
|
||||
}
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<ResponseBase<object?>>> SendResetEmail([FromQuery]string email)
|
||||
{
|
||||
await userService.SendResetPasswordEmailAsync(email);
|
||||
var res = new ResponseBase<object?>(
|
||||
code: 1000,
|
||||
message: "Success",
|
||||
data: null
|
||||
);
|
||||
return Ok(res);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
9
Apimanager_backend/Dtos/ResetPasswordDto.cs
Normal file
9
Apimanager_backend/Dtos/ResetPasswordDto.cs
Normal file
@ -0,0 +1,9 @@
|
||||
namespace Apimanager_backend.Dtos
|
||||
{
|
||||
public class ResetPasswordDto
|
||||
{
|
||||
public string Email { get; set; }
|
||||
public string NewPassword { get; set; }
|
||||
public string Code { get; set; }
|
||||
}
|
||||
}
|
||||
@ -20,7 +20,7 @@ namespace Apimanager_backend.Services
|
||||
/// <param name="token">重置密码的令牌</param>
|
||||
/// <param name="newPassword">新的密码</param>
|
||||
/// <returns>异步操作</returns>
|
||||
Task ResetPasswordAsync(string email, string token, string newPassword);
|
||||
Task ResetPasswordAsync(string email, string code, string newPassword);
|
||||
|
||||
/// <summary>
|
||||
/// 获取用户信息。
|
||||
|
||||
@ -3,9 +3,12 @@ using Apimanager_backend.Data;
|
||||
using Apimanager_backend.Dtos;
|
||||
using Apimanager_backend.Exceptions;
|
||||
using Apimanager_backend.Models;
|
||||
using Apimanager_backend.Tools;
|
||||
using AutoMapper;
|
||||
using Microsoft.AspNetCore.Connections.Features;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Diagnostics;
|
||||
using StackExchange.Redis;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace Apimanager_backend.Services
|
||||
@ -14,10 +17,17 @@ namespace Apimanager_backend.Services
|
||||
{
|
||||
private readonly ApiContext apiContext;
|
||||
private readonly IMapper mapper;
|
||||
public UserService(ApiContext apiContext,IMapper automapper)
|
||||
private readonly ILogger<IUserService> logger;
|
||||
private readonly IConnectionMultiplexer redis;
|
||||
private readonly IEmailService emailService;
|
||||
private readonly int DbSet = 2;
|
||||
public UserService(ApiContext apiContext,IMapper automapper,ILogger<IUserService> logger,IConnectionMultiplexer redis,IEmailService emailService)
|
||||
{
|
||||
this.apiContext = apiContext;
|
||||
this.mapper = automapper;
|
||||
this.logger = logger;
|
||||
this.redis = redis;
|
||||
this.emailService = emailService;
|
||||
}
|
||||
public async Task<UserInfoDto> GetUserAsync(int userId)
|
||||
{
|
||||
@ -40,14 +50,41 @@ namespace Apimanager_backend.Services
|
||||
return await apiContext.Users.AnyAsync(x => x.Username == username);
|
||||
}
|
||||
|
||||
public Task ResetPasswordAsync(string email, string token, string newPassword)
|
||||
public async Task ResetPasswordAsync(string email, string code, string newPassword)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
//校验验证码
|
||||
var db = redis.GetDatabase(DbSet);
|
||||
var value = await db.StringGetAsync(email);
|
||||
if (!value.HasValue || value.ToString() != code)
|
||||
{
|
||||
throw new BaseException(5005, "验证码错误");
|
||||
}
|
||||
//验证成功,开始重置流程
|
||||
var user = await apiContext.Users.FirstOrDefaultAsync(x => x.Email == email);
|
||||
if(user == null)
|
||||
{
|
||||
throw new BaseException(2004, "用户不存在");
|
||||
}
|
||||
//修改密码
|
||||
user.PassHash = newPassword;
|
||||
apiContext.Users.Update(user);
|
||||
await apiContext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public Task SendResetPasswordEmailAsync(string email)
|
||||
public async Task SendResetPasswordEmailAsync(string email)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var randomCode = RandomCodeHelper.GetRandomCodeStr();
|
||||
//记录到redis
|
||||
var db = redis.GetDatabase(DbSet);
|
||||
bool redisSuccess = await db.StringSetAsync(email,randomCode,TimeSpan.FromHours(1));
|
||||
if (!redisSuccess)
|
||||
{
|
||||
throw new BaseException(1005, "Redis Str Set Error");
|
||||
}
|
||||
string subject = "重置验证码";
|
||||
string body = $"您的重置验证码为:{randomCode}<br>有效期60分钟!";
|
||||
//发送邮件
|
||||
await emailService.SendEmailAsync(email,subject,body);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user