验证码增加支持分布式缓存功能

This commit is contained in:
shiningrise 2024-12-20 16:55:07 +08:00
parent 2296e83718
commit 6151ec2cec
3 changed files with 17 additions and 0 deletions

View File

@ -3,6 +3,8 @@
// Lazy.Captcha.Core (https://api.gitee.com/pojianbing/lazy-captcha/)
"CaptchaOptions": {
"CacheType": "Memory", // MemoryRedis
"RedisCacheString": "127.0.0.1:6379,password=, defaultDatabase=2", // Redis
"CaptchaType": 10, // 01234567891011
"CodeLength": 1, // , CaptchaType , 2
"ExpirySeconds": 60, //

View File

@ -11,6 +11,7 @@
<ItemGroup>
<PackageReference Include="IGeekFan.AspNetCore.Knife4jUI" Version="0.0.16" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="9.0.0" />
<PackageReference Include="System.Security.Cryptography.Pkcs" Version="9.0.0" />
</ItemGroup>

View File

@ -32,6 +32,7 @@ using System.Linq;
using System.Text.Encodings.Web;
using System.Text.Unicode;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
namespace Admin.NET.Web.Core;
@ -211,6 +212,19 @@ public class Startup : AppStartup
// 验证码
services.AddCaptcha();
var captchaCacheType = App.GetService<IConfiguration>().GetSection("CaptchaOptions:CacheType")?.Value;
if (captchaCacheType == "Redis")
{
var connectionString = App.GetService<IConfiguration>().GetSection("CaptchaOptions:RedisCacheString")?.Value;
// 如果使用redis分布式缓存
services.AddStackExchangeRedisCache(options =>
{
options.Configuration = connectionString;
options.InstanceName = "captcha:";
});
}
// 控制台logo
services.AddConsoleLogo();