自建登录验证码
This commit is contained in:
1 parent
149eb66890
commit
564ffecfed
9 files changed
+75
-49
No files matched your search
@@ -4,7 +4,7 @@ using Jiaowu.Api.Domain.Academic;
|
||||
using Jiaowu.Api.Domain.Identity;
|
||||
using Jiaowu.Api.Infrastructure.Auth;
|
||||
using Jiaowu.Api.Infrastructure.Caching;
|
||||
using Lazy.Captcha.Core;
|
||||
using CaptchaKit.AspNetCore;
|
||||
using Jiaowu.Api.Infrastructure.Persistence;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
@@ -21,7 +21,7 @@ public sealed class AuthController(
|
||||
UserManager<ApplicationUser> userManager,
|
||||
IAuthSessionService authSessionService,
|
||||
ITwoFactorLoginTicketService twoFactorTickets,
|
||||
ICaptcha captcha,
|
||||
ICaptchaKitService captcha,
|
||||
IAppCache cache) : ControllerBase
|
||||
{
|
||||
[AllowAnonymous]
|
||||
@@ -140,13 +140,12 @@ public sealed class AuthController(
|
||||
[AllowAnonymous]
|
||||
[EnableRateLimiting("public-auth")]
|
||||
[HttpPost("login/captcha")]
|
||||
public ActionResult<LazyLoginCaptchaResponse> CreateLoginCaptcha()
|
||||
public async Task<ActionResult<LoginCaptchaResponse>> CreateLoginCaptcha(CancellationToken cancellationToken)
|
||||
{
|
||||
var captchaId = Guid.NewGuid().ToString("N");
|
||||
var generated = captcha.Generate(captchaId, expirySeconds: 120);
|
||||
return Ok(new LazyLoginCaptchaResponse(
|
||||
captchaId,
|
||||
$"data:image/gif;base64,{Convert.ToBase64String(generated.Bytes)}"));
|
||||
var generated = await captcha.CreateAsync(cancellationToken);
|
||||
return Ok(new LoginCaptchaResponse(
|
||||
generated.Id,
|
||||
$"data:{generated.ContentType};base64,{Convert.ToBase64String(generated.ImageBytes)}"));
|
||||
}
|
||||
|
||||
[AllowAnonymous]
|
||||
@@ -156,7 +155,7 @@ public sealed class AuthController(
|
||||
LoginRequest request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (!captcha.Validate(request.CaptchaId, request.CaptchaCode))
|
||||
if (!await captcha.VerifyAsync(request.CaptchaId, request.CaptchaCode, cancellationToken))
|
||||
return Unauthorized(LoginCaptchaProblem());
|
||||
var user = await userManager.FindByNameAsync(request.UserName);
|
||||
if (user is null || !user.IsEnabled)
|
||||
@@ -334,7 +333,7 @@ public sealed record LoginRequest(
|
||||
[Required, MinLength(3), MaxLength(16)] string CaptchaCode,
|
||||
bool IsNativeApp = false);
|
||||
|
||||
public sealed record LazyLoginCaptchaResponse(string CaptchaId, string ImageData);
|
||||
public sealed record LoginCaptchaResponse(string CaptchaId, string ImageData);
|
||||
|
||||
public sealed record TotpLoginRequest(
|
||||
[Required, MinLength(20), MaxLength(2048)] string TwoFactorTicket,
|
||||
|
||||
Reference in new issue
Block a user