自建登录验证码

This commit is contained in:
biss committed 2026-09-12 08:17:14 +08:00
1 parent 149eb66890
commit 564ffecfed
9 files changed
+75 -49

No files matched your search

@@ -11,7 +11,7 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Lazy.Captcha.Core;
using CaptchaKit.AspNetCore;
namespace Jiaowu.Api.Tests;
@@ -50,7 +50,7 @@ public sealed class AuthControllerTests
var services = new ServiceCollection();
services.AddLogging();
services.AddDistributedMemoryCache();
services.AddCaptcha(new ConfigurationBuilder().Build());
services.AddCaptchaKit();
services.AddDbContext<AppDbContext>(options => options
.UseSqlite(connection)
.ReplaceService<IExecutionStrategyFactory, RetryingExecutionStrategyFactory>());
@@ -113,7 +113,7 @@ public sealed class AuthControllerTests
userManager,
new StubAuthSessionService(),
new TwoFactorLoginTicketService(new Microsoft.AspNetCore.DataProtection.EphemeralDataProtectionProvider()),
scope.ServiceProvider.GetRequiredService<ICaptcha>(),
scope.ServiceProvider.GetRequiredService<ICaptchaKitService>(),
NoOpAppCache.Instance);
var request = new StudentActivationRequest(
student.Name,
@@ -0,0 +1,43 @@
using CaptchaKit.AspNetCore;
using CaptchaKit;
using Microsoft.Extensions.DependencyInjection;
namespace Jiaowu.Api.Tests;
public sealed class CaptchaKitIntegrationTests
{
[Fact]
public async Task Challenge_is_valid_once_and_the_default_generator_returns_a_png()
{
var services = new ServiceCollection();
services.AddDistributedMemoryCache();
services.AddCaptchaKit();
services.AddSingleton<ICaptchaGenerator, KnownGenerator>();
using var provider = services.BuildServiceProvider();
var captcha = provider.GetRequiredService<ICaptchaKitService>();
var challenge = await captcha.CreateAsync();
Assert.Equal("image/png", challenge.ContentType);
Assert.True(challenge.ImageBytes.AsSpan().StartsWith(new byte[] { 137, 80, 78, 71 }));
Assert.True(await captcha.VerifyAsync(challenge.Id, "a1b2"));
Assert.False(await captcha.VerifyAsync(challenge.Id, "a1b2"));
}
[Fact]
public void Default_generator_returns_png_bytes()
{
var challenge = new SkiaTextCaptchaGenerator().Generate(
new CaptchaGenerationRequest("test", DateTimeOffset.UtcNow.AddMinutes(1), 5));
Assert.Equal("image/png", challenge.Challenge.ContentType);
Assert.True(challenge.Challenge.ImageBytes.AsSpan().StartsWith(new byte[] { 137, 80, 78, 71 }));
}
private sealed class KnownGenerator : ICaptchaGenerator
{
public GeneratedCaptcha Generate(CaptchaGenerationRequest request) => new(
new CaptchaChallenge(request.Id, "image/png", new byte[] { 137, 80, 78, 71 }, request.ExpiresAt),
"A1B2");
}
}
@@ -1,26 +0,0 @@
using Lazy.Captcha.Core;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace Jiaowu.Api.Tests;
public sealed class LazyCaptchaIntegrationTests
{
[Fact]
public void Generated_code_is_valid_once_and_returns_image_bytes()
{
var services = new ServiceCollection();
services.AddDistributedMemoryCache();
services.AddCaptcha(new ConfigurationBuilder().Build());
using var provider = services.BuildServiceProvider();
var captcha = provider.GetRequiredService<ICaptcha>();
var id = Guid.NewGuid().ToString("N");
var generated = captcha.Generate(id, expirySeconds: 120);
Assert.NotEmpty(generated.Bytes);
Assert.True(captcha.Validate(id, generated.Code));
Assert.False(captcha.Validate(id, generated.Code));
}
}
Loaded 3 of 9 files, more files were not shown because too many files have changed in this diff. Show more